Reputation: 43
I have a problem when I use user control to get div tag from aspx page that contain my user control.
I have a div in aspx page: <div id="divMask" class="divMask" runat="server"></div>
and in my user control I want get div ID to add attribute "onclick" but I can't get it.
My aspx page:
<div id="dvConfirmAcc" runat="server" style="display: none; position: absolute; z-index: 2000; top: 155px; left: 400px;">
<uc1:ConfirmAccount ID="ucConfirmAccount" runat="server" />
</div>
<div id="divMask" class="divMask" runat="server"></div>
In my user control ucConfirmAccount:
protected void Page_Load(object sender, EventArgs e)
{
string url = "some url";
[I want get divMask at here].Attributes.Add("onclick", "closeMessage('" + url + "')");
}
Please help me.
Upvotes: 3
Views: 15129
Reputation: 6812
You can't access div defined in Page
directly from UserControl
, instead you have to use Page.FindControl
method
protected void Page_Load(object sender, EventArgs e)
{
System.Web.UI.Control divMask = (System.Web.UI.Control)this.Page.FindControl("divMask");
if (divMask is System.Web.UI.HtmlControls.HtmlGenericControl)
{
System.Web.UI.HtmlControls.HtmlGenericControl htmlCtrl = (System.Web.UI.HtmlControls.HtmlGenericControl)divMask;
htmlCtrl.Attributes[..] = "...";
}
}
Upvotes: 4
Reputation: 3235
It would be more helpful if you could post some code so we could help you more.
Try reading these links, they might help you in your problem:
.NET In Search Of ASP.Net Controls
ASP.NET 2.0 MasterPages and FindControl()
Find Control in Multiview
Upvotes: 0