I have a form with radio buttons in it and a submit button which reload page only and not submit form data...what should i do in form action or method so that i can get these radio button values
<form id="form2" name="form2" method="post" action="" runat="server">
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>
Your code doesn't seem to have any radiobutton controls...
Samples showing different options:
HTML
<form id="form1" runat="server">
<div>
<p>
Name:
<asp:TextBox ID="textbox1" runat="server" />
<br />
Coffee preference:
<asp:RadioButtonList ID="CoffeeSelections" runat="server" RepeatLayout="Flow" RepeatDirection="Horizontal">
<asp:ListItem>Latte</asp:ListItem>
<asp:ListItem>Americano</asp:ListItem>
<asp:ListItem>Capuccino</asp:ListItem>
<asp:ListItem>Espresso</asp:ListItem>
</asp:RadioButtonList>
<br />
</p>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
<div>
<h1>
Results</h1>
<p>
<asp:Label runat="server" ID="labelResults"></asp:Label></p>
</div>
</form>
Code:
//You can inspect on Page_Load
protected void Page_Load(object sender, EventArgs e)
{
//Only inspect on submission
if (Page.IsPostBack)
{
if (!string.IsNullOrEmpty(CoffeeSelections.SelectedValue))
{
labelResults.Text = CoffeeSelections.SelectedValue + "<br />";
}
//you can also inspect request.form colletion
foreach (string item in Request.Form)
{
labelResults.Text += "<b>KEY</b> : " + item + " <b>VALUE</b> = " + Request.Form[item] + "<br />";
}
}
}
//You can also inspect on raised control events (e.g. button)
protected void Button1_Click(object sender, EventArgs e)
{
string _foo = CoffeeSelections.SelectedValue;
labelResults.Text += _foo;
}
ASP.NET Web Form tries to give you the illusion that you are working with something like a Windows Form application. If you come from an Html background, you need some aclimatization.
Though the ASP.NET "FORM" spells with the same four letters, it is different from the standard Html <FORM>. By default, the Button1 is not the Submit button in a standard Html <FORM>. You can have multiple Button controls in an ASP.NET FORM. Each Button control has to be tied to an onClick event handler, a la Windows Form, and you put your code in this event handler.
Double click Button1 in the ASPX Design tab and a Button1_Click event will be generated for you in the code behind page. Put your code in this event handler.
ASP.NET then generates some Html output to make your Button1 an <INPUT TYPE='SUBMIT'> Html control with an onClick script (not to be confused with your Button1_Click event handler) to post back to the server with the form parameters that includes a gigantic one called __VIEWSTATE. The server then uses __VIEWSTATE to determine that you are in the Button1_Click event handler and proceeds to execute the code there.
The same goes for the radio buttons. ASP.NET will generate <INPUT TYPE='RADIO'> from your radio buttons, but you need not have (or not supposed) to concern yourself with this. In your Button1_Click event handler, you can read off the properties of your ASP.NET Radio Button controls.
With ASP.NET, basically you are supposed to not know and give up control of the Html output that is generated. You are supposed to work with the objects as visible on the server (code behind) only. <asp:Button ID='Button1' runat='server'> does not even generate into <input type='submit' name='Button1' id='Button1'> except for the simplest ASP.NET page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With