Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > OnSelectedIndexChanged for a RadioButtonList in a DataGrid

Reply
Thread Tools

OnSelectedIndexChanged for a RadioButtonList in a DataGrid

 
 
DotNetGruven
Guest
Posts: n/a
 
      01-28-2004
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


 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      01-29-2004
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>
<aspataGrid 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>
</aspataGrid></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.)

 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      01-31-2004
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.)



 
Reply With Quote
 
DotNetGruven
Guest
Posts: n/a
 
      02-02-2004
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]" <(E-Mail Removed)> wrote in message
news:V57IEY$(E-Mail Removed)...
> 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.)
>
>
>



 
Reply With Quote
 
DotNetGruven
Guest
Posts: n/a
 
      02-02-2004
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" <(E-Mail Removed)> wrote in message
news:e$(E-Mail Removed)...
> 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]" <(E-Mail Removed)> wrote in message
> news:V57IEY$(E-Mail Removed)...
> > 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.)
> >
> >
> >

>
>



 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      02-03-2004
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.)

 
Reply With Quote
 
DotNetGruven
Guest
Posts: n/a
 
      02-03-2004
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]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.)
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGrid Footer DropDownList OnSelectedIndexChanged ASP .Net 0 11-08-2007 02:08 PM
Datagrid - Dropdown OnSelectedIndexChanged Andy ASP .Net 1 09-28-2005 05:55 PM
OnSelectedIndexChanged event does not work with the selection on the checkbox ( on the cell of Datagrid control ) ? bienwell ASP .Net 2 03-09-2005 02:33 PM
DataGrid with DropDownLists not responding to OnSelectedIndexChanged G-Fit ASP .Net Datagrid Control 6 03-04-2004 10:07 AM
OnSelectedIndexChanged not firing Julie Barnet ASP .Net 2 08-21-2003 06:35 AM



Advertisments