简体   繁体   中英

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

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

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

You will have to change your markup if you would like to know which button caused a postback, so that each button has a unique name ( right now they all have the same name). In addition,you must provide a value for each button in order to check which button posted back.

Also, it's recommended to provide an id, but in your situation you could still get to know which button caused postback without providing an id.

Markup recommended for your scenario

<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# code to check which button posted back

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
       }
    }
}

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