Help with client side events Vs. server side events

Hi,

Can someone look as the following script and tell me why marking the check box doesn't generate a client side event eventhough there's:

onclick="checkClick()"? marked.

 Thanks in advance,

Sivan

<%@ Page Language="c#" %>
<HTML>
 <HEAD>
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
 </HEAD>
 <body>
  <SCRIPT language="c#" runat="Server">
 void Page_Load(object s, EventArgs e)
 {
  if (!Page.IsPostBack)
  {
   Label1.Text = Label1.Text + "<P>page_load first time, ";
  }
  else
  {
   Label1.Text = Label1.Text + "<P>page_load postback, ";
  }
 }
 void Page_Unload(object s, EventArgs e)
 {
  Label1.Text = Label1.Text + "Page_Unload, ";
 }
 void buttonServerClick(object s, EventArgs e)
 {
  Label1.Text = Label1.Text + "server button click, ";
 }
 void textServerChange(object s, EventArgs e)
 {
  Label1.Text = Label1.Text + "server text change, ";
 }
 void checkServerClick(object s, EventArgs e)
 {
  Label1.Text = Label1.Text + "server checkbox click, ";
 }
 void checkServerChange(object s, EventArgs e)
 {
  Label1.Text = Label1.Text + "server checkbox change, ";
 }
 void listServerClick(object s, EventArgs e)
 {
  Label1.Text = Label1.Text + "server listbox click, ";
 }
 void listServerChange(object s, EventArgs e)
 {
  Label1.Text = Label1.Text + "server listbox change, ";
 }
  </SCRIPT>
  <SCRIPT language="javascript">
 function buttonClick()
 {
  span1.innerHTML = span1.innerHTML + "button clicked, ";
 }
 function textChange()
 {
  span1.innerHTML = span1.innerHTML + "textbox changed, ";
 }
 function checkClick()
 {
  span1.innerHTML = span1.innerHTML + "checkbox clicked, ";
 }
 function listChange()
 {
 
  span1.innerHTML = span1.innerHTML + "listbox changed, ";
 }
  </SCRIPT>
  <FORM runat="server" ID="Form1">
   Name: <input type="text" onchange="textChange()" onserverchange="textServerChange" id="txtName"
    runat="server" NAME="txtName">
   <P>Profession:
    <SELECT id="lstTitle" onchange="listChange()" onserverchange="listServerChange" onserverclick="listServerClick"
     runat="server" NAME="lstTitle">
     <OPTION selected>Software Engineer</OPTION>
     <OPTION>Software Tester</OPTION>
     <OPTION>Program Manager</OPTION>
    </SELECT>
   </P>
   <P><asp:checkbox autopostback="True" onclick="checkClick()" onserverclick="checkserverclick" oncheckedchanged="checkServerChange"
     runat="server" id="Checkbox1" text="Certified Professional" /></P>
   <P><INPUT type="submit" Value="Save" onServerClick="buttonServerClick" onClick="buttonClick()"
     runat="server" id="Submit1" name="Submit1"></P>
   <p>Client-Side events: <span id="span1" runat="server" />
   </p>
   <p>Server-Side events:
    <asp:label id="Label1" runat="server" /></p>
  </FORM>
 </body>
</HTML>

0
Sivan_r2002
4/13/2007 2:41:06 AM
📁 asp.net.web-forms
📃 93655 articles.
⭐ 6 followers.

💬 7 Replies
👁️‍🗨️ 2649 Views

are you sure it didn't call checkClick()? did you add an alert() inside and make sure a popup showed? cuz I see absolutly nothing wrong with it. 

Liming Xu
Jumptree ASP.NET 2.0 Project Management - With Source Code, Free to Non Profit Org and Effectively Manage Projects/Tasks/Milestones
0
Liming
4/13/2007 4:01:01 AM

You can try to add the below code at page_load

CheckBox1.Attributes.Add("onclick","alert('Checking is working')");

then if it does work then you can call the checkClick function, try to put alert('Testing') at the start of the checkClick() function to see when you call it if it does work or not. if also yes then you have a problem with your javacript code.

HC


Haissam Abdul Malak
MCAD.NET
| Blog |
0
Haissam
4/13/2007 5:48:47 AM

Thanks for your answers but the problem was that the AutoPostBack property was set as TRUE, once I defined it to be FALSE it was fine.

It seems as if the control was sent to the server every time it was clicked so it ignored the client side event- Does it make sense?

 If it does, please explain to me why the page wasn't refreshed every time I checked the chec box when AutoPostBack property was set as TRUE?doesn't it mean the page is sent to the server every time I mark the check box?

 

0
Sivan_r2002
4/13/2007 8:26:39 AM

I have tested the below scenario,

I used a checkbox with ID CheckBox1 and set it's AutoPostBack property to true then use the below line of code in the page_load event

CheckBox1.Attributes.Add("onclick","alert('Checking is working');");

When i run the application and click on the checkbox, the message appeared and after i clicked OK, it was postedback.

Maybe when you had AutoPostBack to true and you used the code, a javascript error happened that is why no postback occured. try to do the steps i mentioned in the start of this post.

HC


Haissam Abdul Malak
MCAD.NET
| Blog |
0
Haissam
4/13/2007 9:52:53 AM

Thanks, but it's still not working, I don't understand how this is possible...

Maybe I'll try running your code and I'll compare it with mine....??

0
Sivan_r2002
4/13/2007 3:23:03 PM
Okay, I ran your code and the javascript was definitly called. I added "alert("is it called")" inside the function.. can't tell you though why the content wasn't updated... as i suspect that autopostback took over before your javascript could complete setting its content.
Liming Xu
Jumptree ASP.NET 2.0 Project Management - With Source Code, Free to Non Profit Org and Effectively Manage Projects/Tasks/Milestones
0
Liming
4/13/2007 4:46:56 PM
This is because the page is posted back, and the server recreate those controls and try to load their value from datas posted to the server.
But span1's value isn't posted, so it's always empty.
0
Raymond
4/17/2007 6:06:24 AM
Reply: