Posts tagged: asp.net dropdownlist

Sep 05 2009

asp.net RadioButtonList control

RadioButtonList is an asp.net server control used to display list of radiobuttons and they grouped by default. This control is also used to allow only one selection in list of items.

Design code

<asp:RadioButtonList ID=”RadioButtonList1″ runat=”server” RepeatColumns=”2″>

<asp:ListItem>Apples</asp:ListItem>

<asp:ListItem>Grapes</asp:ListItem>

<asp:ListItem>Bananas</asp:ListItem>

<asp:ListItem>Mangos</asp:ListItem>

<asp:ListItem>Oranges</asp:ListItem>

<asp:ListItem>Oranges</asp:ListItem>

<asp:ListItem>Cocoa</asp:ListItem>

<asp:ListItem>CustardApple</asp:ListItem>

<asp:ListItem>Breadfruit</asp:ListItem>

</asp:RadioButtonList>

Properties of RadioButtonList

Cellpadding: The padding between each item.

CellSpacing: The spacing between each item.

RepeatColumns: The number of columns to use to lay out the items.

RepeatColumns=2

Repeat Direction: The direction in which items are laid out.

Horizontal / Vertical

Items: The collection of items in the list

TextAlign: The alignment of the text label with respect to each item.

Getting RadioButtonList Selected Item into label control

Step1: Drag RadioButtonList control and add above fruits list items

Step2: Add one Button control and Label control

Design code:

<asp:Label id=”Label1″ runat=”server”/>

<br/>

<asp:RadioButtonList ID=”RadioButtonList1″ runat=”server” RepeatColumns=”2″>

<asp:ListItem>Apples</asp:ListItem>

<asp:ListItem>Grapes</asp:ListItem>

<asp:ListItem>Bananas</asp:ListItem>

<asp:ListItem>Mangos</asp:ListItem>

<asp:ListItem>Oranges</asp:ListItem>

<asp:ListItem>Oranges</asp:ListItem>

<asp:ListItem>Cocoa</asp:ListItem>

<asp:ListItem>CustardApple</asp:ListItem>

<asp:ListItem>Breadfruit</asp:ListItem>

</asp:RadioButtonList>

<br />

<asp:Button ID=”Button1″ runat=”server” onclick=”Button1_Click” Text=”Button” />

Code: Double click Button control then write below code

protected void Button1_Click(object sender, EventArgs e)

{

Label1.Text = “Your favorite fruits is ” + RadioButtonList1.SelectedItem.Text;

}

RadioButtonList SelectedIndexChanged event

Step1: Use above same design code remove Button.

Step2: keep RadioButtonList AutopostBack Property is True

Step3: Double click in RadioButtonList then selectedIndexedChanded event fires

protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = “RadioButtonList selectedIndexChanged items is ” + RadioButtonList1.SelectedItem.Text;
}

using above code selected list item will display in Label control,dont forget to keep RadioButtonList property autopostBack True using RadioButtonList1_SelectedIndexChanged

How to edit selected value in Radiobuttonlist at runtime

consider RadioButtonlist contain Married and UnMarried items

//using database you get marital status value
//string maritialstatus=objdataset.Tables[0].Rows[0]["MaritialStatus"].ToString();
string maritialStatus=”Married”;

rbtnMartialstatus.SelectedValue = maritialStatus;

Sep 02 2009

asp.net radiobutton control

Asp.net RadioButton is a server control. It is used to select only one item in group of items. It will not allow multiple selection like checkbox control.

Asp.net radiobutton Properties

AutoPostBack: automatically post back to the server when the control is clicked.

Checked: The checked state of the control.

GroupName: Group that this radio button belongs to.

Text: The text label shown with the check box.

Text-Align: the alignment of the text label with repect to each item

Design code

<div>

Select your favorite fruit:

<br />

<asp:RadioButton id=”rbApple” Text=”Apple” Checked=”True”

GroupName=”fruit” runat=”server”/>

<br />

<asp:RadioButton id=”rbMango” Text=”Green”

GroupName=”fruit” runat=”server”/>

<br />

<asp:RadioButton id=”rbBanana” Text=”Banana”

GroupName=”fruit” runat=”server”/>

<br />

<asp:Button ID=”Button3″ text=”Submit” OnClick=”submit” runat=”server”/>

<p><asp:Label id=”Label1″ runat=”server”/></p>

</div>

asp.net radio button group or asp.net radio button grouping

see above design code

1st RadioButton id =”rbApple”      its GroupName =”fruit”

2nd RadioButton id =”rbMango”     its GroupName=”fruit”

3rd RadioButton id=”rbBanana”     its GroupName=”fruit”

Above design code every radio button GroupName is same(fruit)

asp.net radio button checked event

Write Below code in Buttom3 event (Double click in Button3)

Sample code:

//Radio Button Checked event

protected void Button3_Click(object sender, EventArgs e)

{

if (rbApple.Checked)

{

Label1.Text = “Your Favorite fruit is: ” + rbApple.Text;

}

else if (rbMango.Checked)

{

Label1.Text = “Your Favorite fruit is: ” + rbMango.Text;

}

else if (rbBanana.Checked)

{

Label1.Text = “Your Favorite fruit is: ” + rbBanana.Text;

}

}

Run the application

Result:

When you choose Apple in radiobutton and click Button

then label1 will display

“Your Favorite fruit is Apple”

……….

asp.net radiobutton oncheckedchanged

<asp:RadioButton id=”rbApple” Text=”Apple” Checked=”True”

GroupName=”fruit” runat=”server” AutoPostBack=”True” oncheckedchanged=”rbApple_CheckedChanged”/>

<br />

<asp:RadioButton id=”rbMango” Text=”Green”

GroupName=”fruit” runat=”server” AutoPostBack=”True” oncheckedchanged=”rbApple_CheckedChanged”/>

<br />

<asp:RadioButton id=”rbBanana” Text=”Banana” AutoPostBack=”True” oncheckedchanged=”rbApple_CheckedChanged”

GroupName=”fruit” runat=”server”/>

1) see above design code oncheckedchanged=”rbApple_CheckedChanged” for every radio button is same

2) AutopostBack Property is true for every radio button

protected void rbApple_CheckedChanged(object sender, EventArgs e)

{

//rbApple_CheckedChanged for every radio button must be same

if (rbApple.Checked)

{

Label1.Text = “Your Favorite fruit is: ” + rbApple.Text;

}

else if (rbMango.Checked)

{

Label1.Text = “Your Favorite fruit is: ” + rbMango.Text;

}

else if (rbBanana.Checked)

{

Label1.Text = “Your Favorite fruit is: ” + rbBanana.Text;

}

}

Happy coding…

Alibi3col theme by Themocracy