c# - How do I get the correct ClientID for my ASP.NET TextBox? - Stack Overflow

I have created a a subclass of Table in my ASP.NET project which creates. The table uses a class that f

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 badges
Add a ment  | 

1 Answer 1

Reset to default 7

You 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742354277a4428128.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信