OnSelectedIndexChanged for a RadioButtonList in a DataGrid

Discussion in 'ASP .Net' started by DotNetGruven, Jan 28, 2004.

  1. DotNetGruven

    DotNetGruven Guest

    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
     
    DotNetGruven, Jan 28, 2004
    #1
    1. Advertising

  2. 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.)
     
    Steven Cheng[MSFT], Jan 29, 2004
    #2
    1. Advertising

  3. 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.)
     
    Steven Cheng[MSFT], Jan 31, 2004
    #3
  4. DotNetGruven

    DotNetGruven Guest

    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]" <> wrote in message
    news:V57IEY$...
    > 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.)
    >
    >
    >
     
    DotNetGruven, Feb 2, 2004
    #4
  5. DotNetGruven

    DotNetGruven Guest

    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" <> wrote in message
    news:e$...
    > 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]" <> wrote in message
    > news:V57IEY$...
    > > 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.)
    > >
    > >
    > >

    >
    >
     
    DotNetGruven, Feb 2, 2004
    #5
  6. 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.)
     
    Steven Cheng[MSFT], Feb 3, 2004
    #6
  7. DotNetGruven

    DotNetGruven Guest

    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]" <> wrote in message
    news:...
    > 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.)
    >
     
    DotNetGruven, Feb 3, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Julie Barnet

    OnSelectedIndexChanged not firing

    Julie Barnet, Aug 20, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    9,010
    Yusuf D M
    Aug 21, 2003
  2. Jon Maz
    Replies:
    0
    Views:
    430
    Jon Maz
    Aug 24, 2003
  3. Andy
    Replies:
    1
    Views:
    3,972
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    Sep 28, 2005
  4. Guest
    Replies:
    0
    Views:
    460
    Guest
    Nov 8, 2007
  5. G-Fit
    Replies:
    6
    Views:
    209
    G-Fit
    Mar 4, 2004
Loading...

Share This Page