Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 1st, 2005, 04:52 AM
Friend of Wrox
 
Join Date: Jul 2004
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,
Code:
<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;
        theform.submit();
    }
// -->
</script>

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

            <span id="Label1">Label</span>
        </form>
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.

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

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

 
Old April 1st, 2005, 07:49 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Santhi,
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.

_____________
Mehdi.
software student.
 
Old April 1st, 2005, 08:48 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

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.

_____________
Mehdi.
software student.
 
Old April 13th, 2005, 10:46 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Mehdi,

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?

-Peter
 
Old April 14th, 2005, 05:46 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

yes,

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.

_____________
Mehdi.
software student.
 
Old May 18th, 2005, 09:11 AM
Registered User
 
Join Date: May 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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






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





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