外部js中的getelementbyid不适用于asp.net-customcontrol
有一个带有按钮的自定义控件。有一个外部 javascript 文件尝试将一些事件附加到我的控件。就像 onclick 事件在运行时添加到按钮上一样。为此,我使用
var save = document.getElementById("btnExecute");
从附加了 JS 文件的 aspx 页面调用此控件。该页面无法工作,我读到的文章说,如果我将代码更改为类似的内容,它就会工作
var save = document.getElementById("<%= btnExecute.ClientID %>");
,但只有当 JS 位于 aspx 文件内时它才工作。如何使其适用于自定义控件。我需要遵循哪些步骤才能使外部 JS 上的 ID 正常工作?
尝试了Ajax81的解决方案: 添加代码
namespace MyCompositeControl
{
public class MyGrid : CompositeControl
{
public string ButtonClientID
{
get { return btnExecute.ClientID; }
set { }
}
protected override void CreateChildControls()
{
#region Execute Button
btnExecute.ID = "btnExecute";
btnExecute.Text = "Execute";
btnExecute.Click += new EventHandler(_button_Click);
Controls.Add(btnExecute);
#endregion
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
string jsResourceName = "MyCompositeControl.Scripts.ControlScriptLibrary.js";
ClientScriptManager cs = this.Page.ClientScript;
cs.RegisterClientScriptResource(typeof(this,GetType()), jsResourceName);
}
}
该按钮是动态添加的,如上面的代码所示。
ControlScriptLibrary.js
function attachEvents()
{
//var save = document.getElementById("btnExecute");
var save = document.getElementById("<%=MyGrid.ButtonClientID%>");
if(save!=null)
{
save.onclick = onSaveAction;
}
}
编辑:仍然返回 null。难道,JS先跑了,控件还没有到吗?但是,如果我像这样传递控件 id,则此方法有效:
function MyController(executeButtonID, onSaveEventHandler)
{
function attachEvents()
{
//var save = document.getElementById("btnExecute");
var save = document.getElementById(executeButtonID);
if(save!=null)
{
save.onclick = onSaveAction;
}
}
}
在我的控件的 RenderContent 方法中,我有以下 javascript 代码:
var myController = new MyController(this.btnExecute.ClientID, onSaveEventHandler);
如果我在控件上有更多按钮并且需要在 JS 中进行一些事件处理,则此方法将会终止。 Ajax81 的解决方案很好,但它对我来说还不起作用。
have a custom control with a button. There is an external javascript file that tries to attach some events to my control. Like onclick event is added at runtime to the button. To do so I use
var save = document.getElementById("btnExecute");
This control is being called from aspx page where I have attached the JS file. The page won't work, I read articles that it will work if I change the code to something like
var save = document.getElementById("<%= btnExecute.ClientID %>");
but it only works if the JS is within the aspx file. How to make this work for a custom control. What all steps do I need to follow to get the ID on external JS to work?
Tried Ajax81's solution: Code added
namespace MyCompositeControl
{
public class MyGrid : CompositeControl
{
public string ButtonClientID
{
get { return btnExecute.ClientID; }
set { }
}
protected override void CreateChildControls()
{
#region Execute Button
btnExecute.ID = "btnExecute";
btnExecute.Text = "Execute";
btnExecute.Click += new EventHandler(_button_Click);
Controls.Add(btnExecute);
#endregion
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
string jsResourceName = "MyCompositeControl.Scripts.ControlScriptLibrary.js";
ClientScriptManager cs = this.Page.ClientScript;
cs.RegisterClientScriptResource(typeof(this,GetType()), jsResourceName);
}
}
The button is added dynamically as you can see in above code.
ControlScriptLibrary.js
function attachEvents()
{
//var save = document.getElementById("btnExecute");
var save = document.getElementById("<%=MyGrid.ButtonClientID%>");
if(save!=null)
{
save.onclick = onSaveAction;
}
}
Edited: Still it returns null. Can it be that, the JS runs first and the control is not yet there? But this works if I pass the control id like this:
function MyController(executeButtonID, onSaveEventHandler)
{
function attachEvents()
{
//var save = document.getElementById("btnExecute");
var save = document.getElementById(executeButtonID);
if(save!=null)
{
save.onclick = onSaveAction;
}
}
}
In my control's RenderContent method I have the following javascript code:
var myController = new MyController(this.btnExecute.ClientID, onSaveEventHandler);
This approach will kill if I have more buttons on the control and need some event handling in JS. Ajax81's solution is nice but it is not working for me yet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 .NET 中,当控件嵌套在表单中时,控件的名称也采用其父控件的名称。例如,在输出 HTML 时,名称为“ComboBox1”的对象可能最终会变成“ctl00_content_ctl00_ComboBox1”。如果您需要在浏览器运行时通过 id 引用按钮,那么您可以检查生成的 HTML 代码并查看按钮的最终名称是什么,然后在 JS 中使用它。但请注意,如果任何按钮父对象的名称发生更改,则按钮的名称也可能会更改。因此,这不是一个很好的解决方案,特别是当页面上可能有许多此类控件时。
有一些 JavaScript 框架(例如 jQuery 和 Prototype)提供了一些工具,可以让您更轻松地在 DOM 中查找对象并附加事件。
In .NET, when controls are nested in in a form, the name of the controls take on the names of their parents too. For example, an object with the name "ComboBox1" may end up like "ctl00_content_ctl00_ComboBox1" by the time the HTML is output. If you need to refer to the button by its id at browser run-time, then you can inspect the generated HTML code and see what the final name of the button is, then use that in your JS. Be aware though that if the names of any of the buttons parent objects change, then the name of the button may change. As such, this is not a great solution, especially when you may have many of these controls on a page.
There are some JavaScript frameworks such as jQuery and Prototype that provide tools to allow you to more easily find objects in the DOM and attach events.
为自定义控件创建一个属性,该属性返回您需要在 JavaScript 中处理的按钮的客户端 ID。
您的代码最终看起来像这样:
在您的自定义控件的代码隐藏中:
在您的 JavaScript
EDIT 中
修复了 JavaScript 中的拼写错误。
Create a property for the custom control that returns the client id of the button you need to address in your javascript.
Your code would end up looking something like this:
Inside your Custom Control's Code Behind:
In your JavaScript
EDIT
Fixed typo in javascript.