How to Access value of TextBox inside ascx control from codebehind

R

rodrigo

This is what I am doing



public class GridCustomPaging : System.Web.UI.Page

{

protected System.Web.UI.WebControls.DataGrid DgSearch;

protected System.Web.UI.HtmlControls.HtmlForm frmsearch;

protected System.Web.UI.WebControls.Label CurrentPage;

protected System.Web.UI.WebControls.Label TotalPages;

protected System.Web.UI.WebControls.LinkButton FirstPage;

protected System.Web.UI.WebControls.LinkButton PreviousPage;

protected System.Web.UI.WebControls.LinkButton NextPage;

protected System.Web.UI.WebControls.LinkButton LastPage;

protected System.Web.UI.WebControls.TextBox Categories1_Textsearch;



protected Int32 _currentPageNumber = 1;



private void Page_load(object sender, System.EventArgs e)

{

if ( !Page.IsPostBack )

{

BindData();

}

}









private void BindData()

{



Response.Write(Categories1_Textsearch.Text);



}


At this point I get this error: Object reference not set to an
instance of an object.


I tryed these two options:
Categories1_Textsearch =
(TextBox)this.Page.Parent.FindControl("Categories1_Textsearch");
and
Categories1_Textsearch =
(TextBox)this.Page.FindControl("Categories1_Textsearch");

Still get the same error

Do I have to reference the ascx control page inside codebehind? How to
do it?

categories.ascx is the control page.

Categories1_Textsearch is the name of the textbox inside
categories.ascx. Actually the original name is only Textsearch, but
Frameworks, renames it behind the scenes. Categories1_Textsearch is
what I see in the source of the html.

I tried to expose the public properties with no success. But that does
not work since both the aspx and ascx are using codebehind.



Thanks
Rod
 
R

Raterus

The usercontrol variable has a FindControl method, use that. As you see, the page level FindControl method won't dig into the usercontrol.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top