问题描述
下面是我的code为动态的DropDownList。它正确生成HTML。但是,不触发事件。而且,当我改变事件名称为的onchange它给了我一个编译错误说,它无法找到脚本。通过它的存在在我的code-后面。
另外,我在OnInit页面事件添加此。
pValueCmbBox.Attributes.Add(RUNAT,服务器);
pValueCmbBox.SelectedIndexChanged + =新的EventHandler(ddlParent_SelectedIndexChanged);
pValueCmbBox.Attributes.Add(OnSelectedIndexChanged,ddlParent_SelectedIndexChanged);
pValueCmbBox.Attributes.Add(的AutoPostBack,真);
- 为什么不OnSelectedIndexChanged被解雇?
- 是的onchange仅用于调用JavaScript的?
- 如果我实现这个作为一个ASCX呢?
1)它不工作,因为你没有添加属性的AutoPostBack你应该的样子。
pValueCmbBox.Attributes.Add(RUNAT,服务器); //没有意义......它只是为了装饰......因为你不能在页面中使用的背后code
pValueCmbBox.SelectedIndexChanged + =新的EventHandler(ddlParent_SelectedIndexChanged); //这一行也没关系
pValueCmbBox.Attributes.Add(OnSelectedIndexChanged,ddlParent_SelectedIndexChanged); //这个是没有必要的......因为你已经通过pValueCmbBox.SelectedIndexChanged规定
pValueCmbBox.Attributes.Add(的AutoPostBack,真); //这就是问题
正如你在这里,的SelectedIndexChanged 时发生从岗位之间的列表控件更改服务器的选择。 。所以,你不得不对的AutoPostBack =真个好主意;你应该写:
pValueCmbBox.AutoPostBack = TRUE;
和现在的 =服务器
问题,您可以设置的功能背后的页面如下:
保护无效ddlParent_SelectedIndexChanged(对象发件人,EventArgs的发送)
{
DropDownList的C =(DropDownList的)寄件人; //这是你pValueCmbBox您设置它的OnInit
//更多code在这里
}
2)的onchange是Javascript,但是对于C#/ VB可以使用 OnTextChanged 结果
3)您可以做到这一点,因为你已经尝试过......或者我的方式告诉你。 :)
Below is my code for a dynamic dropdownlist. It does generate the HTML properly. However, the event is not fired. And, when I change the event name to "onchange" it gives me a compile error saying that it couldn't find the script. By it's there in my code-behind.
Also, I'm adding this in the OnInit page event.
pValueCmbBox.Attributes.Add("runat", "server");
pValueCmbBox.SelectedIndexChanged += new EventHandler(ddlParent_SelectedIndexChanged);
pValueCmbBox.Attributes.Add("OnSelectedIndexChanged", "ddlParent_SelectedIndexChanged");
pValueCmbBox.Attributes.Add("AutoPostBack", "True");
- Why isn't OnSelectedIndexChanged being fired?
- Is "onchange" only for calling javascript?
- Should I implement this as an ASCX instead?
1) It doesn't work because you're not adding the attribute "AutoPostBack" the way you should.
pValueCmbBox.Attributes.Add("runat", "server"); //doesn't make sense...it's just for decoration...because you can't use in page behind code
pValueCmbBox.SelectedIndexChanged += new EventHandler(ddlParent_SelectedIndexChanged); //this line it's okay
pValueCmbBox.Attributes.Add("OnSelectedIndexChanged", "ddlParent_SelectedIndexChanged"); //this it's not necessary at all...because you already specified through pValueCmbBox.SelectedIndexChanged
pValueCmbBox.Attributes.Add("AutoPostBack", "True"); //this is the problem
As you can see in here, SelectedIndexChanged "Occurs when the selection from the list control changes between posts to the server." . So you had a good idea regarding AutoPostBack = true; You should have written:
pValueCmbBox.AutoPostBack = true;
And now for the runat="server"
problem you can set your page behind function as following:
protected void ddlParent_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList c = (DropDownList)sender; //this is your pValueCmbBox that you set it in OnInit
//more code here
}
2) onchange is for javascript, but for c#/vb you can use OnTextChanged
3) You can do that, as you already tried...or the way i told you. :)
这篇关于动态创建的DropDownList没有触发事件。是的,是的AutoPostBack设置为TRUE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!