Wednesday, March 28, 2012

Accessing objects outside update panel

I am trying to access the label or div which is outside the update panel after its post back occurs.

here is code...

how can I make that alert work in Button1_Click method?

thanks in advance...

protected void Page_Load(object sender, EventArgs e) { Label1.Text ="Standard Page Load: " + DateTime.Now.ToLongTimeString(); Label2.Text ="Standard Page Load: " + DateTime.Now.ToLongTimeString(); }protected void Button1_Click(object sender, EventArgs e) { lt.Text ="alert(document.getElementById('" + Label1.ClientID +"').value);"; Label2.Text ="Ajax Page Load: " + DateTime.Now.ToLongTimeString() + lt.Text; }

<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>Untitled Page</title></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><div> <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <ContentTemplate> <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label> <br /> <br /> <script language="javascript"> <asp:Literal id="lt" runat="server" /> </script> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /> </ContentTemplate> </asp:UpdatePanel> </form></body></html>

Try this:

protected void Button1_Click(object sender, EventArgs e)
{
string script = "alert($get('" + Label1.ClientID + "').innerText)";

ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", script, true);

Label2.Text ="Ajax Page Load: " + DateTime.Now.ToLongTimeString();

}


thanks Rashid... that was helpful.


No comments:

Post a Comment