简体   繁体   中英

very simple c# submit button

I have several ImageButtons on the top of my page, then I have a textbox and button in the middle. If you type in the textbox then hit enter on the keyboard the browser follows the link of the first ImageButton instead of the submit button next to the textbox. I have ran into this in the pase and had to put the imagebuttons on the bottom of the page for it to work correctly, that is not an ok fix in this case. I tried setting UseSubmitBehavior="true" , but that does nothing. I tried putting the textbox and button in a separate DIV and a separate panel, didn't work either

TOP of the page

      <div style="position:absolute; left: 70% ; top: 5%;">
    <asp:ImageButton ID="imgFB" runat="server" ImageUrl="Images/facebook_icon.jpg"  PostBackUrl="http://www.facebook.com/832586561" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:ImageButton ID="imgLI" runat="server" ImageUrl="Images/linkedin_logo.jpg" PostBackUrl="http://www.linkedin.com/pub/scott-selby/33/304/44a" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:ImageButton ID="imgCB" runat="server" ImageUrl="Images/careerbuilder_logo.jpg" PostBackUrl="http://www.careerbuilder.com" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:ImageButton ID="imgCP" runat="server" ImageUrl="Images/codeplex_logo.jpg" PostBackUrl="http://www.codeplex.com" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</div>

Middle of Page

    <div ID="formPanel" runat="server" style="position:absolute; top:235px;">
         <asp:TextBox ID="txtNewCity" runat="server"></asp:TextBox>
         <asp:Button ID="btnChangeCity" runat="server" Text="Change City" UseSubmitBehavior="true" />
    </div>

You may set the default button via <form/> attribute.

<form defaultbutton="btnChangeCity" id="form1" runat="server">
  ...
</form>

Or use Panel control to set default button.

<asp:Panel ID="Panel1" runat="server" DefaultButton="btnChangeCity">
  <asp:TextBox ID="txtNewCity" runat="server"></asp:TextBox>
  <asp:Button ID="btnChangeCity" runat="server" Text="Change City" />
</asp:Panel>

You surround your controls with <div> which is a good idea, but to have it run at all you also need <form> . In case you just didn't include it, the submit button also has to have OnClick="sub" attribute.

Read more here .

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