繁体   English   中英

关于回发,如何检查后面的代码中哪个html按钮导致回发

[英]On postback, how can I check which html button cause postback in code behind

关于回发,如何检查后面的代码中哪个html按钮导致回发

<button type="submit" name="index" class="btn" />
<button type="submit" name="index" class="btn" />
<button type="submit" name="index" class="btn" />

如果您想知道哪个按钮引起了回发,则必须更改标记,以便每个按钮具有唯一的名称(现在它们都具有相同的名称)。 另外,您必须为每个按钮提供一个值,以检查回退了哪个按钮。

另外,建议您提供一个ID,但是根据您的情况,您仍然可以在不提供ID的情况下了解哪个按钮导致了回发。

为您的情况推荐标记

<form id="form1" runat="server">
    <div>
        <button type="submit" name="index1" class="btn"  value="Button1">Button 1</button>
        <button type="submit" name="index2" class="btn"  value="Button2">Button 2</button>
        <button type="submit" name="index3" class="btn" value="Button3">Button 3</button>
    </div>
</form>

C#代码检查哪个按钮回发了

protected void Page_Load(object sender, EventArgs e)
{
    if(Page.IsPostBack)
    {

       if(Request["index1"] !=null)
       {
           //then first button posted back
           //Request["index1"] will return the value property of the button index1 if it posted back
       } else if(Request["index2"] !=null)
       {
           //then first button posted back
           //Request["index2"] will return the value property of the button index2 if it posted back
       } else if(Request["index3"] !=null)
       {
            //then first button posted back
            //Request["index3"] will return the value property of the button index3 if it posted back
       }
    }
}

暂无
暂无

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

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