Robust ASP.NET control referencing in JavaScript(encosia.com)

submitted by gt1329agt1329a(7849) 4 years, 9 months ago

How to reference your controls in JavaScript by their true ClientID, so that changes in nesting and/or location won't break your client script.

1 comment |category: |Views: 3

tags: another

new Add a live kick counter to your blog >> liveImage

You can even customize the image by choosing your own colors, and then clicking the button below to update the preview and the html code:

  • "Kick It" text
  • "Kick It" background
  • kick count text
  • kick count background
  • border

Simply copy and paste this HTML into your blog post.


Users who kicked this story:
Comments:

posted by jaideeptjaideept(0) 4 years, 5 months ago 0

another way to solve this issue could be this.
i have found this solution from
http://weblogs.asp.net/scottgu/archive/2006/04/18/Great-New-Advanced-Article-on-ASP.NET-2.0-Master-Pages.aspx
its so easy to use...hete it is:

- define one stringbuilder
System.Text.StringBuilder strControls = new System.Text.StringBuilder();

- have one private method in your code-behind or even inside one utility class

private void ControlWalker(Control ctrl)
{
if(ctrl.HasControls())
{
foreach(Control ctrl2 in ctrl.Controls)
{
ControlWalker(ctrl2);
}
}
else
{
string sClientId = ctrl.ClientID;
string sOriginalId = ctrl.ID;
strControls.Append("var ");
strControls.Append(sOriginalId);
strControls.Append("=document.getElementById('");
strControls.Append(sClientId);
strControls.Append("');");
}
}

- in your page_load

protected void Page_Load(object sender, EventArgs e)
{
controlWalker(this.Page);
Page.ClientScript.RegisterStartupScript(this.GetType(), "ctrl_ids", strControls.ToString(), true);
}
looking into the view source , all the control's client ids are available which can very well be used in javascript.


http://encosia.com/2007/08/08/robust-aspnet-control-referencing-in-javascript/

Reply

information Login or create an account to comment on this story