单选按钮不能触发中继器中的itemcommand事件 [英] Radio Button not firing itemcommand event in repeater

查看:133
本文介绍了单选按钮不能触发中继器中的itemcommand事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 asp.net 自定义控件,其中我使用 >单选按钮。。
我需要消防中继< ItemCommand事件
when RadioButton 是点击。

我遇到的问题是 RadioButton 不是触发 ItemCommend事件的函数,它没有 CommendArgument CommandName 属性。


为了完成我创建的 asp.net服务器控制 code> RadioButton ,并在其中添加 CommendArgument CommandName 属性。

我还在其中添加了一个 Button ,以便我可以调用此按钮的单击事件以编程方式向消息中继器发送消息 ItemCommand事件
现在我遇到的问题是我已经触发了 Button的单击 code> event但仍然 ItemCommand 事件不会触发。


任何想法如何完成这个任务? 当<$ c>

我认为主要问题是你不确定如何创建期望的参数通过 ItemCommand ,这里有一个我认为会有帮助的例子:

ASPX:

 < asp:Repeater ID =rptColorsrunat =serveronitemcommand =rptColors_ItemCommand> 
< ItemTemplate>
< asp:RadioButton ID =rdbColourText ='<%#Eval(Color)%>'AutoPostBack =truerunat =serverOnCheckedChanged =Checked/> < br />
< / ItemTemplate>
< / asp:中继器>

背后的代码:

  public class Colors 
{
public string Color {get;组; }

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


$ b $保护无效检查(object sender,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);



$ b protected void rptColors_ItemCommand(object source,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();
    }
}

这篇关于单选按钮不能触发中继器中的itemcommand事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆