简体   繁体   English

启用“多行文本框”以在asp.net Webforms中的Enter键上汇总表单

[英]Enable Multiline Textbox to sumbit the form on Enter Key in asp.net Webforms

I have a multiline textbox with in a asp webforms panel control with a default button to it. 我在ASP WebForms面板控件中有一个带有默认按钮的多行文本框。 I need to submit the form by hitting the Enter (Return) key than going to the next line in textarea. 我需要通过按Enter(返回)键来提交表单,而不是转到textarea中的下一行。 the code is as below: 代码如下:

<asp:Panel ID="pnlChat" runat="server" CssClass="chat-entry" DefaultButton="btnChat">
     <p>Type Your Message and Press Enter</p>
          <asp:TextBox ID="txtChat" runat="server" CssClass="span12" TextMode="MultiLine" Rows="4"></asp:TextBox>
          <asp:Button ID="btnChat" runat="server" Text="Send" CssClass="btn" OnClick="btnChat_Click" />
</asp:Panel>

I tried with the jquery keypress method 我尝试了jquery keypress方法

  $("#<%= txtChat.ClientID %>").keypress(function (e) {
            if (e.which == 13) {
              //  alert('You pressed enter!');
              return false;
            }
        });

But the form is not getting submitted by pressing the enter (return) key. 但是按Enter(返回)键无法提交表单。 Suggestions/solutions are most welcome. 欢迎提出建议/解决方案。

btw: the panel is inside an updatepanel. 顺便说一句:面板在updatepanel内部。

You can have a hidden button outside the UpdatePanel and call that button's click event to submit the form. 您可以在UpdatePanel外部有一个隐藏按钮,然后调用该按钮的click事件来提交表单。

In the markup: 在标记中:

...
</asp:UpdatePanel>
<asp:Button ID="Button1" runat="server" style="display:none" OnClick="btnSubmit_Click" />

In the code: 在代码中:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    //Do whatever you need
}

And in the script: 并在脚本中:

$(document).ready(function () {
    $("#<%= pnlChat.ClientID %>").on("keypress","#<%= txtChat.ClientID %>", function(e){
        if (e.which == 13) {
            var s = $(this).val();
            $(this).val(s + "\n");
            $("#<%= btnSubmit.ClientID %>").trigger('click');
        }
    });
});

Although I don't understand why you need to post the whole form when you are using UpdatePanel. 尽管我不明白为什么在使用UpdatePanel时为什么需要发布整个表单。

if your Multiline textbox is not working then put your buttons from outside of Update panel. 如果您的“多行”文本框不起作用,则将按钮从“更新”面板之外放置。 its work perfectly for me. 它非常适合我。 i just put my asp buttons outside of update panel. 我只是将我的asp按钮放在更新面板之外。 here is code sample when my multiline textbox text not working. 这是当我的多行文本框文本不起作用时的代码示例。

<td style="float: center; padding-left: 155px;">
                                        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                                            <ContentTemplate>
                                                <asp:Button ID="btnPre" OnClientClick="Validate();" ValidationGroup="group1" runat="server"
                                                    CssClass="alt_btn" Text="Preview Your Email" OnClick="btnPre_Click" TabIndex="8" />
                                                <asp:Button ID="Continue" OnClientClick="Validate();" ValidationGroup="group1" runat="server"
                                                    CssClass="alt_btn" Text="Continue" OnClick="Continue_Click" TabIndex="8" />
                                            </ContentTemplate>
                                        </asp:UpdatePanel>
                                    </td>

and when i put my button from out side of update panel then multiline textbox text updating perfectly. 当我从更新面板的外面放按钮时,多行文本框文本会完美更新。

 <td style="float: center; padding-left: 155px;">
                                                <asp:Button ID="btnPre" OnClientClick="Validate();" ValidationGroup="group1" runat="server"
                                                    CssClass="alt_btn" Text="Preview Your Email" OnClick="btnPre_Click" TabIndex="8" />
                                                <asp:Button ID="Continue" OnClientClick="Validate();" ValidationGroup="group1" runat="server"
                                                    CssClass="alt_btn" Text="Continue" OnClick="Continue_Click" TabIndex="8" />
                                    </td>

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

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