We help IT Professionals succeed at work.

In C# dot.net master page, how can you disable/enable a text field with a radio button list click?

saabStory
saabStory asked
on
513 Views
Last Modified: 2012-05-08
I'm new to dot.net and am struggling to figure out how to do this in dot.net.  I have a radio button list - button 1 is labeled 'Yes' and button 2 is labeled 'No'.  You click on button 1 and a text box below these is enabled.  Click on button 2 and the text box is disabled and contents cleared.

So far, I've got it reading the value of the button and displaying that in an alert but the javascript I've been trying (document.getElementById('txtProgramName').disabled = false;) is not working.  I don't know if this is due to the master page being used or not and am somewhat stuck and needing help.

Code example is below - thanks so much for any help.


formMaster.master
=================================================================
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="formMaster.master.cs" Inherits="formMaster" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ContentPlaceHolder id="projectInfo" runat="server"></asp:ContentPlaceHolder>
</form>
</body>
</html>  
formMaster.master.cs
=================================================================
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; 
public partial class formMaster : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
			Page.ClientScript.RegisterClientScriptInclude("testScripts.js", ResolveClientUrl("~/js/testScripts.js"));
    }
} 
 
test2.aspx
=================================================================
<%@ Page Title="" Language="C#" MasterPageFile="~/Templates/formMaster.master" AutoEventWireup="true" CodeFile="test2.aspx.cs" Inherits="test2" %> 
<asp:Content ID="projectContent" ContentPlaceHolderID="projectInfo" Runat="Server">
		<fieldset>
			<legend><span>3. Project Information</span></legend>
			<table cellpadding="0" cellspacing="0">
				<tr>
					<td valign="top"><asp:Label ID="lblCampaign" runat="server" Text="Is This Request Part:<br> of a Larger Campaign?"></asp:Label></td>
					<td>
						<asp:RadioButtonList ID="rblCampaign" runat="server">
							<asp:ListItem Value="1">Yes</asp:ListItem>
							<asp:ListItem Value="0">No</asp:ListItem>
						</asp:RadioButtonList>
					</td>
					<td></td>
				</tr>
				<tr>
					<td><asp:Label ID="lblCampaignName" runat="server" Text="Campaign Name:"></asp:Label></td>
					<td><asp:TextBox ID="txtCampaignName" runat="server" Width="300px" Enabled="False"></asp:TextBox></td>
					<td></td>
				</tr>
			</table>
		</fieldset>
</asp:Content>  
test2.aspx.cs
=================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; 
public partial class test2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
			for (int i = 0; i < this.rblCampaign.Items.Count; i++)
			{
				this.rblCampaign.Items[i].Attributes.Add("onclick", "radioButtonItemOnClick(this,'" + this.rblCampaign.Items[i].Value + "')");
			} 
    }
} 
 
/js/jsScripts.js
=================================================================
function radioButtonItemOnClick(elementRef, itemValue) {
	if (itemValue == 1) {
		alert("button 1");
	} else {
		alert("button 2");
		//    	if (elementRef.checked == true) {
		//    		alert(' Value: [' + itemValue + ']');
	}
}

Open in new window

Comment
Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Hmmmm - when I plug that into my js file, I'm getting an error saying that  'document.getElementById() is null or not an object'.
Any idea what I'm doing wrong?

Author

Commented:
Sorry - I posted this, got called down to HR and was laid off.  Totally forgot about this in the backwash of that day.

My sincere apologies for dropping the ball on this - you were right about the clientID - only seems to require that when using master pages.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.