ASP Radiobutton oncheckedchange没有开火

时间:2014-08-07 14:36:31

标签: c# asp.net

我在转发器内部有一个自定义单选按钮,当用户点击它时,它应该触发后面的代码,但这根本不会触发。我在方法的开头放置了一个断点,它永远不会到达。唯一确实发生的事情是updatepanel的回发

<EclipseUI:CustomRadioButton runat="server" ID="RadioButton_Select"
                                            ClientIDMode="AutoID" ToolTip='<%# "id_" + Eval("FeaturePackId") %>' GroupName='<%# "id_" + Eval("FeaturePackId") %>'
                                            OnCheckedChanged="RadioButton_Select_OnCheckedChanged" AutoPostBack="True"/>

背后的代码就是这样,它获取每个已检查的radiobutton的值并将其放在隐藏的字段中以供以后使用。

protected void RadioButton_Select_OnCheckedChanged(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        sb.Clear();

        foreach (RepeaterItem repeaterItem in Repeater_Select.Items)
        {
            CustomRadioButton radioButton = repeaterItem.FindControl("RadioButton_Select") as CustomRadioButton;
            if (radioButton != null)
            {
                if (radioButton.Checked)
                {
                    sb.Append(radioButton.GroupName.Substring(4));
                }
                else
                {
                    sb.Replace(radioButton.GroupName.Substring(4), "");
                }
            }
        }

        HF_Feature.Value = sb.ToString();
    }

我的问题是,这根本不会发生。

修改

我的代码现在如下,并且项目数据绑定事件被命中,因此将事件处理程序+ =赋值给radiobutton,但是单击radiobutton仍然没有在radiobutton方法的OnCheckedChange中命中我的断点:

protected void Page_Load(object sender, EventArgs e)
    {
        Repeater_Select.ItemDataBound += new RepeaterItemEventHandler(Repeater_Select_OnItemDataBound);
    }

protected void Repeater_Select_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            CustomRadioButton customRbtn = (CustomRadioButton)e.Item.FindControl("RadioButton_Select");
            customRbtn.CheckedChanged += new EventHandler(RadioButton_Select_OnCheckedChanged);
        }
    }

但它仍然无效

2 个答案:

答案 0 :(得分:1)

在转发器中,您必须连接转发器_ItemDataBound内的OnCheckedChanged事件。

 protected void Page_Init(object sender, EventArgs e
{
   myRepeater.ItemDataBound += new RepeaterItemEventHandler(myRepeater_ItemDataBound);
}

然后..

private void myRepeater_ItemDataBound(object sender, RepeaterCommandEventArgs e)
{
    if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType !=
       ListItemType.AlternatingItem)
    {

        CustomRadioButton customRbtn = (CustomRadioButton)e.Item.FindControl("RadioButton_Select");
        //Now you have an instance of your eclipse radio button so you can do what you want with it.

    }
}

对此有所了解:OnCheckedChanged event handler of asp:checkbox does not fire when checkbox is unchecked 在途中&#34; _ItemDataBound&#34;有线。

如果您在.aspx文件中使用项目模板,本文可能会更接近您的问题。 http://www.codeguru.com/csharp/.net/net_asp/tutorials/article.php/c12065/ASPNET-Tip-Use-the-ItemDataBound-Event-of-a-Repeater.htm

答案 1 :(得分:0)

只需尝试设置 CauseValidation =“ false” (除了Autopostback =“ true”)

<EclipseUI:CustomRadioButton runat="server" ID="RadioButton_Select" ClientIDMode="AutoID" ToolTip='<%# "id_" + Eval("FeaturePackId") %>' GroupName='<%# "id_" + Eval("FeaturePackId") %>' OnCheckedChanged="RadioButton_Select_OnCheckedChanged" AutoPostBack="True" CauseValidation="false"/>

有效...