Kolla
Kolla

Reputation: 75

How do you set a value in codebehind depending on which radio button is chosen?

I have a variable called "type" that is an int and it can be 1 or 2, this is used to create a dataset from a stored procedure. I want to set type depending on which radiobutton is chosen.

What I want to happen is when I choose one of the two radiobuttons "Alpha" or "Beta" I want it to set "type" to 1 or 2 when the button "Update" is clicked.

This is what I have on the asp site.

<asp:Button ID="update" Text="Update" runat="server" OnClick="update_Click" />
<asp:Label runat="server">Alpha</asp:Label>
<asp:RadioButton ID="vyAlpha" runat="server" GroupName="button" AutoPostBack="true"/>
<asp:Label runat="server">Beta</asp:Label>
<asp:RadioButton ID="vyBeta" runat="server" GroupName="button" AutoPostBack="true" />
<asp:TextBox ID="TextBox1" runat="server" Text="2012-12-12"></asp:TextBox>

And here is the codebehind

DateTime date = DateTime.ParseExact(TextBox1.Text, "yyyy-MM-dd", null);
TextBox1.Text = date.ToString("yyyy-MM-dd");
int name = -1;
int type = 1; // here is where I want to control it between being 1 or 2 depending on which radiobutton is chosen.

    DataSet ds = view.Time(date, name, type);

            return ds;

Upvotes: 0

Views: 967

Answers (1)

Christos
Christos

Reputation: 53958

Just write

int type = (vyAlpha.Checked) ? 1 : ((vyBeta.Checked) ? 2 : 0);

in your code behind class. The type will be setted to 0 if both checkboxA or checkboxB aren't checked.

(In case of not knowing the ? operator, please take a look here: http://msdn.microsoft.com/en-us/library/ty67wk28.aspx).

Upvotes: 1

Related Questions