Access TextBox inside UserControl Error: error obejct reference not set

on Oct 01, 2018 06:54 AM

hello i am trying to access textbox value which is inside usercontrol from content page.

i am using master page.

here is my user control

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="AccessUser.ascx.cs" Inherits="UserControl_AccessUser" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

my content page- i am using master page

    <uc1:AccessUser runat="server" ID="AccessUser" />  <br />
    <asp:Label ID="Label1" runat="server" Text="No Data"></asp:Label><br />
    <asp:Button ID="Button1" runat="server" Text="Fetch TextBox Value from User Control" OnClick="Button1_Click" />

button click event

   protected void Button1_Click(object sender, EventArgs e)
        UserControl uc = this.Page.FindControl("AccessUser") as UserControl;
        string name = (uc.FindControl("TextBox1") as TextBox).Text;
        Label1.Text = name;        

it gives error obejct reference not set

Download FREE API for Word, Excel and PDF in ASP.Net: Download
on Oct 01, 2018 07:15 AM

ok this is working for me

TextBox box = (TextBox)this.AccessUser.FindControl("TextBox1");
Label1.Text = box.Text;