Datalist and RadioButton

Discussion in 'ASP .Net Web Controls' started by Rob Risner, Apr 20, 2004.

  1. Rob Risner

    Rob Risner Guest

    Is there a way to use a RadioButton web control in a datalist template
    column without the button getting a unique name? It doesn't allow a single
    radio button to be clicked.

    Thanks,
    Rob
    Rob Risner, Apr 20, 2004
    #1
    1. Advertising

  2. Radiobuttonlists implement inamingcontainer which forces unique id's. What
    are you trying to do? Maybe there is a workaround

    --
    Regards,
    Alvin Bruney [ASP.NET MVP]
    Got tidbits? Get it here...
    http://tinyurl.com/27cok
    "Rob Risner" <> wrote in message
    news:%...
    > Is there a way to use a RadioButton web control in a datalist template
    > column without the button getting a unique name? It doesn't allow a single
    > radio button to be clicked.
    >
    > Thanks,
    > Rob
    >
    Alvin Bruney [MVP], Apr 20, 2004
    #2
    1. Advertising

  3. Rob Risner

    Rob Risner Guest

    I am trying to display a list of images and allow the user to click on a
    radio button to select one. I thought about the Radiobuttonlist but that
    seems to want a Datatextfield to show one item from the database.

    Thanks for the quick reply,
    Rob

    "Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message
    news:%23w3$...
    > Radiobuttonlists implement inamingcontainer which forces unique id's. What
    > are you trying to do? Maybe there is a workaround
    >
    > --
    > Regards,
    > Alvin Bruney [ASP.NET MVP]
    > Got tidbits? Get it here...
    > http://tinyurl.com/27cok
    > "Rob Risner" <> wrote in message
    > news:%...
    >> Is there a way to use a RadioButton web control in a datalist template
    >> column without the button getting a unique name? It doesn't allow a
    >> single radio button to be clicked.
    >>
    >> Thanks,
    >> Rob
    >>

    >
    >
    Rob Risner, Apr 20, 2004
    #3
  4. Hi Rob,

    Based on my understanding, you nested RadioButton in the DataList control,
    and you want to handle the CheckedChanged event for different RadioButton.

    Suppose you are doing like this:
    <asp:DataList id="DataList1" runat="server">
    <ItemTemplate>
    <asp:RadioButton Runat="server" ID="rb"></asp:RadioButton>
    </ItemTemplate>
    </asp:DataList>

    To handle the RadioButton's CheckedChanged event for each row, you should
    hook into DataList.ItemCreated event, and add the event handler for it. Do
    like this:

    private void DataList1_ItemCreated(object sender,
    System.Web.UI.WebControls.DataListItemEventArgs e)
    {
    if(e.Item.ItemType==ListItemType.Item)
    {
    RadioButton rb=e.Item.FindControl("rb") as RadioButton;
    rb.CheckedChanged+=new EventHandler(rb_CheckedChanged);
    }
    }

    private void rb_CheckedChanged(object sender, EventArgs e)
    {
    }

    Then, in the rb_CheckedChanged, you can do your program logic.

    ===============================================
    Please apply my suggestion above and let me know if it helps resolve your
    problem.

    Thank you for your patience and cooperation. If you have any questions or
    concerns, please feel free to post it in the group. I am standing by to be
    of assistance.

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
    Jeffrey Tan[MSFT], Apr 20, 2004
    #4
  5. Hi Rob,

    Does my reply make sense to you? Do you still have any concern on this
    issue?

    Please feel free to post. Thanks

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
    Jeffrey Tan[MSFT], Apr 22, 2004
    #5
  6. Rob Risner

    Rob Risner Guest

    I tried your method and could not get it to work. I've stepped through and
    it's adding a new event handler to the rb.CheckedChanged but rb_CheckChanged
    is not being called when the radio button is being click during a post back.
    This also does not solve the problem with the user being able to click
    multiple radio buttons and their name uniquness. I've worked around this by
    simply using a radio button list instead.

    Thanks,
    Rob

    ""Jeffrey Tan[MSFT]"" <> wrote in message
    news:...
    > Hi Rob,
    >
    > Based on my understanding, you nested RadioButton in the DataList control,
    > and you want to handle the CheckedChanged event for different RadioButton.
    >
    > Suppose you are doing like this:
    > <asp:DataList id="DataList1" runat="server">
    > <ItemTemplate>
    > <asp:RadioButton Runat="server" ID="rb"></asp:RadioButton>
    > </ItemTemplate>
    > </asp:DataList>
    >
    > To handle the RadioButton's CheckedChanged event for each row, you should
    > hook into DataList.ItemCreated event, and add the event handler for it. Do
    > like this:
    >
    > private void DataList1_ItemCreated(object sender,
    > System.Web.UI.WebControls.DataListItemEventArgs e)
    > {
    > if(e.Item.ItemType==ListItemType.Item)
    > {
    > RadioButton rb=e.Item.FindControl("rb") as RadioButton;
    > rb.CheckedChanged+=new EventHandler(rb_CheckedChanged);
    > }
    > }
    >
    > private void rb_CheckedChanged(object sender, EventArgs e)
    > {
    > }
    >
    > Then, in the rb_CheckedChanged, you can do your program logic.
    >
    > ===============================================
    > Please apply my suggestion above and let me know if it helps resolve your
    > problem.
    >
    > Thank you for your patience and cooperation. If you have any questions or
    > concerns, please feel free to post it in the group. I am standing by to be
    > of assistance.
    >
    > Best regards,
    > Jeffrey Tan
    > Microsoft Online Partner Support
    > Get Secure! - www.microsoft.com/security
    > This posting is provided "as is" with no warranties and confers no rights.
    >
    Rob Risner, Apr 22, 2004
    #6
  7. Hi Rob,

    Thanks very much for your feedback.

    For your RadioButton event does not fire problem, it is a common issue.
    Normally, it is because you did not determine the postback, and re-bind
    your datagrid every postback time. Like this:

    private void Page_Load(object sender, System.EventArgs e)
    {
    DataSet ds=new DataSet ();
    SqlDataAdapter adapter=new SqlDataAdapter("select * from
    jobs","server=localhost;database=pubs;uid=sa;pwd=");
    adapter.Fill(ds);
    DataGrid1.DataSource=ds;
    DataGrid1.DataBind();
    }

    Through this way, your datagrid's child controls(Such as RadioButton) will
    be re-created every postback, its status will be re-freshed every time, so
    its event will not fire. Instead, you should only bind the DataGrid at the
    first time, like this:

    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!this.IsPostBack)
    {
    DataSet ds=new DataSet ();
    SqlDataAdapter adapter=new SqlDataAdapter("select * from
    jobs","server=localhost;database=pubs;uid=sa;pwd=");
    adapter.Fill(ds);
    DataGrid1.DataSource=ds;
    DataGrid1.DataBind();
    }
    }

    Through this way, the event should fire for you.

    If this is not your problem, please pasted some code snippet to reproduce
    your problem, I will figure out the problem for you.
    ========================================

    Thank you for your patience and cooperation. If you have any questions or
    concerns, please feel free to post it in the group. I am standing by to be
    of assistance.

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
    Jeffrey Tan[MSFT], Apr 22, 2004
    #7
  8. Rob Risner

    Rob Risner Guest

    There is still the problem with unique RadioButtons.

    The datalist changes the names to
    DataList1__ctl0_rb
    DataList1__ctl1_rb
    Etc...

    This allows for mulitple RadioButtons to be checked.

    ""Jeffrey Tan[MSFT]"" <> wrote in message
    news:xi$...
    > Hi Rob,
    >
    > Thanks very much for your feedback.
    >
    > For your RadioButton event does not fire problem, it is a common issue.
    > Normally, it is because you did not determine the postback, and re-bind
    > your datagrid every postback time. Like this:
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > DataSet ds=new DataSet ();
    > SqlDataAdapter adapter=new SqlDataAdapter("select * from
    > jobs","server=localhost;database=pubs;uid=sa;pwd=");
    > adapter.Fill(ds);
    > DataGrid1.DataSource=ds;
    > DataGrid1.DataBind();
    > }
    >
    > Through this way, your datagrid's child controls(Such as RadioButton) will
    > be re-created every postback, its status will be re-freshed every time, so
    > its event will not fire. Instead, you should only bind the DataGrid at the
    > first time, like this:
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > if(!this.IsPostBack)
    > {
    > DataSet ds=new DataSet ();
    > SqlDataAdapter adapter=new SqlDataAdapter("select * from
    > jobs","server=localhost;database=pubs;uid=sa;pwd=");
    > adapter.Fill(ds);
    > DataGrid1.DataSource=ds;
    > DataGrid1.DataBind();
    > }
    > }
    >
    > Through this way, the event should fire for you.
    >
    > If this is not your problem, please pasted some code snippet to reproduce
    > your problem, I will figure out the problem for you.
    > ========================================
    >
    > Thank you for your patience and cooperation. If you have any questions or
    > concerns, please feel free to post it in the group. I am standing by to be
    > of assistance.
    >
    > Best regards,
    > Jeffrey Tan
    > Microsoft Online Partner Support
    > Get Secure! - www.microsoft.com/security
    > This posting is provided "as is" with no warranties and confers no rights.
    >
    Rob Risner, Apr 22, 2004
    #8
  9. Hi Rob,

    Thanks very much for your feedback.

    Oh, yes, in Asp.net Repeater controls, you can not make them Mutually
    Exclusive, this is a known issue, please refer to:
    "BUG: Radio Buttons Are Not Mutually Exclusive When Used in a Repeater
    Server Control"
    http://support.microsoft.com/default.aspx?scid=kb;en-us;316495

    Thank you for your patience and cooperation. If you have any questions or
    concerns, please feel free to post it in the group. I am standing by to be
    of assistance.

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
    Jeffrey Tan[MSFT], Apr 22, 2004
    #9
  10. Hi Rob,

    Does my reply make sense to you? Do you still have any concern on this
    issue?

    Please feel free to post. Thanks

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
    Jeffrey Tan[MSFT], Apr 26, 2004
    #10
  11. Hi everyone,
    I'm currently encountering the same problem. Microsoft states, it's a
    bug, but there seems to be no bug fix for this.
    Does anybody know a workaround ?


    Thanx in advance

    Sascha

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Sascha Bötzel, Apr 27, 2004
    #11
    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. Nevyn Twyll
    Replies:
    8
    Views:
    1,848
  2. Luis Esteban Valencia Muñoz
    Replies:
    0
    Views:
    753
    Luis Esteban Valencia Muñoz
    Jun 3, 2005
  3. Gert
    Replies:
    0
    Views:
    471
  4. ensingg
    Replies:
    0
    Views:
    663
    ensingg
    Sep 15, 2006
  5. ensingg

    Radiobutton inside datalist

    ensingg, Sep 17, 2006, in forum: .NET
    Replies:
    0
    Views:
    621
    ensingg
    Sep 17, 2006
Loading...

Share This Page