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?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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();
}
}