Thursday, March 22, 2012

Trouble with Radiobuttons

Hi,

I am working on this project of creating webforms and this one particular page has a bunch of radiobuttons in it. Once the user clicks one of the radiobuttons and then clicks on submit, the user is redirected to a different webform. In the event handler for the submit button, I check to see if that one particular radiobutton has been clicked or not ,kinda like this below:

privatevoid InitializeComponent()

{

this.submit_button.Click +=new System.EventHandler(this.submit_button_Click);

}

privatevoid submit_button_Click(object sender, System.EventArgs e)

{

if (radiobutton1.Checked ==true)

{

Response.Redirect("page1.aspx");

}

The problem is when I check radiobutton1 and then click submit, nothing happens and I stay on the same page. I ran the debugger and it didn't even step through the code. Can anyone help me out here?

Try this - uses both types of radio button/list

<%@.PageLanguage="C#" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<scriptrunat="server">

protectedvoid RadioButton3_CheckedChanged(object sender,EventArgs e)

{

Response.Redirect("Default.aspx?Fruit=" + Server.HtmlEncode(((RadioButton)sender).Text));

}

protectedvoid RadioButtonList1_SelectedIndexChanged(object sender,EventArgs e)

{

Response.Redirect("Default.aspx?Fruit=" + Server.HtmlEncode(RadioButtonList1.SelectedValue));

}

</script>

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

<title>Untitled Page</title>

</head>

<body>

<formid="form1"runat="server">

<div>

<asp:RadioButtonID="RadioButton1"runat="server"GroupName="RB1"Text="Apples"OnCheckedChanged="RadioButton3_CheckedChanged"/>

<asp:RadioButtonID="RadioButton2"runat="server"GroupName="RB1"Text="Oranges"

OnCheckedChanged="RadioButton3_CheckedChanged"/>

<asp:RadioButtonID="RadioButton3"runat="server"GroupName="RB1"Text="Gone Bananas"

OnCheckedChanged="RadioButton3_CheckedChanged"/>

</div>

<div>

<asp:RadioButtonListID="RadioButtonList1"runat="server"OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">

<asp:ListItem>Pears</asp:ListItem>

<asp:ListItem>Cherries</asp:ListItem>

<asp:ListItem>WinkleBerry</asp:ListItem>

</asp:RadioButtonList>

</div>

<asp:LinkButtonID="LinkButton1"runat="server">Submit</asp:LinkButton>

</form>

</body>

</html>


Thanks for the response...I am working with .aspx files...So instead of embedding scripts like you suggested, I have to write backend code in the .aspx.cs files..Its just that after I click on the submit button it should hit that part of the code where I have the event handler in case the submit button is clicked. I still can't figure out why its not doing that...I am working on a visual studio 2003 platform

0 comments:

Post a Comment