mogorilla
mogorilla

Reputation: 305

Making item selected in RadioButtonList

I have a RadioButtonList which is populated with 2 fields, Male and Female. They are populated using TableAdapters, this is my code:

<asp:RadioButtonList ID="gender_radioBtn" runat="server" RepeatDirection="Horizontal" style="display:inline" Width="288px" DataSourceID="genderObject" DataTextField="gender" DataValueField="id">
        </asp:RadioButtonList>
<asp:ObjectDataSource ID="genderObject" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="getGenders" TypeName="registerTableAdapters.genderTableAdapter"></asp:ObjectDataSource>

I want the Male which has a Value of 1 to be selected without having to do it programatically on PageLoad.

Does any know what should be done to achieve Male being selected within the RadioButtonList when the page is loaded without programitcally doing so in the PageLoad event?

Upvotes: 0

Views: 53

Answers (1)

user2366842
user2366842

Reputation: 1216

This can actually be done within the page load function, as was discussed in the comments. You will want to check the IsPostBack property of the page to determine if the page is being loaded for the first time, or if it's performing a postback.

Example code slightly modified from MSDN:

  private void Page_Load()
  {
     if (!IsPostBack)
     {
          //Put your code to check the Male Radio Button here.
     }
  }

Related MSDN link: https://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback(v=vs.110).aspx

Upvotes: 1

Related Questions