Access TextBox inside UserControl Error: error obejct reference not set

Last Reply 5 months ago By nauna

Posted 5 months ago

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

You are viewing reply posted by: nauna 5 months ago.
Posted 5 months ago

ok this is working for me

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