1

I wonder if it is possible to add or attach or extend the asp.net checkbox in a way to add OnCommand , CommandName, CommandArgument functionality.

In brief I want to add to a asp. net CheckBox the OnCommand functionality like Button or LinkButton has.

I want to place a check box in a Repeater ItemTemplate and want to handle on checkbox clicks in OnItemCommand handler.

Patrik
  • 1,286
  • 1
  • 31
  • 64

3 Answers3

1

I solved my problem using a simple trick. I created a web user control using a checkbox and invisible link button.

<asp:CheckBox ID="cbSelector" runat="server" AutoPostBack="true" />
<asp:LinkButton ID="btnHiddenCheckBox" runat="server" Visible="false" CommandName="Select"></asp:LinkButton>

in the code behind i added some event handlers ...

  protected void Page_Load(object sender, EventArgs e)
  {
     cbSelector.CheckedChanged += new EventHandler(cbSelector_CheckedChanged);
  }

  void cbSelector_CheckedChanged(object sender, EventArgs e)
  {
      btnHiddenCheckBox.CommandName = "Select";
      btnHiddenCheckBox.CommandArgument = Convert.ToString(cbSelector.Checked);
      ((IPostBackEventHandler)btnHiddenCheckBox).RaisePostBackEvent(null);
  }

and programmatically raised a Click event with parameters and it works.

Patrik
  • 1,286
  • 1
  • 31
  • 64
0

The same could be achieved using OnCheckChanged event.

You can find out what the RepeaterItem is to be able to access controls in the same RepeaterItem


ALternatively, see this SO question for a solution on how to use OnItemCommand for CheckBox:

Checkbox OnClick/ItemCommand in Repeater or DataList

Community
  • 1
  • 1
Curtis
  • 101,612
  • 66
  • 270
  • 352
  • Yes. I know the OnCheckChanged event but I want to place all in the OnItemCommand event handler of the Repeater. – Patrik Oct 18 '11 at 10:28
  • Updated my answer with a link to an existing similar question. You may find this useful – Curtis Oct 18 '11 at 10:32
0

Patrik I am going to suggest you something completely different, use JQuery and get this done via Ajax so to give the users way smoother experience.

there is a nice article with a step by step here: ASP.Net CheckBox and JQuery

everything is better than a CheckBox or a Button with AutoPostBack set to true nowadays in year 2011, 2012 soon ;-)

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • Thank you. Your solution is OK but in my situation I will stick to the server side. I must do something in the OnItemCommand of the repeater that is complex. – Patrik Oct 18 '11 at 10:26