calvin Posted August 11, 2004 Posted August 11, 2004 Hi, I do a VS.NET (Webform)radiobuttonlist with one dropdownlist and textbox. If I pick "1" then "Enable dropdownlist" and "Disable textbox", "2" "Enable textbox" but "Disable dropdownlist" This is my code: <asp:RadioButtonList id="rbSelection" onclick="abc(this);" runat="server"> <asp:ListItem Value="1">1</asp:ListItem> <asp:ListItem Value="2">2</asp:ListItem> </asp:RadioButtonList> function abc(rad) { switch (rad.value){ case "1": var e=document.getElementById("dropdownlist"); e.style.disabled =true; var t=document.getElementById("textbox"); t.style.disabled ="false"; break; case "2" : var e=document.getElementById("dropdownlist"); e.style.disabled ="false"; var t=document.getElementById("textbox"); t.style.disabled ="true"; break; } } I think the problem should be in <onclick="abc(this);">, Is anyone know do it? Appreciate for any help. :) Calvin Quote
Daspoo Posted August 11, 2004 Posted August 11, 2004 Calvin: Instead of doing things in the .aspx HTML-based part of the WebForm, why not do things in the code-behind page for the WebForm? The steps are simple and don't seem to present any problems: VB.NET code-behind used: ..................................................................................................... Private Sub rbSelection_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbSelection.SelectedIndexChanged Select Case rbSelection.SelectedIndex Case 1 DropDownList.Enabled = False TextBox.Enabled = True Case 2 DropDownList.Enabled = True TextBox.Enabled = False End Select End Sub ..................................................................................................... Hope this helps! Quote 3,450,897,223 posts away from crazy...
Cassio Posted August 11, 2004 Posted August 11, 2004 If you want to do it client side you have to know the names of the radio buttons of your radiobuttonlist. They are usually named with a number after the radiobuttonlist name (rbSelection_1 for example). function abc() { var radios = new Array(2); radios[0] = document.getElementById('rbSelection_0'); radios[1] = document.getElementById('rbSelection_1'); var en; var dis; switch(true) { case radios[0].checked: en = document.getElementById("textbox"); dis = document.getElementById("dropdownlist"); break; case radios[1].checked: en = document.getElementById("dropdownlist"); dis = document.getElementById("textbox"); break; } en.disabled = false; dis.disabled = true; } Quote Stream of Consciousness (My blog)
calvin Posted August 12, 2004 Author Posted August 12, 2004 Object required error! I did follow your code and paste it to the html page. But I get this object required error. As I mentioned before, how to call the function with the object?? The function is good, but what i need to do on the following??I only want to use a radiobuttonlist with few radiobutton(ListItem), not use separetely radiobutton as a group.I dunno how to "Naming" the "radiobutton_0" & "radiobutton_1" in radiobuttonlist. This is what I want: <asp:RadioButtonList id="rbSelection" onclick="javascript:abc(this);" runat="server"> <asp:ListItem Value="1">1</asp:ListItem> <asp:ListItem Value="2">1</asp:ListItem> </asp:RadioButtonList></TD> Not this: <asp:RadioButton id="rbSelection_0" onclick="javascript:abc(this);" runat="server"></asp:RadioButton> <asp:RadioButton id="rbSelection_1" onclick="javascript:abc(this);" runat="server"></asp:RadioButton></TD> Should I add something to the onclick event?? for example: onclick="javascript:abc(this,xxx);" (*XXX <- object??) (I really don't know how to write it. Can you write the full function code :D ) Thank you for helps. Calvin Quote
calvin Posted August 12, 2004 Author Posted August 12, 2004 NOT individual radio button, but radiobuttonlist In fact, I was do a DataList which contain a radiobuttonlist to allow user check either dropdownlist to select the item or textbox to type himself. If I create separate radiobutton, then when I "check dropdownlist in the first DataListItem" and "check textbox in the second DataListItem". It makes the "first DataListItem of dropdownlist" to unselect after I check the "second DataListItem of dropdownlist/textbox" and so on. If I use the radiobuttonlist, the problem won't occur. But can't do javacript function because in "onclick="abc(this,xx(object))"" and the function don't know which object(ListItem in RadioButtonList) I select. For any suggestion and comment will appreciate. Calvin :( Quote
Daspoo Posted August 12, 2004 Posted August 12, 2004 Hope this helps.... <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title>WebForm2</title> <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0"> <meta name="CODE_LANGUAGE" content="Visual Basic 7.0"> <meta name=vs_defaultClientScript content="JavaScript"> <meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <script> function abc() { if (document.form1.rbSelection[0].checked==true) { document.form1.dropdownlist.disabled=false; document.form1.textbox.disabled=true; } else { document.form1.dropdownlist.disabled=true; document.form1.textbox.disabled=false; } } </script> <body MS_POSITIONING="GridLayout"> <form id="form1" method="post" runat="server"> <input type="radio" name="rbSelection" onclick="javascript:abc();" value="1">Enable dropdown, disable textbox<br> <input type="radio" name="rbSelection" onclick="javascript:abc();" value="2">Enable textbox, disable dropdown <br> <br> <br> <select name="dropdownlist"> <option value="1" selected>Default</option> </select> <br> <br> <input type="text" name="textbox"> </form> </body> </HTML> Quote 3,450,897,223 posts away from crazy...
Cassio Posted August 12, 2004 Posted August 12, 2004 Hi! I´m not telling you to use separate radiobuttons controls. You can use a RadioButtonList, but when your page is sent to the client your RadioButtonLinst turns into a table with html RadioButtons inside. And these RadioButtons names follows this pattern, RadioButtonListName_0, RadioButtonListName_1, etc. So these are the names you´ll have to handle in javascript. You can use the code I posted with this page: <asp:RadioButtonList id="rbSelection" onclick="abc();" runat="server"> <asp:ListItem Value="1">1</asp:ListItem> <asp:ListItem Value="2">2</asp:ListItem> </asp:RadioButtonList> <asp:TextBox id="textbox" runat=server ></asp:TextBox> <asp:DropDownList id="dropdownlist" runat=server ></asp:DropDownList> It worked fine here. Quote Stream of Consciousness (My blog)
calvin Posted August 13, 2004 Author Posted August 13, 2004 Not works in Child Control First of all, I'm really appreciate for you helps. I had try you code again, it is works :eek: , but how can I do the "Child control"(In script :confused: ) for the textbox and dropdonwlist inside a "DataList". I was get the "Object Required" Error by this problem :( . Thanks again. Calvin :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.