OnSelectedIndexChanged for a RadioButtonList in a DataGrid

D

DotNetGruven

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
 
S

Steven Cheng[MSFT]

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_SelectedIndexChanged" 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_SelectedIndexChanged" 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.)
 
S

Steven Cheng[MSFT]

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.)
 
D

DotNetGruven

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
 
D

DotNetGruven

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
 
S

Steven Cheng[MSFT]

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.)
 
D

DotNetGruven

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top