Radio Buttons
I have given radioB1 as male and rb2 as female.If i execute i could able to select any one .like pls give me codes to select any one.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Srinivas PabballaPosted Aug 27, 2015, 8:00 AM
Rajeesh MenothPosted Aug 27, 2015, 7:44 AM
Upendra Pratap ShahiPosted Aug 27, 2015, 7:27 AM
Design Page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="ReadTextFile_Test" %>
DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>title>
head>
<body>
<form id="form1" runat="server">
<div>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
Name:
td>
<td>
<asp:TextBox ID="txtName" runat="server" />
td>
tr>
<tr>
<td>
Gender:
td>
<td>
<asp:RadioButton ID="rbMale" Text="Male" runat="server" GroupName="Gender" />
<asp:RadioButton ID="rbFemale" Text="Female" runat="server" GroupName="Gender" />
td>
tr>
<tr>
<td>
td>
<td>
<asp:Button ID="Button1" Text="Submit" runat="server" OnClick="Submit" />
td>
tr>
table>
div>
form>
body>
html>
Code behind-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
public partial class ReadTextFile_Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Submit(object sender, EventArgs e)
{
string name = txtName.Text.Trim();
string gender = string.Empty;
if (rbMale.Checked)
{
gender = "M";
}
else if (rbFemale.Checked)
{
gender = "F";
}
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Persons(Name, Gender) VALUES(@Name, @Gender)"))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Gender", gender);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
Kindly accept the answer.viswa aPosted Aug 27, 2015, 7:20 AM
Rajeesh MenothPosted Aug 27, 2015, 5:36 AM
Upendra Pratap ShahiPosted Aug 27, 2015, 4:45 AM
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
Name:
td>
<td>
<asp:TextBox ID="txtName" runat="server" />
td>
tr>
<tr>
<td>
Gender:
td>
<td>
<asp:RadioButton ID="rbMale" Text="Male" runat="server" GroupName="Gender" />
<asp:RadioButton ID="rbFemale" Text="Female" runat="server" GroupName="Gender" />
td>
tr>
<tr>
<td>
td>
<td>
<asp:Button Text="Submit" runat="server" OnClick="Submit" />
td>
tr>
table>
using System.Configuration;
using System.Data.SqlClient;
protected void Submit(object sender, EventArgs e)
{
string name = txtName.Text.Trim();
string gender = string.Empty;
if (rbMale.Checked)
{
gender = "M";
}
else if (rbFemale.Checked)
{
gender = "F";
}
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Persons(Name, Gender) VALUES(@Name, @Gender)"))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Gender", gender);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
Kindly accept the answer.Gunjan TiparePosted Aug 27, 2015, 4:26 AM