WTFZane
WTFZane

Reputation: 612

asp.net - auto checked the radiobutton if the same database

I want to have my radiobutton to be automatically be checked if the value in my database matches his. here is like this.

<asp:RadioButton ID="serviceable" GroupName="inputStatus" runat="server" Text="Serviceable" />
<asp:RadioButton ID="unserviceable" GroupName="inputStatus" runat="server" Text="Unserviceable" />
<asp:RadioButton ID="fordisposal" GroupName="inputStatus" runat="server" Text="For disposal" />

In my back code I am calling it like this.

   status = rdr["asset_status"].ToString();

then im thinking about doing an if or maybe javascript but I am not fond of using Javascript.. Sooo how can I use the variable status to define whether what radiobutton will be checked? Thank you

Upvotes: 0

Views: 556

Answers (2)

user7450900
user7450900

Reputation:

According to your code imagine you want to check your radiobutton with id serviceable in some conditions of status value.

 if(status=="somevalue")
 {
    serviceable.Checked = true;
 }

If your RadioButton ID exactly matchs your status you can check your radio according to status text without even using if statement.

 (this.FindControl(status.ToLower()) as RadioButton).Checked = true;

But if your radiobutton text exactly matchs your status I suggest you to use RadioButtonList and then you can use the following code snippet to select radiobuttons those text matchs exactly status.

RadioButtonList1.SelectedIndex = RadioButtonList1.Items.IndexOf(RadioButtonList1.Items.FindByText(status)); 

Upvotes: 0

Usman
Usman

Reputation: 4693

you can use switch statement which are fast in performance than condition

string check = "For disposal";
 switch (check)
         {
                        case "Serviceable":
                            serviceable.Checked = true;
                            break; /* optional */
                        case "Unserviceable":
                            unserviceable.Checked = true;
                            break; /* optional */
                        case "For disposal":
                            fordisposal.Checked = true;
                            break; /* optional */


           }

it will check the case if it matches it will set that radio button to true and it will work on both webforms and winforms

Upvotes: 1

Related Questions