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.
我在我的ASP中创建了一个表的子类。净项目创建。该表使用一个类来格式化和创建制表符和制表符,我们可以称之为RowCreator。MyTable调用rowCreator.CreateRow()并返回一个包含很多好处的制表符。
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.
在这个列表框中,有一个文本框,它应该在onblur事件中触发一个javascript方法,这个事件是由RowCreator类添加的。
textBox.Attributes.Add("onblur", "javascriptMethod('" + textbox.ClientID + "');");
textBox.Attributes。Add(“onblur”、“javascriptMethod(“+文本框。ClientID +”);”);
I've also tried created a subclass of textBox which implements a method that adds the onblur event:
我还尝试过创建一个textBox的子类,它实现了添加onblur事件的方法:
Attributes.Add("onblur", "javascriptMethod('" + this + "');")
属性。添加(“onblur”、“javascriptMethod(“+ +”);”)
Which doesn't work. The ID is just the namespace of the textbox subclass.
这是行不通的。ID只是文本框子类的名称空间。
And the JavaScript method is very simple:
JavaScript方法非常简单:
function javascriptMethod(boxId) { alert(boxId); }
函数javascriptMethod(boxId){警报(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?
麻烦的是,我猜这是因为文本框控件尚未添加到控制集合,这种boxId不是适当的客户机ID。它是一样的服务器端ID。有适当的方法ID,而无需使用控件添加行。首先在页面上添加?还有其他的建议吗?
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?
我这样做的原因是,每当修改文本框内容时,都要从javascript函数中读取它的内容。也许有更好的办法?
1 个解决方案
#1
7
You could change the javascript call to this:
可以将javascript调用改为:
textBox.Attributes.Add("onblur", "javascriptMethod(this);");
Then you can access the textbox in your javascript method:
然后你可以用javascript方法访问文本框:
function javascriptMethod(textbox) { alert(textbox.id); }
That way you won't need to use the ClientID of the textbox control.
这样就不需要使用文本框控件的ClientID。
#1
7
You could change the javascript call to this:
可以将javascript调用改为:
textBox.Attributes.Add("onblur", "javascriptMethod(this);");
Then you can access the textbox in your javascript method:
然后你可以用javascript方法访问文本框:
function javascriptMethod(textbox) { alert(textbox.id); }
That way you won't need to use the ClientID of the textbox control.
这样就不需要使用文本框控件的ClientID。