5 Replies - 40260 Views - Last Post: 23 December 2009 - 11:06 PM

#1 krazyk   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 22-May 09

to get value for RadioButtonList in asp.net with c#

Posted 15 November 2009 - 01:22 AM

hello Friends,

m using RadioButtonList control in asp.net with c#

m getting value from database for RadioButtonList 's ListItem different from inputed..

here is ma code ...

aspx page...

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebFormEx.aspx.cs" Inherits="WebApplication1.WebFormEx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
	<title>Untitled Page</title>
</head>
<body>
	<form id="form1" runat="server">
	<div>
		
		<asp:RadioButtonList ID="RadioButtonList1" runat="server" 
			RepeatDirection="Horizontal"
			onselectedindexchanged="RadioButtonList1_SelectedIndexChanged">
			<asp:ListItem>item1</asp:ListItem>
			<asp:ListItem>item2</asp:ListItem>
		</asp:RadioButtonList> 
	
	</div>
	</form>
</body>
</html>



this is aspx.cs page..

	  protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
			   con.Open();

			   OleDbCommand cmd=new OleDbCommand("SELECT item1 FROM Table1",con);
				
			   OleDbDataReader rd=cmd.ExecuteReader();

			   while(rd.Read())
			   {
				  RadioButtonList1.Text=rd["item1"].ToString();
			   }
			   
			   con.Close();
			 }
		  }



i had field named item1 in table name Table1 in database..


do i need to bind RadioButtonList control to database's table?...

for ListItem item1 & item2 ..do i need 2 fields in database table?

plz tell me whr m going wrong?

thanks in advance..

Is This A Good Question/Topic? 0
  • +

Replies To: to get value for RadioButtonList in asp.net with c#

#2 ramesh866   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 91
  • Joined: 03-October 09

Re: to get value for RadioButtonList in asp.net with c#

Posted 15 November 2009 - 08:21 AM

you are using the connection in the page load event, if you want to get values or dats based on the selected radio button you have to use the checkedchanged event of the radio button..
Was This Post Helpful? 0
  • +
  • -

#3 krazyk   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 22-May 09

Re: to get value for RadioButtonList in asp.net with c#

Posted 15 November 2009 - 11:57 AM

for RadioButtonList control thr is two ListItem..

and in checkedchanged event only the d id of the RadioButtonList is refered rathe than tht two ListItem..

well i had put the condition for tht...

bt works nothing!!!


m i going wrong?
Was This Post Helpful? 0
  • +
  • -

#4 ramesh866   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 91
  • Joined: 03-October 09

Re: to get value for RadioButtonList in asp.net with c#

Posted 15 November 2009 - 01:55 PM

 protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
	{

		string s = ConfigurationManager.ConnectionStrings["masterConnectionString"].ConnectionString;
		SqlConnection cn = new SqlConnection(s);
		cn.Open();

			RadioButton rd;
			rd = (RadioButton)sender;
			switch (rd.ID)
			{
				case "RadioButton1":
					RadioButton2.Checked = false;
				   
					SqlCommand cmd = new SqlCommand("select names from radio", cn);
					SqlDataReader dr = cmd.ExecuteReader();
					while (dr.Read())
					{
						Label1.Text = Convert.ToString(dr[0]);
					}
					break;
				case "RadioButton2":
					RadioButton1.Checked = false;
					
					SqlCommand cmd1 = new SqlCommand("select phone from radio", cn);
					SqlDataReader d1 = cmd1.ExecuteReader();
					while (d1.Read())
					{
						Label1.Text = Convert.ToString(d1[0]);
					}
					break;
			}
		
	}



this code works fine.. try it.. just change the onchecked event to a single event handler .. in my case i did with "RadioButton1_CheckedChanged".

This post has been edited by ramesh866: 15 November 2009 - 02:20 PM

Was This Post Helpful? 0
  • +
  • -

#5 krazyk   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 22-May 09

Re: to get value for RadioButtonList in asp.net with c#

Posted 16 November 2009 - 06:14 AM

ok...

here you have used 2 RadioButtons ....

the code is perfect..!!


i was just looking for RadioButtonList control ...group of radiobuttons..


well m doing ma best .!!!

Thanks a Lot ..it is also appreciated..!!!
Was This Post Helpful? 0
  • +
  • -

#6 c111   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-December 09

Re: to get value for RadioButtonList in asp.net with c#

Posted 23 December 2009 - 11:06 PM

string sel_gender=ds.Tables[0].Rows[0]["gender"].ToString();
if(sel_gender.Trim().ToString()=="Male")
rbl_gender.SelectedIndex=0;
else
rbl_gender.SelectedIndex=1;
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1