473,397 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

OnSelectedIndexChanged for a RadioButtonList in a DataGrid

I have a webform that has a DataGrid on it with a RadioButtonList in each
row. It is a simple On & Off.

When the User Clicks on either of the RadioButtons, I need to postback to
the server and update the database.

Here is the control containment hierarchy:

ASP Page
Static User Control // just provides a pretty border around
contained user control
Dynamic User Control
Data Grid:
RadioButtonList: Autopostback = true and
OnSelectedIndexChanged hooked up

When I click on any radio button, I get a SelectedItemChange for each row
that was in the off state and all are now on!

Any ideas?

TIA
George
Nov 18 '05 #1
6 7694
Hi George,
Thanks for posting in the community! My name is Steven, and I'll be
assisting you on this issue.
From your description, you used a RadioButtonList control in a DataGrid's
template column. The RadioButtonList contains two items. "On" and "Off" and
was set as AutoPostBack=True. And you want to do some database
manipulations in the RadioButtonList's SelectIndexChanged postback event,
yes?
If there is anything I misunderstood, please feel free to let me know.

As for this problem, I think you may try the following steps to accomplish
it:
1. Write a event handler function for the RadioButtonList like below:
protected void rblState_SelectedIndexChanged(object sender,
System.EventArgs e)
{
// retrieve the RadionButton's value and the DataGridItem(which contains
the certain RadioButtonList) 's index
// and do some database operations
}

2. Rigister the handler for the RadioButtonList control in the aspx page,
such as:
<asp:RadioButtonList id=rblState runat="server"
OnSelectedIndexChanged="rblState_SelectedIndexChan ged" AutoPostBack="True"
SelectedIndex='<%# SetState(DataBinder.Eval(Container.DataItem,"state "))
%>'>

To make the above description clearly, I've made a sample page, you may
refer to it if you feel anything unclear:
-------------------------------------aspx
page------------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>RBL</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table align="center" width="500">
<tr>
<td>
<asp:Label id="lblMessage" runat="server"></asp:Label></td>
</tr>
<tr>
<td>
<asp:DataGrid id="dgRBL" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="index"
HeaderText="Index"></asp:BoundColumn>
<asp:BoundColumn DataField="name"
HeaderText="Name"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="State">
<ItemTemplate>
<FONT face="ËÎÌå">
<asp:RadioButtonList id=rblState runat="server"
OnSelectedIndexChanged="rblState_SelectedIndexChan ged" AutoPostBack="True"
SelectedIndex='<%# SetState(DataBinder.Eval(Container.DataItem,"state "))
%>'>
<asp:ListItem Value="On">On</asp:ListItem>
<asp:ListItem Value="Off">Off</asp:ListItem>
</asp:RadioButtonList></FONT>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid></td>
</tr>
</table>
</form>
</body>
</HTML>
--------------------------------code behind page
class------------------------------
public class RBL : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblMessage;
protected System.Web.UI.WebControls.DataGrid dgRBL;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
BindGrid();
}
}

protected void BindGrid()
{

DataTable tb = new DataTable();
tb.Columns.Add("index");
tb.Columns.Add("name");
tb.Columns.Add("state");

for(int i=0;i<15;i++)
{
int index = i+1;
DataRow newrow = tb.NewRow();

newrow["index"] = index.ToString();
newrow["name"] = "Name" + index.ToString();

if(i%2 == 0)
{
newrow["state"] = true;
}
else
{
newrow["state"] = false;
}

tb.Rows.Add(newrow);
}

dgRBL.DataSource = tb;
dgRBL.DataBind();
}

protected void rblState_SelectedIndexChanged(object sender,
System.EventArgs e)
{

RadioButtonList rbl = (RadioButtonList)sender;

Control parent = rbl.Parent;

while(!(parent is System.Web.UI.WebControls.DataGridItem))
{
parent = parent.Parent;
}

int index = ((DataGridItem)parent).ItemIndex;

if(rbl.SelectedIndex == 0)
{
lblMessage.Text = "Row[" + index + "]'s state is changed to On!";
}
else
{
lblMessage.Text = "Row[" + index + "]'s state is changed to Off!";
}

}

protected int SetState(object st)
{
string state = st.ToString();

if(state.Equals("True"))
{
return 0;
}
else
{
return 1;
}

}

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}

}

-----------------------------------------------

Please check out the preceding suggestions. If you need any further
assistance, please feel free to post here.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #2
Hi George,
Have you had a chance to try out my suggestion or the code or have you got
any progress on this issue? If you have any questions, please feel free to
let me know.
Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #3
Hi Steven,

Yes, I have looked at your suggestion and it works, but the

production code I'm working on still doesn't. I can't figure out why.

I built a page that is a similar implementation of my production page,

and it works. I can't send you the production code because it relies on

a Business Layer that relies on a Database Layer that relies on an

Oracle Data Source.

Also, I've been working on other projects but now my priority is

this task. I'll let your know how I'm doing and if I have any

questions.

Thanks,

George

"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:V5*************@cpmsftngxa07.phx.gbl...
Hi George,
Have you had a chance to try out my suggestion or the code or have you got
any progress on this issue? If you have any questions, please feel free to
let me know.
Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #4
OK, found the problem!

In the ItemDataBound Handler of the DataGrid I was setting the Value on both
List Items in the RadioButtonList. In the code sample that Stephen
provided, I noticed he was only setting the Value Attribute of the Item[0].

In my ItemDataBound Handler that was causing me problems, I was doing this:

listItem[0].Value = listItem[1].Value =
myDataRowView.Item["fieldOfInterest"].toString();

changing it to this made the problem go away:

listItem[0].Value = myDataRowView.Item["fieldOfInterest"].toString();

It turns out, the bug goes away if I only set listItem[0]!!!

This doesn't make sense to me! Can anyone explain why this isn't a bug in
the RadioButtonList WebControl??

Thanks,
George
"DotNetGruven" <ms********@javagruven.com> wrote in message
news:e$**************@TK2MSFTNGP11.phx.gbl...
Hi Steven,

Yes, I have looked at your suggestion and it works, but the

production code I'm working on still doesn't. I can't figure out why.

I built a page that is a similar implementation of my production page,

and it works. I can't send you the production code because it relies on

a Business Layer that relies on a Database Layer that relies on an

Oracle Data Source.

Also, I've been working on other projects but now my priority is

this task. I'll let your know how I'm doing and if I have any

questions.

Thanks,

George

"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:V5*************@cpmsftngxa07.phx.gbl...
Hi George,
Have you had a chance to try out my suggestion or the code or have you got any progress on this issue? If you have any questions, please feel free to let me know.
Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


Nov 18 '05 #5
Hi DotNetGruven,
Thanks for your response. I'm glad that you've found out the root cause of
the problem in this issue. As for why the problem will occur when you use
the below code:
listItem[0].Value = listItem[1].Value =
myDataRowView.Item["fieldOfInterest"].toString();
This is because the RadioButtonList will be rendered as a certain group of
<input type=radio ...> into the client browser and the <input tyep=
radio..> ones have the same id. And in html page, certain <input type=radio
..> controls which have the same id(in the same group) can't be set as same
value(every member just the ListIItem on serverside should have a unique
value different from the others in the same group), other wise, there'll
occur unexpected errors. So if you set all the ListItem of a
RadioButtonList as same value, it'll cause the strange behavior you've met.
And when you set them as different value, the error disappears. How do you
think of this?

Please check out my suggestion, if you feel anything unclear on it, please
feel free to let me know.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #6
Thanks for your help with this problem.

It seems that the value attribute of each element should not have to be
different, but if that is the way it works, so be it.

Thanks again,
George
"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:Gq**************@cpmsftngxa07.phx.gbl...
Hi DotNetGruven,
Thanks for your response. I'm glad that you've found out the root cause of
the problem in this issue. As for why the problem will occur when you use
the below code:
listItem[0].Value = listItem[1].Value =
myDataRowView.Item["fieldOfInterest"].toString();
This is because the RadioButtonList will be rendered as a certain group of
<input type=radio ...> into the client browser and the <input tyep=
radio..> ones have the same id. And in html page, certain <input type=radio .> controls which have the same id(in the same group) can't be set as same
value(every member just the ListIItem on serverside should have a unique
value different from the others in the same group), other wise, there'll
occur unexpected errors. So if you set all the ListItem of a
RadioButtonList as same value, it'll cause the strange behavior you've met. And when you set them as different value, the error disappears. How do you
think of this?

Please check out my suggestion, if you feel anything unclear on it, please
feel free to let me know.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Empire City | last post by:
I have an ASP.NET form with a DataGrid and Button. I want to put a RadioButtonList in a DataGrid cell. I bind it to an ArrayList which has a ListItem in the cell. The display part works fine. I...
0
by: Bob Hoke | last post by:
I am trying to do some of the things we used to do in classic ASP that required some pretty ugly loops in the .ASP page. I have two different DataSets that each have a DataView that I can change...
1
by: Tim | last post by:
I have a RadioButtonList inside the EditItemTemplate of a Datagrid. On the Edit command of the datagrid I am calling the following command: public void dgNodeBranches_Edit(object sender,...
5
by: DotNetGruven | last post by:
Anyone have any pointers on how to set the Value and Selected attributes in a ListItem in a RadioButtonList that is in a DataGrid? Here's what I have ------DataGrid------ -- BoundColumn 0 --...
0
by: Luis Esteban Valencia | last post by:
http://www.codeproject.com/aspnet/DataGridCCEvents.asp#xx1009236xx Read this first and see if you can help me have tried but the Intelisense of the radiobuttonlist doestn have the event...
2
by: Brian | last post by:
I have a datagrid that is listing a bunch of questions from my DB.. I am creating a radiobuttonlist for each question to give me a yes or no answer. I would like to use the "QuestionCode" column...
0
by: ad | last post by:
I have a tabe with a filed SexID, it use 1 for Male and 2 for Female. I want to let user can edit this in a DataGrid with a RadioButtonList. I add a RadioButtonList in the EditItemTemplate:...
2
by: glenn | last post by:
Hi folks, I am trying to determine which item in a DropDownList Web control has been selected. I have posted an OnSelectedIndexChanged subroutine in my code with a reference to the subroutine...
0
by: rcoco | last post by:
Hi, Ihave a datagrid that Insert data in some rows. One column has RadioButtonList that contains three Items. But When I select one radiobuton, insert data in other columns and select insert data...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.