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

时间:2019-03-03 07:47:11

标签: c# asp.net webforms

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

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

1 个答案:

答案 0 :(得分:1)

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

此外,建议提供一个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
       }
    }
}
相关问题