Groups | Blog | Home
all groups > asp.net webcontrols > november 2006 >

asp.net webcontrols : client-side code


Dan Aldean
11/29/2006 10:41:28 PM
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'

marss
11/30/2006 6:51:22 AM

[quoted text, click to view]
.... 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. :)
Dan Aldean
11/30/2006 7:45:59 PM
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

[quoted text, click to view]

MikeS
11/30/2006 7:51:25 PM
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/default.asp?url=/workshop/author/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/default.asp?url=/workshop/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?
marss
11/30/2006 11:03:44 PM

[quoted text, click to view]


And getElementById is not a global function but document object method.
document.getElementById(...)
marss
11/30/2006 11:14:01 PM
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.
MikeS
12/1/2006 5:44:11 AM
[quoted text, click to view]

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.
Dan Aldean
12/1/2006 6:45:39 AM
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.

[quoted text, click to view]

MikeS
12/1/2006 8:08:49 PM
<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.
Dan Aldean
12/1/2006 10:48:37 PM
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>

Dan Aldean
12/1/2006 10:48:58 PM
Thanks marss

[quoted text, click to view]

Dan Aldean
12/1/2006 11:31:35 PM
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)
.....

MikeS
12/2/2006 8:53:29 AM
Time to do some reading.

http://www.google.com/search?q=asp%2enet+form+validation
AddThis Social Bookmark Button