client-side code

D

Dan Aldean

Hi,

I try to have some code on the client side of a button, but it does not
work.

<HTML>
<HEAD>
function openWarningPopup()
{
alert("this is a message");
var varProgram = getElementById("TextBox1");
varProgram.text = "HAHAHA";
}
</HEAD>
<BODY>
<asp:Button id="Button1" runat="server" Text="Button"
onload="openWarningPopup"></asp:Button>
</BODY>
</HTML>


ERROR:
Compiler Error Message: CS0117: 'ASP.WebForm1_aspx' does not contain a
definition for 'openWarningPopup'
 
M

marss

Dan said:
Hi,

I try to have some code on the client side of a button, but it does not
work.
.... because you don't read any javascript guide at first.
Sorry for straightforwardness.
Javascript the same programming language as any other, it is too
complicated to study it by trial and error method. :)
 
D

Dan Aldean

I'll be straightforward too:
- this is a forum for ASP.net, not for javascript specialists
- if all participants were specialists this forum would be empty, no
questions - no answers relationship
- I read javascript documentation and like all the developers I have
troubles getting some of the things done
- thanks anyway, you've been helpful
 
M

MikeS

So say you actually surrounded your javascript with script tags

<script>
function openWarningPopup()
{
alert("this is a message");
var varProgram = getElementById("TextBox1");
varProgram.text = "HAHAHA";
}
</script>


And you actually tried to attach that to a client side script handler
instead of a server side script handler.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
Button1.Attributes.Add("onload", "openWarningPopup()")
End Sub

I am not seeing that an input type=button has an onload event.

http://msdn.microsoft.com/library/d...thor/dhtml/reference/objects/input_button.asp

And if you ever get around to actually including a control with an ID
of TextBox1 you might want to set its value property instead of its
text.

http://msdn.microsoft.com/library/d...author/dhtml/reference/objects/input_text.asp

Is there some reason you need to do this when the button loads and not
when the page loads and so use RegisterStartupScript?
 
M

marss

MikeS said:
So say you actually surrounded your javascript with script tags

<script>
function openWarningPopup()
{
alert("this is a message");
var varProgram = getElementById("TextBox1");
varProgram.text = "HAHAHA";
}
</script>


And you actually tried to attach that to a client side script handler
instead of a server side script handler.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
Button1.Attributes.Add("onload", "openWarningPopup()")
End Sub

I am not seeing that an input type=button has an onload event.

http://msdn.microsoft.com/library/d...thor/dhtml/reference/objects/input_button.asp

And if you ever get around to actually including a control with an ID
of TextBox1 you might want to set its value property instead of its
text.

http://msdn.microsoft.com/library/d...author/dhtml/reference/objects/input_text.asp

Is there some reason you need to do this when the button loads and not
when the page loads and so use RegisterStartupScript?


And getElementById is not a global function but document object method.
document.getElementById(...)
 
M

marss

Here is convenient and simple IDE for javascript development with code
syntax checking and context tips(like IntelliSense in Visual Studio).
http://aptana.com
Maybe it will be helpfull.
 
D

Dan Aldean

Thanks Michael.
The code is a little messed up, as I played around trying different
configurations. In fact I tried on the onclick event initially, not onload.
The reason of trying this way is I need some validation on client side
before submitting which causes a post back.
 
M

MikeS

I need some validation on client side before submitting which causes a post back.

I think that OnClientClick is for hooking up a client side function to
a button. Return false from that function to prevent the postback. Or
use the validation controls including the CustomValidator.

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)
If IsPostBack Then
Response.Write(TextBox1.Text)
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script>
function checkIt()
{
var tb = document.getElementById("TextBox1");
var lbl = document.getElementById("Label1");
if (tb.value == "") {
lbl.innerHTML = "Please enter something";
return false;
}
return true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button id="Button1" runat="server" Text="Button"
OnClientClick="return checkIt()"></asp:Button>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>


Note that using document.getElementById on server side controls is not
so good as ID's for server side controls can get decorated and then
their real ID is in their ClientID. So in that case you want to
generate the script block server side and register it.
 
D

Dan Aldean

I tryed this:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="WebApplication5.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">

<script type="text/javascript" language="javascript">
function openWarningPopup()

{
alert("this is a message");
var varProgram = document.getElementById("TextBox1");
varProgram.value = "HAHAHA";

}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server"
OnClick="openWarningButton">
<asp:DropDownList id="DropDownList1" style="Z-INDEX: 101; LEFT: 60px;
POSITION: absolute; TOP: 64px" runat="server"
AutoPostBack="True"></asp:DropDownList>
<asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 292px; POSITION:
absolute; TOP: 69px" runat="server" Text="Button" ></asp:Button>
<asp:DataGrid id=DataGrid1 style="Z-INDEX: 103; LEFT: 54px; POSITION:
absolute; TOP: 127px" runat="server" DataSource="<%# dataSet11 %>"
DataMember="JOBS">
</asp:DataGrid>
<asp:TextBox id="TextBox1" style="Z-INDEX: 104; LEFT: 58px; POSITION:
absolute; TOP: 92px" runat="server"></asp:TextBox>
<asp:Button id="btnTransfer" style="Z-INDEX: 105; LEFT: 468px; POSITION:
absolute; TOP: 78px" runat="server" Text="Next Page"></asp:Button>
</form>
</body>
</HTML>

-----------------------------

and in the .cs
private void Page_Load(object sender, System.EventArgs e)
{
Button1.Attributes.Add("onclick", "openWarningPopup()");
}

the only thig I don't understand now is why it is working when I use the
event in the form
<form id="Form1" method="post" runat="server"
OnClick="openWarningButton">

but not in the button
<asp:Button id="Button1" OnClick="openWarningButton" runat="server"
Text="Button" ></asp:Button>
 
M

MikeS

<form id="Form1" method="post" runat="server"
OnClick="openWarningButton">

If you take out: OnClick="openWarningButton"
Does it still work?
I am suprised that doesn't cause the same kind of compiler error we
started with.
 
D

Dan Aldean

the only thing I did not succeed was to prevent the postback. I tried to
return false, but how is it stopped from doing the server side actions. I
even tried Event.cancel = true;
function openPopupMessage(Button, Event)
.....
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top