简体   繁体   English

asp.net textarea换行并防止返回键回发

[英]asp.net textarea line feed and preventing postback on return key

I have an ASP.net web application with numerous pages. 我有一个包含大量页面的ASP.net Web应用程序。 Most of them have Multiline APS.net textbox and standard text box. 它们中的大多数都有Multiline APS.net文本框和标准文本框。 Pressing the return key has unexpected result depending on browsers, page, last control used. 按下返回键会产生意外结果,具体取决于浏览器,页面,最后使用的控件。 It can fire any control either the master or the child page. 它可以激发母版页或子页上的任何控件。 Sometimes hitting the return key even fire the logout control. 有时按下返回键甚至会触发注销控件。

So to prevent this I added: 因此,为防止这种情况,我添加了:

body onkeydown="return (event.keyCode!=13)"

In the the master page body tag as recommended in many places, which works like a charm. 在母版页正文标记中,如很多地方所建议的那样,它的工作原理就像是一种魅力。 However, this cause the the return key not to respond to line feed in text area. 但是,这导致返回键不响应文本区域中的换行。 Users need to be able to insert line feed in Text area ( TextBoxMode.MultiLine) 用户需要能够在文本区域(TextBoxMode.MultiLine)中插入换行符

How can I prevent this behavior? 如何防止这种行为?

You can use this 你可以用这个

window.onload = function() {
    setEvent("text", onKeyDown);
}

function onKeyDown()
{
   return (event.keyCode!=13)
}

Which will cancel return button in only textboxes 这将取消仅文本框中的返回按钮

Found a solution: Keep: 找到了解决方案:保留:

body onkeydown="return (event.keyCode!=13)"

this will disable any post back using Return Key, Then add to any multi line textbox the following attribute: 这将使用回车键禁用任何回发,然后将以下属性添加到任何多行文本框中:

TextBox1.Attributes.Add("onkeyup", "EnterEvent(event,this)");

with the following javascript: 使用以下javascript:

// add Line feed in text area.
function EnterEvent(e,ctl) {
    var keycode = (e.keyCode ? e.keyCode : e.which);
    if (keycode == 13) {

    var st1 = $(ctl).textrange('get', 'start');
    var l = ctl.value.length;
    if (st1 < l) {
        $(ctl).textrange('replace', '\n');
        $(ctl).textrange('set', st1 + 1, 0);
    }
    else {
        ctl.value = ctl.value + '\n';
    }
    }

}

this "manually" insert LF on return key up event. 此“手动”在返回键向上事件中插入LF。

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

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