【问题标题】:Radio Button not firing itemcommand event in repeater单选按钮未在转发器中触发 itemcommand 事件
【发布时间】:2013-02-05 00:50:28
【问题描述】:
我正在处理asp.netcustom control,我在其中使用repeater control 来显示radio buttons。
当RadioButton 被点击时,我需要触发中继器ItemCommand event。
我面临的问题是RadioButton 不能触发ItemCommend event 并且它没有CommendArgument 和CommandName 属性。
为了完成我创建了asp.net server control,从RadioButton 驱动我并在其中添加CommendArgument 和CommandName 属性。
我还在其中添加了一个Button,以便我可以以编程方式调用此button 的点击事件来触发转发器ItemCommand event。
现在我面临的问题是我已经触发了 Button's click 事件,但仍然没有触发 ItemCommand 事件。
知道如何完成这个想法吗?
【问题讨论】:
标签:
asp.net
gridview
radio-button
custom-controls
【解决方案1】:
当触发单选按钮的OnCheckedChanged 时,您可以调用转发器ItemCommand 事件。
我认为主要问题是您不确定如何创建ItemCommand 期望的参数,这是一个我相信会有所帮助的示例:
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>
背后的代码:
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();
}
}