admin管理员组文章数量:1333442
I have created a a subclass of Table in my ASP.NET project which creates. The table uses a class that formats and creates TableRows and TableCells which we can call RowCreator. So MyTable calls rowCreator.CreateRow() and gets back a TableRow with a lot of goodies in it.
In this TableRow there is a TextBox which is supposed to trigger a javascript method on the onblur event, which is added by the RowCreator class.
textBox.Attributes.Add("onblur", "javascriptMethod('" + textbox.ClientID + "');");
I've also tried created a subclass of textBox which implements a method that adds the onblur event:
Attributes.Add("onblur", "javascriptMethod('" + this + "');")
Which doesn't work. The ID is just the namespace of the textbox subclass.
And the JavaScript method is very simple:
function javascriptMethod(boxId) {
alert(boxId);
}
The trouble is, and I'm guessing this is because the textbox control hasn't been added to the control collection yet, that the boxId isn't the proper client ID. It is the same as the server side ID. Is there a way of getting the proper ID without having to add the row using Controls.Add on the page first? Any other suggestions?
The reason I'm even doing this is to read the textbox contents from a javascript function whenever it's been changed. Maybe there's a better way to do this?
I have created a a subclass of Table in my ASP.NET project which creates. The table uses a class that formats and creates TableRows and TableCells which we can call RowCreator. So MyTable calls rowCreator.CreateRow() and gets back a TableRow with a lot of goodies in it.
In this TableRow there is a TextBox which is supposed to trigger a javascript method on the onblur event, which is added by the RowCreator class.
textBox.Attributes.Add("onblur", "javascriptMethod('" + textbox.ClientID + "');");
I've also tried created a subclass of textBox which implements a method that adds the onblur event:
Attributes.Add("onblur", "javascriptMethod('" + this + "');")
Which doesn't work. The ID is just the namespace of the textbox subclass.
And the JavaScript method is very simple:
function javascriptMethod(boxId) {
alert(boxId);
}
The trouble is, and I'm guessing this is because the textbox control hasn't been added to the control collection yet, that the boxId isn't the proper client ID. It is the same as the server side ID. Is there a way of getting the proper ID without having to add the row using Controls.Add on the page first? Any other suggestions?
The reason I'm even doing this is to read the textbox contents from a javascript function whenever it's been changed. Maybe there's a better way to do this?
Share Improve this question edited Jan 19, 2009 at 11:12 Peter Evjan asked Jan 19, 2009 at 10:09 Peter EvjanPeter Evjan 2,4333 gold badges32 silver badges50 bronze badges1 Answer
Reset to default 7You could change the javascript call to this:
textBox.Attributes.Add("onblur", "javascriptMethod(this);");
Then you can access the textbox in your javascript method:
function javascriptMethod(textbox) { alert(textbox.id); }
That way you won't need to use the ClientID of the textbox control.
本文标签: cHow do I get the correct ClientID for my ASPNET TextBoxStack Overflow
版权声明:本文标题:c# - How do I get the correct ClientID for my ASP.NET TextBox? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742354277a2459105.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论