Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Radio button processing

Status
Not open for further replies.

eca2

MIS
Nov 4, 2017
3
0
0
US
I would like to add a Radio Button control within my asp.net Form. When the form is submitted, I would like the value of the Radio Control go to the "Code Behind page (Default.aspx.cs)". Using C#, I want the processing to occur there, and not within the page where the Form is (Default.aspx). Any suggestions? Bellow is a basic layout of the form with a working "Code behind page with existing Form controls"

Thanks in advance!

Default.aspx

<%@ Page
Language = "C#"
AutoEventWireup = "false"
Inherits = "RadioButton.Default"
ValidateRequest = "false"
EnableSessionState = "false"
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head>
<title>RadioButton</title>

<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="CACHE-CONTROL" content="NO-CACHE" />
<meta http-equiv="PRAGMA" content="NO-CACHE" />

</head>

<body>

<form id="Form1" method="post" runat="server">
<!-- Site Code goes here! -->
<table>
<tr>
<td valign="top">Name:</td>
<td>
<input id="_Input_Name" runat="server" />
</td>
</tr>

<tr>
<td valign="top">Options:</td>
<td>
<asp:RadioButtonList id="RadioButtonList1" runat="server">
<asp:ListItem Selected="True">Item1</asp:ListItem>
<asp:ListItem>Item2</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>

<tr>
<td colspan="2" valign="top">
<input id="_Button_Ok" type="submit" value="Ok" runat="server" />
</td>
</tr>

</table>

</form>
</body>
</html>
-----------------------------------------------------------
Default.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace RadioButton
{
public class Default : Page
{

//Form Control Constructors
protected HtmlInputButton _Button_Ok;
protected HtmlInputText _Input_Name;


//PageInit
protected void PageInit(object sender, EventArgs e)
{
}

//PageExit
protected void PageExit(object sender, EventArgs e)
{
}

//Page_Load
private void Page_Load(object sender, EventArgs e)
{
//Post Back
if(IsPostBack)
{
}
}

//Button Click Event
protected void Click_Button_Ok(object sender, EventArgs e)
{
Response.Write( _Button_Ok.Value + " was clicked!<br>");
}

//Input Change Event
protected void Changed_Input_Name(object sender, EventArgs e)
{
Response.Write( _Input_Name.Value + " has changed!<br>");
}

//region Initialize Component
protected override void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

//InitializeComponent Method
private void InitializeComponent()
{
this.Load += new System.EventHandler(Page_Load);
this.Init += new System.EventHandler(PageInit);
this.Unload += new System.EventHandler(PageExit);

_Button_Ok.ServerClick += new EventHandler(Click_Button_Ok);
_Input_Name.ServerChange += new EventHandler(Changed_Input_Name);
}

}
}
 
I am not understanding your question. Your radio button list is on Default.aspx. When you click the OK ,button the page posts back to itself. In the Page_Load or Button_Click event you can then get the value. So I am not understanding what you think will happen or what you want to happen
 
Ya, I know you can post back to the form page, no troubles there. My question was clearly stated, "Code Behind page". When I post the form, I wanted to display the value of the selected radio button. Anyway, I figured it out.

I added the following to the Button Click event in the "Code Behind Page", and got the result I wanted:

string stTest = Request.Form["RadioButtonList1"].ToString();
Response.Write(stTest);

Works to perfection
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top