from.submit & server side event handler

  • Thread starter Perry van Kuppeveld
  • Start date
P

Perry van Kuppeveld

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");
}
}
 
M

Mike Hacker

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top