简体   繁体   English

如果按下 html 按钮,如何在页面后面执行 C# 代码?

[英]How to execute C# code behind the page if an html button is pressed?

my current code (that doesn't do anything):我当前的代码(什么都不做):

if (Request.Form["delete"] != null)

and the delete button is generated in the C# behind the page:并在页面后面的C#中生成删除按钮:

public string delete = ""; //in the public partial class

delete += " <button name=\"delete\"/>Delete</button>";

once I click the button - nothing happens, how can I change the if (or the delete which is the button) so that after I press the button the code will execute?一旦我单击按钮 - 没有任何反应,我该如何更改if (或delete按钮),以便在按下按钮后代码将执行?

for example:例如:

when I press the button, a message will appear...当我按下按钮时,会出现一条消息...

if (Request.Form["delete"] != null)
{
Response.Write("inside the if");
}

side note:边注:

I've tried using this code我试过使用这段代码

Button clickedButton = sender as Button;

    if (clickedButton.ID == "delete")

and it didn't work as well.它也没有奏效。

if (Request.Form["delete"] != null)
    {

and put your button in a form .并将您的按钮放在一个form中。

the form : form

<form name="form_name" action="your_page.aspx" method="post" >
<input type="submit" name="delete" value="delete"/>
</form>

the problem?问题? my button wasn't a submit button.我的按钮不是submit按钮。 you HAVE to use type=submit on it so that after it's clicked, it will actually "go to" the form .您必须在其上使用type=submit ,以便在单击它后,它实际上会“转到” form

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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