设置 'onClick'在代码隐藏中动态添加控件时调用客户端方法 [英] Set the 'onClick' to call a client method when dynamically adding control in Code Behind
问题描述
我正在代码隐藏中动态添加一个 RadioButtonList.我想要它,以便OnClick"不调用 JavaScript,而是调用我后面代码中的方法.
I am dynamically adding a RadioButtonList in the Code Behind. I want it so that the 'OnClick' does not call JavaScript, but instead it calls a method in my code behind.
这可能吗?
另外,有没有办法设置它,让这个say control有runat="server"?
In addition, is there a way to set it up so that this say control has runat="server"?
推荐答案
是的,这是可能的.此外,由于您是在代码中创建控件,因此不需要 runat="server".
Yes, it is possible. Also, there is no need to have runat="server" since your are creating the control in code.
您需要设置 RadioButtonList 对象的 OnSelectedIndexChanged 事件.正如@Robert 提到的,如果您要动态创建控件,则需要将它们包装在 Page_Init() 中.
You need to set your RadioButtonList object's OnSelectedIndexChanged event. As @Robert mentioned, if you are creating controls dynamically you need to wrap them in the Page_Init().
protected void Page_Init(Object sender, EventArgs e) {
RadioButtonList radiobuttonlist = new RadioButtonList();
radiobuttonlist.SelectedIndexChanged += radiobuttonList_CheckedChanged;
//Set the AutoPostBack to true proptery to that the user action
// will immediately post-back to the server
radiobuttonlist.AutoPostBack = true;
}
private void radiobuttonList_CheckedChanged(object sender, EventArgs e) {
//Code you want to execut when a user selects a different item
}
这篇关于设置 'onClick'在代码隐藏中动态添加控件时调用客户端方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!