简体   繁体   中英

Find HTML radio button inside repeater

In a repeater I have following button:

  <input type="radio" name="mainSelection" id="rbtn" />

I want to check on of ratio button as True. So I am doing this onItemDataBound Event

How Can I find this button on the OnItemDataBount Event. RepAddress_OnItemDataBound

This is my code:

protected void RepAddress_OnItemDataBound(object sender, RepeaterItemEventArgs e)
 {
  if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
   {
    // I cannot use ---> RadioButton rb = (RadioButton)e.Item.FindControl("rbtn");  
   // becuase its not an asp Radio Button.

  //I cannot use--> HtmlGenericControl rb = e.Item.FindControl("rbtn") as HtmlGenericControl;

   if (//some condition)
   {
    rb.Checked = true; // That's the reason I need to find the button.
   }
   else
   {
    rb.Checked = false;
    }
  }
}

You cannot do that with pure HTML controls. It's only capable of finding server controls because they are actually present in the controls collection.

If you want you can convert your HTML radio button to server control using runat attribute like this:-

<input type="radio" runat="server" name="mainSelection" id="rbtn" />

After this you can find your control in ItemDataBound like this:-

HtmlInputRadioButton rb = e.Item.FindControl("rbtn") as HtmlInputRadioButton;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM