简体   繁体   中英

C#: Html Form data on button onClick C# code

I want to send my HTML Form Action url to post by the C# pragmatically not by the HTML code on click of button action..

<form action="url" method=POST runat="server"></form>

What I am trying is

 <form id="form" method="POST" runat="server">

and in C# code

 protected void Button2_Click(object sender, EventArgs e)
    {

    HtmlForm form = (HtmlForm)this.FindControl("form");

      form.Attributes.Add("action", "url");

    }

This does not trigger the action method but url gets added.. how do i trigger call from C# code

Instead of trying to change your form action, you can use "Server.Transfer" to post the form data to a different form:

Server.Transfer(url);

If you don't need to post the form data to the next form and just want to change to a separate URL, use "Response.Redirect" to redirect the browser to whichever page you intend:

Response.Redirect(url);
<!DOCTYPE html>
<html>
<body>

<button type="button" onclick="THIS IS WHERE YOU PUT THE CODE">Send Info</button>
<p>This is the code:
<!DOCTYPE html>
<html>
<body>

<button type="button" onclick="alert('')">Send Info</button>
</p>
<p> If this code doesn't show, inspect it...

</body>
</html>

</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM