Thursday, 12 March 2015

How can i access controls in a user control.?


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

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<%@ 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>


</head>
<body>

<form id="form1" runat="server">

<div>

<uc1:WebUserControl ID="WebUserControl1" runat="server" />



</div>
</form>
</body>

</html>

This is aspc.cs code

protected void Page_Load(object sender, EventArgs e)
{
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,

No comments:

Post a Comment