Click here to Skip to main content
15,509,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi experts
i have a text box in a user control. my user control is in master page. i want to access this text box from default.aspx.

how i can access to text box?

thanks in advance
Posted
Updated 27-Aug-13 2:29am
v2
Comments
Maarten Kools 27-Aug-13 7:38am    
You can just loop recursively through the child controls of a certain parent control (be it a page, or the user control itself) until you find the control you're looking for. Or if the TextBox is a direct child of the user control, just use the FindControl[^] method.

hi
i have a user control which contain below textbox having id "txtname"

and i have change textbox value from default.aspx page like as below

((this.Master.FindControl("usercontrol") as UserControl).FindControl("txtname") as TextBox).Text = "pqr";
 
Share this answer
 
Comments
zinajoonjooni 27-Aug-13 7:54am    
i add my user control in web.config

<add src="~/UserControls/UserLoginStatus.ascx" tagname="UserLoginStatus" tagprefix="abs">
pradippatel99 27-Aug-13 8:43am    
i have registered user control in web config file then i used.
it works.
can you check which control you cannot find user control or image of usercontrol ?
zinajoonjooni 28-Aug-13 0:12am    
its my user control in web config:
add src="~/UserControls/UserLoginStatus.ascx" tagname="UserLoginStatus" tagprefix="abs"

it cannot find user control.
This is also fine.

TextBox txt = ((UserControl)Master.FindControl("TextBox1")).FindControl("TextBox1") as TextBox;
 
Share this answer
 
Comments
zinajoonjooni 28-Aug-13 1:11am    
where is my user control's name here?
Thomas ktg 29-Aug-13 1:25am    
I have given same name for both userControl and TextBox. The TextBox1 in the first place represents the usercontrol.

TextBox txt = ((UserControl)Master.FindControl("UserControlTextBox1")).FindControl("TextBox1") as TextBox;
XML
for that you need to put usercontrol in other folder then use it check my code as below
in webconfig file
 &lt;pages masterPageFile="~/Site.Master"&gt;
      <controls>
        <add src="~/UserControls/WebUserControl1.ascx" tagname="userControltag" tagprefix="uc1"></add>
      </controls>

    &lt;/pages&gt;

in site master
&lt;uc1:userControltag ID="usercontrol"  runat="server" /&gt;

in sitemaster.cs
you can access usercontrol.

please follow folder structure like usercontrol should be put in other fodler

UserControl ::- WebUserControl1.ascx
 
Share this answer
 
v2
thanks pradippatel99
its my code:

C#
if (filAvatar.HasFile)
                {
                    BLL.Users.UploadAvatarForAlbum(Convert.ToInt32(Page.User.Identity.Name), filAvatar, 0, strAddress, 0);

                    ((this.Master.FindControl("UserLoginStatus") as UserControl).FindControl("imgAvatar") as Image).ImageUrl = "pqr";

}

but when i run it i got this error:

Object reference not set to an instance of an object.

i add my user control in web.config:

HTML
<add src="~/UserControls/UserLoginStatus.ascx" tagname="UserLoginStatus" tagprefix="abs" />
 
Share this answer
 
v4
Comments
Anuja Pawar Indore 27-Aug-13 7:58am    
Use Improve question link to update your question. Don't add your question into an answer block.
ahammed056 26-Oct-16 5:06am    
hi i had problem on cross page post back....

i had page with saved.aspx in that user control consists of gridview...

i want to display the usercontrol grid view new viewprofile.aspx

the codes are


string command = e.CommandName;
string mprofilecode = e.CommandArgument.ToString();
switch (command)
{


case "viewprofile":
gvsaveProfile.Visible = true;
Panel_saveprofile.Visible = true;
userentitys uur = new userentitys();
uur.Profilecode = Session["Profilecode"].ToString();
dataaccesslayer dbfound = new dataaccesslayer();
this.Mp_view_profile.Uc_viewprofile(mprofilecode);
dbfound.rpv(uur, mprofilecode);
break;

<asp:LinkButton ID="LinkButton1" runat="server" CssClass="fa fa-search" PostBackUrl="~/viewprofile.aspx" CommandName="viewprofile" CommandArgument='<%# Eval("profilecode") %>'> View Profile


viewpage.aspx page load

if (this.Page.PreviousPage != null)
{
GridView GridView1 = (GridView)this.Page.PreviousPage.FindControl("Panel_viewprofile").FindControl("Mp_view_profile").FindControl("gvieviewprofile");
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900