Reputation: 33
I need to find a div in the master page from a webusercontrol.ascx
I tried it with:
HtmlGenericControl Mydiv = (HtmlGenericControl)Page.Master.FindControl("dvAlert");
But that doesn't work.
Upvotes: 0
Views: 714
Reputation: 39025
Make your div run at server.
Make a public readonly property YourProperty
that exposes that control on the masterpage.
Acces the control form your ascx like this:
Page.Master.YourProperty
EDIT: It looks like that the OP wants to find it client-side with jQuery. So, he has many options.
According to his comment, he can inject the script form server side. So he needs a reliable selector. He has several options to get it:
1) Don't make the control runat="server" and give it a fixed id that he knows won't be repeated in the page <div id="FixedUniqueId"...
. Use this selector: $('#FixedUniqueId')
2) Make the conrol run at server, and use the ClientId. Construct the jQuery selector like this "$('#" + Page.Master.YourProperty.ClientId +"')"
3) Use the know part of the id (the end of the id) and find it using jquery $("element[id$='dvAlert']")
But as OP really wants to show an alert, it's much better to create a client-side javascript function on the masterpage itself that updates the contents of the div, using option 1) for the id. Then, instead of injecting a script in which the script is needed, the injected script can be reduced to a call to that function:
function showAlert(message) {
$('#FixedUniqueId').text(message);
}
Then, the injected script can be as simple as a call to showAlert("message")
. This way he can change the script and page design anytime (the way of showing the alert) and change the function on the MasterPage, and this without breaking the UC functionality.
Upvotes: 4