473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

javascript in user control

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[i].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

Nov 19 '05 #1
5 13987
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.

-Brock
DevelopMentor
http://staff.develop.com/ballen
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[i].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


Nov 19 '05 #2
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 Allen wrote:
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.

-Brock
DevelopMentor
http://staff.develop.com/ballen
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[i].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


Nov 19 '05 #3

I tried it but it doesn't work

ch B

*** Sent via Developersdex http://www.developersdex.com ***
Nov 19 '05 #4

HI

How do you access the teller value in the webform?

ch B.
*** Sent via Developersdex http://www.developersdex.com ***
Nov 19 '05 #5


Hi Brock

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

thanks

B.

*** Sent via Developersdex http://www.developersdex.com ***
Nov 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: New User | last post by:
I have a System.Web.UI.UserControl as custom control and I have a javascript block for user control. The problem is I want to bring src attribute from outside as property or other method. e.g...
8
by: rn5a | last post by:
I have gone through a no. of posts in this NewsGroup regarding my problem but alas, couldn't come across one which would have helped me in resolving the issue. My problem is this: An ASPX Form...
4
by: archana | last post by:
Hi all, i am having one user control. what i want is to add javascript which will gets called on button click of user control. but user control is not working if i add javascript in user...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.