asp.net中后端如何对通过和不通过两种按钮在同一个函数里进行赋值
时间: 2024-03-21 18:38:20 浏览: 55
在ASP.NET中,您可以使用Button控件的CommandName和CommandArgument属性来区分通过和不通过两个按钮,并使用单个事件处理程序来处理它们的单击事件。
以下是一个示例,其中有两个按钮,一个用于通过,另一个用于不通过。在单击这些按钮时,将在后端代码中设置一个名为"status"的隐藏字段,其值将根据按钮的CommandName属性值设置为"approved"或"rejected":
```asp.net
<asp:Button ID="btnApprove" runat="server" Text="Approve" CommandName="approved" />
<asp:Button ID="btnReject" runat="server" Text="Reject" CommandName="rejected" />
<asp:HiddenField ID="hdnStatus" runat="server" />
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
// Check which button was clicked
string command = Request.Form["__EVENTCOMMAND"];
if (command == "approved")
{
hdnStatus.Value = "approved";
}
else if (command == "rejected")
{
hdnStatus.Value = "rejected";
}
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
// Get the value of the hidden field to determine the status
string status = hdnStatus.Value;
if (status == "approved")
{
// Code for approved status here
}
else if (status == "rejected")
{
// Code for rejected status here
}
}
```
阅读全文