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

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
|
|
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\">
|

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
|
|
Hi Mehdi
Add attributes for the textbox control.
(eg) ctlTextbox.Attributes.Add("onChange","javascript:s omefunction();")
|

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

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.
_____________
Mehdi.
software student.
|

April 13th, 2005, 10:46 PM
|
 |
Friend of Wrox
Points: 16,481, Level: 55 |
|
|
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
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
|

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

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 |
|
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |