Answered by:
How can i access controls in a user control.?

Question
-
User1253338400 posted
Hi,
i have defined a user control with a label and a datagrid . Now i have dropped that user control on the default.aspx page no problems there. I have populated a dataset and want to bind that to the datagrid, BUT how do i access the datagrid, ??? The datagrid name is datagrid1 and when i type intellisense doesnt recognise it. Do i have to type the control id and then the control name ?
thanks
robby
Sunday, February 24, 2008 6:11 AM
Answers
-
User-891526991 posted
You can access user controls thru properties. In your User Control code behind create a property (for accessing the controls).
public DataGrid ChildGrid
{
get { return datagrid1; }
}in your default.aspx page access it using usercontrol name as uc1.ChildGrid. (uc1 is the ID of your usercontrol in default.aspx page)
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, February 24, 2008 10:06 PM -
User-813426089 posted
u want a set property in ur user conntrol.
Public DataGrid Grid
{
set
{
urGridid.DataSource=value;
urGridid.DataBind();
}
}
call this property in ur page wher u use the actaull user control.
UserControlId.Grid=_DataSource;
hope this helps
sam
mark this answer if this helps
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, February 24, 2008 11:00 PM -
User-582865899 posted
Hi I am sending an example
This is usercontrol source code
<%
@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %> <asp:GridView ID="GridView1" runat="server"></
asp:GridView><
asp:TextBox ID="TextBox1" runat="server">fgggggf</asp:TextBox>this is web form Source code
<%
@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><
html xmlns="http://www.w3.org/1999/xhtml" ><
head runat="server"> <title>Untitled Page</title><
body> <form id="form1" runat="server"> <div> <uc1:WebUserControl ID="WebUserControl1" runat="server" /></
body></
html>This is aspc.cs code
{
WebUserControl WebUserControl1 = (WebUserControl)FindControl("WebUserControl1"); TextBox TextBox1 = (TextBox)WebUserControl1.FindControl("TextBox1"); GridView GridView1 = (GridView)WebUserControl1.FindControl("GridView1");TextBox1.Visible =
false; DataSet ds = Class1.Getdata();GridView1.DataSource = ds;
GridView1.DataBind();
}
Try this one. Hope this helps,
Please remember to click “Mark as Answer” on the post that helps you
Regards
Radhika
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, February 25, 2008 5:35 AM
All replies
-
User-891526991 posted
You can access user controls thru properties. In your User Control code behind create a property (for accessing the controls).
public DataGrid ChildGrid
{
get { return datagrid1; }
}in your default.aspx page access it using usercontrol name as uc1.ChildGrid. (uc1 is the ID of your usercontrol in default.aspx page)
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, February 24, 2008 10:06 PM -
User-813426089 posted
u want a set property in ur user conntrol.
Public DataGrid Grid
{
set
{
urGridid.DataSource=value;
urGridid.DataBind();
}
}
call this property in ur page wher u use the actaull user control.
UserControlId.Grid=_DataSource;
hope this helps
sam
mark this answer if this helps
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, February 24, 2008 11:00 PM -
User-582865899 posted
Hi I am sending an example
This is usercontrol source code
<%
@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %> <asp:GridView ID="GridView1" runat="server"></
asp:GridView><
asp:TextBox ID="TextBox1" runat="server">fgggggf</asp:TextBox>this is web form Source code
<%
@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><
html xmlns="http://www.w3.org/1999/xhtml" ><
head runat="server"> <title>Untitled Page</title><
body> <form id="form1" runat="server"> <div> <uc1:WebUserControl ID="WebUserControl1" runat="server" /></
body></
html>This is aspc.cs code
{
WebUserControl WebUserControl1 = (WebUserControl)FindControl("WebUserControl1"); TextBox TextBox1 = (TextBox)WebUserControl1.FindControl("TextBox1"); GridView GridView1 = (GridView)WebUserControl1.FindControl("GridView1");TextBox1.Visible =
false; DataSet ds = Class1.Getdata();GridView1.DataSource = ds;
GridView1.DataBind();
}
Try this one. Hope this helps,
Please remember to click “Mark as Answer” on the post that helps you
Regards
Radhika
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, February 25, 2008 5:35 AM -
User1253338400 posted
Hi,
when i go to the line to find the gridview i get an instance object error
also i used WebControl not WebUserControl when declaring the usercontrol
thanks
robby
Monday, February 25, 2008 6:42 AM -
User-398804652 posted
<STRIKE>WebUserControl WebUserControl1 = (WebUserControl)FindControl("WebUserControl1");</STRIKE>
TextBox TextBox1 = (TextBox)WebUserControl1.FindControl("TextBox1");
GridView GridView1 = (GridView)WebUserControl1.FindControl("GridView1");
TextBox1.Visible = false;
DataSet ds = Class1.Getdata();
GridView1.DataSource = ds;
GridView1.DataBind();
The first line of code is not required at all.Since the control is there in aspx and is a server control,there is no need of asigning it to a webuser control. Also it shud be accessed using the "user control" class and not webusercontrol or webcontrol
Saturday, January 24, 2009 7:37 AM