javascript in user control

J

Jimmy

Hi

I am trying to check whether checkboxes are checked in my user control. I
have problems getting the right reference to the checkboxes:


<%@ Page language="c#" Codebehind="WebForm2.aspx.cs"
AutoEventWireup="false" Inherits="javascriptusercontrol.WebForm2" %>
<%@ Register TagPrefix="uc1" TagName="WebUserControl1"
Src="WebUserControl1.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<script>

function checkit2(field)
{
var teller=0;
for (i = 0; i < 2; i++)
{
if (field.checked == false)
{
teller = teller+1;
}
}
if (teller==2)
{
alert("no good");
}

}

</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<input type="button" name="c5"
onclick="checkit2(document.Form1['uc:list'])" style="Z-INDEX: 101; LEFT:
248px; WIDTH: 128px; POSITION: absolute; TOP: 16px; HEIGHT: 24px"
value="check if checked">
<uc1:WebUserControl1 id="WebUserControl11"
runat="server"></uc1:WebUserControl1>
</form>
</body>
</HTML>


usercontrol code:

<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="WebUserControl1.ascx.cs"
Inherits="javascriptusercontrol.WebUserControl1"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>

<asp:CheckBox ID="cb1" Runat="server" name="list"></asp:CheckBox>
<asp:CheckBox ID="cb2" Runat="server" name="list"></asp:CheckBox>

what could be wrong?

ch Beffmans
 
B

Brock Allen

You should dynamically fill in those IDs from the Control.ClientID proprty
on the control. So where you're hard coding "document.Form1['uc:list']",
change that to dynamically incorporate List.ClientID.
 
C

Clint Hill

Brock is right. However, a more elegant approach is to change the way
you are using JavaScript a bit.

For your UserControl:

<%@ Control Language="c#"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<script>
var teller=0;
function OnCheckBox_Clicked(checkbox)
{
if(checkbox.checked && teller < 2)
{
teller = teller+1;
}
if(!checkbox.checked && teller > 0)
{
teller = teller-1;
}
}
</script>
<script language=C# runat=server>
void Page_Load(object sender, System.EventArgs e)
{
cb1.Attributes["onClick"] = "OnCheckBox_Clicked(this)";
cb2.Attributes["onClick"] = "OnCheckBox_Clicked(this)";
}
</script>
<asp:CheckBox ID="cb1" Runat="server"></asp:CheckBox>
<asp:CheckBox ID="cb2" Runat="server"></asp:CheckBox>


And on your web form change the onclick of the input type to this:

onclick="javascript:if(teller==2)alert('No Good');"

With this approach you are letting the click event of the check box
dictate the value of teller and not the click event of the button. You
may consider making the checkboxes server side events so the teller
value can be more global (i.e. saved to a database maybe).

Clint Hill
H3O Software
http://www.h3osoftware.com

Brock said:
You should dynamically fill in those IDs from the Control.ClientID
proprty on the control. So where you're hard coding
"document.Form1['uc:list']", change that to dynamically incorporate
List.ClientID.




Hi

I am trying to check whether checkboxes are checked in my user
control. I have problems getting the right reference to the
checkboxes:

<%@ Page language="c#" Codebehind="WebForm2.aspx.cs"
AutoEventWireup="false" Inherits="javascriptusercontrol.WebForm2" %>
<%@ Register TagPrefix="uc1" TagName="WebUserControl1"
Src="WebUserControl1.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<script>
function checkit2(field)
{
var teller=0;
for (i = 0; i < 2; i++)
{
if (field.checked == false)
{
teller = teller+1;
}
}
if (teller==2)
{
alert("no good");
}
}

</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<input type="button" name="c5"
onclick="checkit2(document.Form1['uc:list'])" style="Z-INDEX: 101;
LEFT:
248px; WIDTH: 128px; POSITION: absolute; TOP: 16px; HEIGHT: 24px"
value="check if checked">
<uc1:WebUserControl1 id="WebUserControl11"
runat="server"></uc1:WebUserControl1>
</form>
</body>
</HTML>
usercontrol code:

<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="WebUserControl1.ascx.cs"
Inherits="javascriptusercontrol.WebUserControl1"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>

<asp:CheckBox ID="cb1" Runat="server" name="list"></asp:CheckBox>
<asp:CheckBox ID="cb2" Runat="server" name="list"></asp:CheckBox>

what could be wrong?

ch Beffmans

 
B

Beffmans

Hi Brock

Can you put some sample code here , that makes it easier for me..

thanks

B.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top