单选按钮未在转发器中触发 itemcommand 事件单选、按钮、器中、事件

2023-09-06 22:02:24 作者:丨丶主打歌灬

我正在开发 asp.net custom control,我在其中使用 repeater control 来显示 单选按钮.当点击 RadioButton 时,我需要触发中继器 ItemCommand 事件.我面临的问题是 RadioButton 不能触发 ItemCommend 事件 并且它没有 CommendArgumentCommandName属性.为了完成我创建了 asp.net 服务器控件,从 RadioButton 驱动 i 并添加 CommendArgumentCommandName 属性它.我还在其中添加了一个Button,以便我可以通过编程方式调用此button 的点击事件来触发中继器ItemCommand 事件.现在我面临的问题是我已经触发了 Button's click 事件,但仍然没有触发 ItemCommand 事件.知道如何完成这个想法吗?

解决方案

当单选按钮的 OnCheckedChanged 被触发时,您可以调用中继器 ItemCommand 事件.p>vb中option单选按钮如何能选中多个

我认为主要问题是您不确定如何创建 ItemCommand 预期的参数,这是一个我相信会有所帮助的示例:

ASPX:

<asp:Repeater ID="rptColors" runat="server" onitemcommand="rptColors_ItemCommand"><项目模板><asp:RadioButton ID="rdbColour" Text='<%# Eval("Color") %>'AutoPostBack="true" runat="server" OnCheckedChanged="Checked"/><br/></项目模板></asp:中继器>

背后的代码:

公共类颜色{公共字符串颜色 { 获取;放;}}公共部分类默认值:System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){如果(!Page.IsPostBack){rptColors.DataSource = 新列表<颜色>{新颜色{颜色=红色"},新颜色{颜色=黑色"}};rptColors.DataBind();}}受保护的无效检查(对象发送者,EventArgs e){foreach(rptColors.Items 中的RepeaterItem 项){RadioButton rdbColour = item.FindControl("rdbColour") as RadioButton;if (rdbColour.Text.Equals((sender as RadioButton).Text)){CommandEventArgs commandArgs = new CommandEventArgs("SomeCommand", rdbColour.Text);RepeaterCommandEventArgs repeaterArgs = new RepeaterCommandEventArgs(item, rdbColour, commandArgs);rptColors_ItemCommand(rdbColour, repeaterArgs);}}}protected void rptColors_ItemCommand(对象源,RepeaterCommandEventArgs e){//当您选择中继器中的单选按钮时运行System.Diagnostics.Debugger.Break();}}

I am working on the asp.net custom control in which I am using repeater control to show radio buttons. I need to fire repeaters ItemCommand event when RadioButton is click. The problem I faced is RadioButton is not capabel of firing ItemCommend event and it does not have CommendArgument, and CommandName properties. To accomplish I created asp.net server control, drived i from RadioButton and add CommendArgument, and CommandName properties in it. I also added a Button in it so that I can call the click event of this button programatically to fire repeaters ItemCommand event. Now the problem I am facing is I have fired Button's click event but still ItemCommand event is not firing. Any idea how to gat this thind done?

解决方案

You can call the repeaters ItemCommand event when the OnCheckedChanged of the radio button is fired.

I think the main problem is you're not sure how to create the arguments expected by ItemCommand, here's an example which I believe will help:

ASPX:

<asp:Repeater ID="rptColors" runat="server" onitemcommand="rptColors_ItemCommand">
    <ItemTemplate>
        <asp:RadioButton ID="rdbColour" Text='<%# Eval("Color") %>' AutoPostBack="true" runat="server" OnCheckedChanged="Checked" /> <br />
    </ItemTemplate>
</asp:Repeater>

Code behind:

public class Colours
{
    public string Color { get; set; }
}

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            rptColors.DataSource = new List<Colours> { new Colours { Color = "Red" }, new Colours { Color = "Black" } };
            rptColors.DataBind();
        }
    }

    protected void Checked(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in rptColors.Items)
        {
            RadioButton rdbColour = item.FindControl("rdbColour") as RadioButton;
            if (rdbColour.Text.Equals((sender as RadioButton).Text))
            {
                CommandEventArgs commandArgs = new CommandEventArgs("SomeCommand", rdbColour.Text);
                RepeaterCommandEventArgs repeaterArgs = new RepeaterCommandEventArgs(item, rdbColour, commandArgs);
                rptColors_ItemCommand(rdbColour, repeaterArgs);
            }
        }
    }

    protected void rptColors_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        //Runs when you select the radio button in the repeater
        System.Diagnostics.Debugger.Break();
    }
}