Radio Button not firing itemcommand event in repeater(单选按钮未在转发器中触发 itemcommand 事件)
问题描述
我正在开发 asp.net
custom control
,我在其中使用 repeater control
来显示 单选按钮代码>.
当点击 RadioButton
时,我需要触发中继器 ItemCommand 事件
.
我面临的问题是 RadioButton
不能触发 ItemCommend 事件
并且它没有 CommendArgument
和 CommandName
属性.
为了完成我创建了 asp.net 服务器控件
,从 RadioButton
驱动 i 并添加 CommendArgument
和 CommandName
属性它.
我还在其中添加了一个Button
,以便我可以通过编程方式调用此button
的点击事件来触发中继器ItemCommand 事件
.
现在我面临的问题是我已经触发了 Button's click
事件,但仍然没有触发 ItemCommand
事件.
知道如何完成这个想法吗?
当单选按钮的 OnCheckedChanged
被触发时,您可以调用中继器 ItemCommand
事件.p>
我认为主要问题是您不确定如何创建 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();
}
}
这篇关于单选按钮未在转发器中触发 itemcommand 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:单选按钮未在转发器中触发 itemcommand 事件
- 如何用自己压缩一个 IEnumerable 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- C# 中多线程网络服务器的模式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01