470,620 Members | 1,711 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,620 developers. It's quick & easy.

from.submit & server side event handler

Hi,

I would like to submit a form through scripting, and still retrieve the
click event on the server.

See code below to test some stuff.
Create a C# webapplication and replace the WebForm1 class with the code
below.
While running the program, give the textarea the focus and press enter. See
the dif with clicking submit.

public class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
TextBox tb = new TextBox( );
tb.TextMode = TextBoxMode.MultiLine;
string script = "if (event.keyCode == 13) {
document.forms[0].submit(); return false; }";
tb.Attributes["onkeypress"] = script;
this.FindControl("form1").Controls.Add(tb);

Button b = new Button();
b.Text = "Submit";
b.Click +=new EventHandler(b_Click);
this.FindControl("form1").Controls.Add(b);
}

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}

private void b_Click(object sender, EventArgs e)
{
Response.Write("Submit");
}
}
Nov 19 '05 #1
1 1469
You could do this, but it *could* break in future versions of .NET....

1. Make sure your submit button is a server control.
2. Open the page in a web browser, mouse over the link. You will see that
it is calling a javascript method called __doPostBack(...something here...).
You could use that method call in your script to cause the form to post and
the server side click method for that control to execute.

Good luck,

Mike

"Perry van Kuppeveld" <no*****************@spam.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi,

I would like to submit a form through scripting, and still retrieve the
click event on the server.

See code below to test some stuff.
Create a C# webapplication and replace the WebForm1 class with the code
below.
While running the program, give the textarea the focus and press enter.
See the dif with clicking submit.

public class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
TextBox tb = new TextBox( );
tb.TextMode = TextBoxMode.MultiLine;
string script = "if (event.keyCode == 13) {
document.forms[0].submit(); return false; }";
tb.Attributes["onkeypress"] = script;
this.FindControl("form1").Controls.Add(tb);

Button b = new Button();
b.Text = "Submit";
b.Click +=new EventHandler(b_Click);
this.FindControl("form1").Controls.Add(b);
}

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}

private void b_Click(object sender, EventArgs e)
{
Response.Write("Submit");
}
}

Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Jim Hammond | last post: by
3 posts views Thread by Borr | last post: by
4 posts views Thread by ACaunter | last post: by
6 posts views Thread by tshad | last post: by
2 posts views Thread by areef.islam | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.