Jump to content
Xtreme .Net Talk

What value pass form Radiabuttonlist to (Javascript)function?


Recommended Posts

Posted

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

Posted

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!

3,450,897,223 posts away from crazy...
Posted

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;
	}


Posted

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

Posted

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 :(

Posted

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>

3,450,897,223 posts away from crazy...
Posted

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.

Posted

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 :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...