在webfrom上按Enter键会抛出错误

时间:2014-05-28 07:40:36

标签: c# asp.net button webforms

我在asp.net中使用一个表单我有很多按钮,我希望当我按下输入时它应该默认触发SAVE按钮但是当我按下此页面上的回车然后它会抛出错误:

 The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.

enter image description here

CODE:

asp:TextBox ID="txtName" runat="server" CssClass="textField_width"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName" ErrorMessage="RequiredFieldValidator" ForeColor="#FF3300" SetFocusOnError="True" ValidationGroup="Complaints">Name is Required</asp:RequiredFieldValidator>
        <label>Address</label>
        <asp:TextBox ID="txtAddress" runat="server" CssClass="textField_width"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtAddress" ErrorMessage="RequiredFieldValidator" ForeColor="#FF3300" ValidationGroup="Complaints">Address is Required</asp:RequiredFieldValidator>
        <label>Contact</label>
        <asp:TextBox ID="txtContact" runat="server" CssClass="textField_width"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtContact" ErrorMessage="RequiredFieldValidator" ForeColor="#FF3300" ValidationGroup="Complaints">Contact is Required</asp:RequiredFieldValidator>
        <label>Complainant Type</label>
        <asp:DropDownList ID="DropdwnCompType" runat="server" CssClass="DropDown_Width" AutoPostBack="false" />
          <br />
          <br />


    <span id="Save-Span">
    <asp:Button ID="btnSave"  CssClass="btn btn-success"  runat="server" Text="Save" 
         ValidationGroup="Complaints" ClientIDMode="Static" OnClick="btnSave_Click" />
        </span>

的.cs

protected void btnSave_Click(object sender, EventArgs e)
    {
        SaveComplainant(); //calling a function which saves values in db being entered in textbox
    }

更新:此行应该可以正常工作,但不确定将页面放在哪里。

this.Page.Form.DefaultButton = btnSave.UniqueID;

1 个答案:

答案 0 :(得分:0)

将Panel控件的属性DefaultButton设置为按钮ID。

<asp:Panel DefaultButton="btnSave" ... > ... </asp:Panel>