Use RadioButtonList to enable dropdown list

Last post 11-17-2009 8:37 AM by Craigmeister68. 14 replies.

Sort Posts:

  • Use RadioButtonList to enable dropdown list

    11-12-2009, 11:48 AM
    • Member
      5 point Member
    • Craigmeister68
    • Member since 07-30-2009, 9:21 AM
    • Charlotte, NC
    • Posts 32

    I am trying to have a radiobuttonlist control enable and disable certain drop down boxes depending on the item selected. The problem I am having is that the radiobuttonlist is also a server/ASP control that is bound to a database field. Thus I cannot mix server and client-side code. (Head-slapping compile errors ensue).

    From what I can tell I have 2 options:

    1. Make the button list control a client side control and use javascript to enable and disable the appropriate dropdown boxes.

    2. Use code behind to set the Enabled attributes of the dropdown box.

    The problem with 1 is how to get the value of the radiobuttonlist into the SQLDatasource. Would it make sense to put a line in the javascript to set the value of the radiobuttonlist to text box that is set as Visible="false" and is bound to the SQLDatasource field?

    The problem with 2 is that it really doesn't make sense do a postback roundtrip for a purely client action.

    Does anybody have any thoughts?

    Thanks.

     

  • Re: Use RadioButtonList to enable dropdown list

    11-12-2009, 12:28 PM
    • Member
      140 point Member
    • jerrylefou
    • Member since 08-26-2009, 4:27 PM
    • Posts 27

    You can keep your Radiobutton list server side and add to it some js client side. A server side controle can still run client side code. 

    Jerry
    .Net Dev
  • Re: Use RadioButtonList to enable dropdown list

    11-12-2009, 1:06 PM
    • Contributor
      5,014 point Contributor
    • deepthoughts
    • Member since 01-27-2009, 2:55 PM
    • Posts 739

    Hi!

    You can ofcourse use javascript with the databound RadioButtonList.. There could be several approaches to achieve the given task.. One approach could be to handle the OnDataBound event of the RadioButtonList and assign the onclick attribute to your desired items and in that call a javascript function that disables the respective dropdownlist.

     

    I've created a test scenario for you... here I've got two items in the databound radiobuttonlist and I've got two dropdownlists.. when one item is selected first dropdownlists gets disabled and when other is selected the other gets disabled. Please check the code below..

    ASPX page

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script type="text/javascript">
            function DisableDDL(ddlId) {
                document.getElementById(ddlId).disabled = "disabled";
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:DropDownList ID="ddl1" runat="server">
        <asp:ListItem>Val 1</asp:ListItem>
        <asp:ListItem>Val 2</asp:ListItem>
        </asp:DropDownList>
        <br />
        <asp:DropDownList ID="ddl2" runat="server">
        <asp:ListItem>Val 3</asp:ListItem>
        <asp:ListItem>Val 4</asp:ListItem>
        </asp:DropDownList>
        
            <asp:RadioButtonList ID="RDBL1" runat="server" DataSourceID="SqlDataSource1" DataValueField="ID" DataTextField="Name" OnDataBound="RDBL1_DataBound">
            </asp:RadioButtonList>
            
            <br />
            
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [Options]" >
            </asp:SqlDataSource>
        </div>
        </form>
    </body>
    </html>


     

    Codebehind OnDataBound event..

        protected void RDBL1_DataBound(object sender, EventArgs e)
        {
            RDBL1.Items[0].Attributes.Add("onclick", "DisableDDL('" + ddl1.ClientID + "')");
            RDBL1.Items[1].Attributes.Add("onclick", "DisableDDL('" + ddl2.ClientID + "')");
        }


    So I hope you get the idea..

     

    Thanks.

    MARK AS ANSWER if it helps
  • Re: Use RadioButtonList to enable dropdown list

    11-12-2009, 1:49 PM
    • Member
      5 point Member
    • Craigmeister68
    • Member since 07-30-2009, 9:21 AM
    • Charlotte, NC
    • Posts 32

    Would I put the bind operation on RDB1 in the  RDB1_DataBound event as well rather than in the HTML?

  • Re: Use RadioButtonList to enable dropdown list

    11-12-2009, 2:00 PM
    • Contributor
      5,014 point Contributor
    • deepthoughts
    • Member since 01-27-2009, 2:55 PM
    • Posts 739

    Craigmeister68:

    Would I put the bind operation on RDB1 in the  RDB1_DataBound event as well rather than in the HTML?

     

    You see in the ASPX page in the <asp:RadioButtonList> declaration we've got OnDataBound="RDBL1_DataBound" which means that when the RadioButtonList has been bound to underlying datasource call this event.. I couldn't understand what do you mean b HTML??

     

    Thanks.

    MARK AS ANSWER if it helps
  • Re: Use RadioButtonList to enable dropdown list

    11-12-2009, 2:03 PM
    • Member
      5 point Member
    • Craigmeister68
    • Member since 07-30-2009, 9:21 AM
    • Charlotte, NC
    • Posts 32

    Also, do you have a VB version o fthe code behind? I am getting a message 'Name <control name> not declared' when I put in your code and substitue my control name.

    Thanks.

     

  • Re: Use RadioButtonList to enable dropdown list

    11-12-2009, 2:10 PM
    • Contributor
      5,014 point Contributor
    • deepthoughts
    • Member since 01-27-2009, 2:55 PM
    • Posts 739

    Hi! Please check...

    Protected Sub RDBL1_DataBound(ByVal sender As Object, ByVal e As EventArgs)
        RDBL1.Items(0).Attributes.Add("onclick", "DisableDDL('" & ddl1.ClientID & "')")
        RDBL1.Items(1).Attributes.Add("onclick", "DisableDDL('" & ddl2.ClientID & "')")
    End Sub


    Thanks.

    P.S. You can always convert C# code to VB and vice versa through this converter tool. 

    MARK AS ANSWER if it helps
  • Re: Use RadioButtonList to enable dropdown list

    11-12-2009, 2:27 PM
    • Member
      5 point Member
    • Craigmeister68
    • Member since 07-30-2009, 9:21 AM
    • Charlotte, NC
    • Posts 32

     I am officially pounding my head on the table.

    I am using your example above and am getting Name <control> is not declared.'  The name, ID, everything is the same. I simply copied the first part of the sub name to make sure I had everything the same.

    I assume there is nothing more needed or you would have added them. Am I right?

  • Re: Use RadioButtonList to enable dropdown list

    11-12-2009, 2:44 PM
    • Member
      296 point Member
    • tusharrs
    • Member since 05-02-2009, 3:39 PM
    • Posts 78

    hello,

    i agree with deepthoughts  but i think you also need to make the dropdownlist enabled when the radionbuttonlist

    item is unselected

    change databound code and send radionbuttonlist item's client id also like below

    protected void RDBL1_DataBound(object sender, EventArgs e)  

    {  

        RDBL1.Items[0].Attributes.Add("onclick""DisableDDL('" + ddl1.ClientID + "','" + RDBL1.Items[0].ClientID + "'"));  

        RDBL1.Items[1].Attributes.Add("onclick""DisableDDL('" + ddl2.ClientID + "','" + RDBL1.Items[1].ClientID + "'));  

    }

    if you need change the javacript function to


      <script type="text/javascript">  

            function DisableDDL(ddlId,rblitemid)

           {  

                if (document.getElementById(rblitemid).checked == true )

                           document.getElementById(ddlId).disabled = "disabled";  

               else

                          document.getElementById(ddlId).disabled = ! document.getElementById(ddlId).disabled;  

           }      

      </script>


    Regards

    ( Mark as Answer if it helps you out )

    View my Blog
  • Re: Use RadioButtonList to enable dropdown list

    11-12-2009, 3:30 PM
    • Member
      5 point Member
    • Craigmeister68
    • Member since 07-30-2009, 9:21 AM
    • Charlotte, NC
    • Posts 32

    Using MS Visual Web Developer and getting message 'Name <control ID> is not declared' in the code on the aspx.vb page for both RadioButtonList1 and DropDownList3. Code: 

    Protected Sub RadioButtonList1_Databound(ByVal sender As Object, ByVal e As System.EventArgs)
            RadioButtonList1.Items(0).Attributes.Add.("onclick","DisableDDL('"& DropDownList3.ClientID & "')")
    
        End Sub

    I am going nuts going over and over and checking other pages. I think this indicates a problem with the aspx.vb file, but have no idea how to fix it.

    Anyone?

     

  • Re: Use RadioButtonList to enable dropdown list

    11-12-2009, 3:35 PM
    • Member
      5 point Member
    • Craigmeister68
    • Member since 07-30-2009, 9:21 AM
    • Charlotte, NC
    • Posts 32

    Another idea I had was to 'eval bind' the Enabled property of the dropdown to the value of the radio button list, but could not figure out the syntax.

    What are your thoughts there?

  • Re: Use RadioButtonList to enable dropdown list

    11-13-2009, 9:51 AM
    • Contributor
      5,014 point Contributor
    • deepthoughts
    • Member since 01-27-2009, 2:55 PM
    • Posts 739

    Craigmeister68:
    Using MS Visual Web Developer and getting message 'Name <control ID> is not declared' in the code on the aspx.vb page for both RadioButtonList1 and DropDownList3. Code: 
     

    Could you try creating a standalone test application with a test db and see its working... Also could you paste the exact error that you are getting.

     

    Thanks.

    MARK AS ANSWER if it helps
  • Re: Use RadioButtonList to enable dropdown list

    11-13-2009, 9:51 AM
    • Contributor
      5,014 point Contributor
    • deepthoughts
    • Member since 01-27-2009, 2:55 PM
    • Posts 739

    Craigmeister68:
    Using MS Visual Web Developer and getting message 'Name <control ID> is not declared' in the code on the aspx.vb page for both RadioButtonList1 and DropDownList3. Code: 
     

    Could you try creating a standalone test application with a test db and see its working... Also could you paste the exact error that you are getting.

     

    Thanks.

    MARK AS ANSWER if it helps
  • Re: Use RadioButtonList to enable dropdown list

    11-17-2009, 5:24 AM

    Craigmeister68:

    Another idea I had was to 'eval bind' the Enabled property of the dropdown to the value of the radio button list, but could not figure out the syntax.

    What are your thoughts there?

     

    Could you please post your relevant code to us checking?

    Gary yang - MSFT
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Use RadioButtonList to enable dropdown list

    11-17-2009, 8:37 AM
    Answer
    • Member
      5 point Member
    • Craigmeister68
    • Member since 07-30-2009, 9:21 AM
    • Charlotte, NC
    • Posts 32

    I decided to change the interface and underlying logic. I also scrapped the aspx and aspx.vb files and copied and pasted the html code into a new aspx file and built the aspx.vb file from scratch. That seemed to resolve the errors regarding the control name not being found (registered?).

    Thanks for everyone's help.

Page 1 of 1 (15 items)