Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enable\Disable button when checkbox is clicked 2

Status
Not open for further replies.

HateASP

IS-IT--Management
May 2, 2005
14
US
I am trying to modify a login page that uses an ASP:Button for submit. I am have added an ASP:checkbox object and am trying to disable the button when the checkbox is not selected. The following is part of the code for the page;

Any help would be greatly appreciated!


<body onload="pageLoad();">
<table width="500" border="0" align="center" cellspacing="0" cellpadding="0">
<tr>
<td>
<img src="contents/images/teamcenter_login_bannerAW.jpg" width="974" height="122" /></td>
</tr>
<tr>
<TD class=ProductText style="background-color: #FF0000">Collaborative
Exchange</TD>
</tr>
<tr>
<td>
<img src="contents/images/logout_text.gif" alt="You are logged out of the system" visible="false" id="logoutImg" runat="server" />
</td>
</tr>
<tr>
<td align="left" style="padding-left: 80px">
<table class="login" border="0">
<tr>
<td valign="bottom" colspan="2" align="right">
<asp:label id="lblMsg" class="normalred" runat="server" />
</td>
</tr>
<tr>
<td valign="bottom" colspan="2" align="right" nowrap>
<noscript>
<font face="arial" size="2" weight="400" color="#ff0000">
<%= GetString("noJavascript")%>
</font>
</noscript>
</td>
</tr>
</table>
<div id="formDiv" style="position:relative;visibility:hidden">
<form name="LogonFrm" id="LoginFrm" method="post" runat="server">
<table class="login" border="0">
<tr>
<td valign="bottom" class="login" nowrap>
<%= GetString("usernameLbl")%>
</td>
<td>
<asp:TextBox id="txtUsername" class="login" runat="server" Width="230" />
<asp:Label id="usernameLbl" class="login" visible="false" runat="server" />
</td>
</tr>
<tr>
<td valign="bottom" class="login" nowrap>
<asp:Label ID="passwordLbl" class="login" Runat="server" />
</td>
<td>
<asp:TextBox id="txtPassword" class="login" runat="server" Width="230" TextMode="Password" />
</td>
</tr>
<tr>
<td valign="bottom" class="login" nowrap>
<asp:Label ID="newPasswordLbl" class="login" Runat="server" />
</td>
<td>
<asp:TextBox id="txtNewPassword" class="login" runat="server" Width="230" MaxLength="14" TextMode="Password" />
</td>
</tr>
<tr>
<td valign="bottom" class="login" nowrap>
<asp:Label ID="confirmPasswordLbl" class="login" Runat="server" />
</td>
<td>
<asp:TextBox id="txtConfirmPassword" class="login" runat="server" Width="230" MaxLength="14" TextMode="Password" />
</td>
</tr>
<tr>
<td></td>
<td align="left" class="login">
<asp:button id="Logon" class="login" runat="server" /><br><br>
<asp:checkbox checked="true" id="checkbox" runat="server" oncheckedchange="Check_Clicked" />
 
VB Code
Code:
If Not Page.IsPostBack Then
 checkbox.Attributes.Add("onclick", "javascript:disableApprove(this.form);")
End If

Javascript
Code:
<script>
function disableApprove(form) {
 if (form.chkApproved.checked) {
  form.Logon.disabled=false;
 }else{
  form.Logon.disabled=true;
 }
}
</script>

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
checkai, I should have noted that the page is written in C#. Will your VB code still work?
 
well, obviously no, but their should be a very similar equivalent...

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Change:
If Not Page.IsPostBack Then
checkbox.Attributes.Add("onclick", "javascript:disableApprove(this.form);")
End If

To:
if(!IsPostBack)
checkbox.Attributes.Add("onclick", "javascript:disableApprove(this.form);")

The rest is the same.
 
I know I couldnt use the exact code, was just wondering if C# had similar code.

Thanks for your help checkai
 
Veep,

Does it matter where in the page I add the c# code. I put it right after the ASP:Checkbox code and I am receiving the error "ispostback" is undefined. THis doesnt need to be declared, does it?
 
Stick it in the Page_Load(). All you're doing here is injecting it into the form once within your <input type="checkbox"> tags.
 
At the top...have you ever created an asp.net web page before?

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
No, I have never even looked at ASP.net code until 1 week ago when I was asked if I could modify this page. We do not have any ASP programmers in our office. I have minimal coding experience in Vb and scripting.
 
I see...are you using visual studio? or just coding within the .aspx page?

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
I still receive 'ispostback is undefined'

<%@ Page language="c#" Codebehind="Login.aspx.cs" AutoEventWireup="false" Inherits="EDSPLM.Tc.Authentication.Login" %>
<HTML>
<HEAD>
<link rel="stylesheet" type="text/css" href="Contents/Css/portal.css">
<title>
Autoweb Collaboration Exchange
</title>
<script language="javascript">
function pageLoad()
{
var formDiv = document.getElementById("formDiv");
formDiv.style.visibility = 'visible';

var userName = document.getElementById("txtUsername");
var userPass = document.getElementById("txtPassword");


if(userName && !userName.disabled)
userName.focus();
else if(userPass)
userPass.focus();

if(!IsPostBack)
checkbox.Attributes.Add("onclick", "javascript:disableApprove(this.form);")
}
window.onresize = pageLoad;
</script>
 
you're intermingling javascript and c#...the c# stays outside of the the <script> tags I believe...needs to be within <% %> tags...

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Yikes!
OK, you can take this:
if(!IsPostBack)
checkbox.Attributes.Add("onclick", "javascript:disableApprove(this.form);")

out of that JavaScript function. The JavaScript function that checkai gave you belongs between those <Script Language="JavaScript"></Script> tags.

If you're using VisualStudio...change to Design View and double-click on the form. This will bring up the code-behind file. There should be a:
Code:
private void Page_Load(object sender, System.EventArgs e)
{
    if(!IsPostBack)
     checkbox.Attributes.Add("onclick", "javascript:disableApprove(this.form);")

}
stick the code snippet in there.
 
OK....where did they hide design view in VS.NET. I know in 6 it was just a tab, but I have been thru every menu and there is nothing for design view....
 
should be on the bottom left of your html view...

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Thats what I thought....but there is only an 'Output' tab
 
Is there an alternative way to access the Page_Load code other than design view? I can see design view in other projects, but for some reason it is not an option on this aspx page.
 
When you open the document is there a corresponding .cs file...for instance...you opened page1.aspx is there a page1.cs?

if so that would be it...

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top