p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

aspdotnet_website_programming thread: Trying to implement the Chapter 4 trick (Javascript Prompt ).


Message #1 by "CotasseWrox" <ocotasson@n...> on Thu, 23 May 2002 11:57:23
Hello code-fellows . 

I do appreciate the trick explained in chapter 4 regarding the "Javascript 
Prompt" that eventually triggers a server-side validation (those who read 
the Book know what I am talking about). 

This code snippet is quite handy, I tried to test it on a blank page. 
Unfortunately it doesn't work. Please have a look at my code below. You 
may even test it on your computer (no codebehind or inheritance involved). 

Generated page can be seen at : 
http://hosting.msugs.ch/Cypher/connSQL/Javascript.aspx


Here's what it should do : 

When you click on the "ok" button, the Javascript function tests whether 
the TextBox was filled in.
If not then 
 just start an "alert();" message. 
else
 trigger the __doPostBack('btn2','');
end if 

But the latter part doesn't work...

However if 
-I replace __doPostBack('btn2',''); by my (pure client-side) __doPostBack2
('btn2',''); function it works. 

-I type javascript:__doPostBack('btn2','') in the address bar it also 
works (server-side function). 

-for demo purpose I specified the Text attribute (value=test3) of 
my "btn2" button. When I click this link, it also work. 

Any idea, anyone ? 




Here is the code : 


<%@ Page Language=C#%>
<script runat=server>
	void Page_Load(Object sender, EventArgs e){
		if (!IsPostBack){
		lbl1.Text="Hello!";
		btn1.Attributes.Add("OnClick","javascript:btn2()");

		}
	}
	
	void btn2_Click	(Object sender, EventArgs e){
		lbl1.Text="Click!";
	}
</script>

<html>
<head>
<title></title>

<script language=Javascript>
function btn2(){
	if(document.myForm.txt1.value!=""){
		alert("passe");
		__doPostBack('btn2','');
	}else{
		alert("field cannot be left empty!");
	}
}

function __doPostBack2(zeString,zeVal){
alert("zeString "+zeString);
}
</script>

</head>
<body>

<form id=myForm runat=server>
	<asp:TextBox  id=txt1 runat=server/>
	<BR>
	<asp:Button Text=Ok id=btn1 runat=server/>
	<BR>
	<asp:Label id=lbl1 runat=server/>
	<BR>
	<asp:LinkButton text="test3" ID="btn2" runat="server" 
OnClick="btn2_Click" />
</form>
...


Thanx for reading!


Message #2 by "CotasseWrox" <ocotasson@n...> on Thu, 23 May 2002 14:14:46
For those it may concerns : I found the reason why...

You cannot add Javascript attributes to a Web Control if you wanna use 
this trick. 

Use a normal (i.e. plain) HTML control. 
That is : 
<a href="javascript:Btn2()">This Will work</A> 

http://hosting.msugs.ch/Cypher/connSQL/javascript.aspx



Cheers. 

  Return to Index