Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
Password Reminder
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 1st, 2005, 04:52 AM
Friend of Wrox
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default Firing server side events at client side codes

Hi Experts,

I have a simple WebForm it has a TextBox

my TextBox has OnChange Event and its AutoPostBack property is true,

now what I want to accomplish is to make a Button to raise this event for me at client side(with javascript)

when I view the source of my ASP.NET page it is something like bellow,
<script language="javascript">
    function __doPostBack(eventTarget, eventArgument) {
        var theform;
        if (window.navigator.appName.toLowerCase().indexOf("netscape") > -1) {
            theform = document.forms["Form1"];
        else {
            theform = document.Form1;
        theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
        theform.__EVENTARGUMENT.value = eventArgument;
// -->

            <input type="submit" name="Button1" value="Button" id="Button1" />
            <button id="mahdi" type="button" language="javascript" onclick="document.Form1.TextBox1.onchange();">
            <input name="TextBox1" type="text" onchange="__doPostBack('TextBox1','')" language="javascript" id="TextBox1" />

            <span id="Label1">Label</span>
what I did ,I made a simple button(runat=server) and for its onclick I execute the TextBox onchange attribute

this is the tag I used for my button to raise the server side event,

<button id="mahdie" type="button" onclick="document.Form1.TextBox1.onchange();">

now when I test my page the OnChange event at server side doesn't fire

any opinion?

Thanks in advance.

software student.
<font color=\"teal\"><font size=\"1\"><b>Mehdi.
software student.</b></font id=\"size1\"></font id=\"teal\">
  #2 (permalink)  
Old April 1st, 2005, 06:52 AM
Friend of Wrox
Join Date: Oct 2003
Location: Chennai, TamilNadu, India.
Posts: 326
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Santhi Send a message via MSN to Santhi

Hi Mehdi
    Add attributes for the textbox control.
(eg) ctlTextbox.Attributes.Add("onChange","javascript:s omefunction();")

  #3 (permalink)  
Old April 1st, 2005, 07:49 AM
Friend of Wrox
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post

Thanks for the reply,
what I wanted is somewhat strange,

I want to raise TextBox1_TextChanged event(server side event) when user clicks on Button
at client-side codes(I mean I want to launch a server side event from the client side codes)

this is the tag I use in my ASP.NET page
<button id="mahdi" type="button" language="javascript" onclick="document.Form1.TextBox1.onchange();">

when user clicks on the button document.Form1.TextBox1.onchange() will be executed
so __doPostBack('TextBox1','') would be executed so there would be a postback but
when this postback occures just Page_Load fires other server side events like TextBox1_TextChanged doesn't fire.

software student.
  #4 (permalink)  
Old April 1st, 2005, 08:48 AM
Friend of Wrox
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post

figuroud out,
well,it was clear,because I didn't change the value of textbox

<button id="mahdi" type="button" language="javascript"
onclick="document.Form1.TextBox1.value='somethingn ew';document.Form1.TextBox1.onchange();">

now TextBox1_TextChanged event is fired.

software student.
  #5 (permalink)  
Old April 13th, 2005, 10:46 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts


Despite the appearance that the onChange event should result in the server-side event being fired, it doesn't. The server event fires when it sees an actual change in the textbox value as you've found.

I'm not clear on what you are trying to accomplish with this button, but why not use a regular asp:button that would supply its own postback event?

  #6 (permalink)  
Old April 14th, 2005, 05:46 AM
Friend of Wrox
Join Date: Jul 2004
Location: Tehran, , Iran.
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post


because asp:button is rendered to submit button and there was no onclick attribute

<input type="submit" name="Button" value="Button" id="Button" />

so after that I added an onclick attribute to my asp:button

Button.Attributes.Add("onclick","__doPostBack('But ton','')");

so it would be rendered

<input type="submit" name="Button" value="Button" id="Button" onclick="__doPostBack('Button','')" style="height:0px;width:0px;"/>

I worked with button click event instead of text changed event.

software student.
  #7 (permalink)  
Old May 18th, 2005, 09:11 AM
Registered User
Join Date: May 2005
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts

Well, if what you want is to have the button trigger your onchange event server-side, do something like the following (syntax may be slightly different...programming from memory)

private function mytextbox_TextChanged(sender as System.Object, e as System.EventArgs) handles mytextbox.TextChanged
    ' do my text change stuff here
end function

private function mybutton_Click(sender as System.Object, e as System.EventArgs) handles mybutton.Click
    ' Call the textbox's onchange event
    mytextbox_TextChanged(sender, e)
end function

What this does is when the button is pressed, it calls the textbox's textchanged event. It technically does not raise the textchanged event, but hey...it works. :D


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem Converting Client-side to Server-side Code kwilliams ASP.NET 2.0 Professional 1 November 21st, 2007 05:25 PM
sharing a server-side variable with client-side pigtail Javascript How-To 6 November 4th, 2004 02:01 AM
Two Client Side vs Server Side issues Milo Classic ASP Professional 5 May 25th, 2004 02:47 PM
Accessing Server Side Data on Client Side steve456 Classic ASP Professional 3 October 15th, 2003 02:33 PM

All times are GMT -4. The time now is 03:53 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.