5 textbox radiobutton checkbox in asp.net
5 textbox radiobutton checkbox in asp.net
The TextBox control is used to get the input from the user of the web application.
For example.
Dropdown Box (Combo box) web control has the property autopostback.
If we set the property to true, whenever user selects a different value in the combo box, an
event will be fired in the server. i.e., a request will be sent to the server.
Example 2
Consider a login page, which contains text fields User ID, User Name and Password fields.
User name text box will not be editable.
So the requirement will be like this, when user enters the user id and clicks tab, his name
should be displayed in the User Name text field.
For achieving this we have to make autopostback property of the user id text field to true and
handle the event in the server side, In the event handler, we have to write code to fetch the
user name from the database for the user id, which we will be getting from the user id text
box.
Events of TextBox:
TextChanged - This event is fired when the text is changed.
Methods of a TextBox:
Focus - Set input focus onto the control.
To view the properties of the TextBox, Right click on the control, and select Properties. In
the properties window, you can also find the events supported by the control.
All these properties can be set at the design time, or at runtime using code.
In the properties window the thunder symbol is used to view the events associated with
the respective server control. Below, the events of a TextBox are seen.
ASP.NET Radio Button Control
Radio Button control is used, when you want the user to select only one option from
the available choices.
For example, the gender of a person. A person can be Male or Female. He cannot be both.
So, if the user has first selected Male, and if tries to select Female, the initial Male
selection he made should automatically get de-selected.
Another example, would be when you want the user to select his or her favourite colour.
In short, if you want to provide the user with mutually exclusive options, then choose
a Radio Button Control.
Events:
CheckedChanged - This event is fired when the checked status of the radio button control
is changed.
Drag and drop 2 radiobutton name them male and female and run the code.
You can select both. This should not be allowed , so we use group name property.
Select male radio button, go to properties, and give a name for GroupName property say
Gender, then select female radio button and give the GroupName property the same
Gender. Now run the code you will be allowed to select one radio button.
Another example, would be when you want the user to select the days of his availability.
In short, if you want to provide the user with more than one option to select from,
then choose a check box Control.
Methods:
Focus() - Just like TextBox, checkbox also supports, Focus() method. If you want to set the
input focus, to a specific checkbox, Call this method for that check box control.
Events:
CheckedChanged - This event is fired when the checked status of the check button control
is changed.
Set GroupName property as Gender for male,female and transgender radiobuttons for
mutually exclusive behaviour.
Fieldset and legend is used to get the look of gender and education seen in the web page.
First Set autopostback to false for textbox
// a word of caution, the webform name and webapplication name you are
using above should be the same as youa re using in VS
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div style="font-family:Arial">
<br />
Name :
<asp:TextBox ID="TextBox1" runat="server" BackColor="#00CC66" Font-
Bold="True" ForeColor="White"></asp:TextBox>
<br />
<br />
<div style="font-family:Arial">
<fieldset style="width:350px">
<legend><b>Gender</b></legend>
<asp:RadioButton ID="RadioButton1"
runat="server" GroupName="Gender" Text="Male" />
<asp:RadioButton ID="RadioButton2"
runat="server" GroupName="Gender" Text="Female" />
<asp:RadioButton ID="RadioButton3"
runat="server" GroupName="Gender" Text="Transgender" />
<br />
</fieldset><br />
<br />
<br />
<div style="font-family:Arial">
<fieldset style="width:350px">
<legend><b>Education</b></legend>
</form>
</body>
</html>
Code from the CodeBehind file
using System;
using System.Text;
namespace WebApplication8
// a word of caution the namespace name here should be the name of
the webapplication you are using in VS
{
public partial class WebForm2: System.Web.UI.Page
// a word of caution the webform name here should be the webform
name you are using in VS
{
protected void Page_Load(object sender, EventArgs e)
// Sender is the class object which has raised (or invoked) the event and EventArgs is a
//object which contains any data sent by the Sender.
{
if (!IsPostBack)
{
CheckBox2.Focus();
CheckBox1.Checked = true;
}
}
if (CheckBox1.Checked)
{
sbUserChoices.Append(CheckBox1.Text);
}
if (CheckBox2.Checked)
{
sbUserChoices.Append(" " + CheckBox2.Text);
}
if (CheckBox3.Checked)
{
sbUserChoices.Append(" " + CheckBox3.Text);
}
Response.Write("Your Degrees : " + sbUserChoices.ToString() + " ");
}
}
}