最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论