问题描述
我是yogesh sharma.我的问题是我想在输入文本框时显示文本的长度,我已将文本框的autopostback属性设置为true,我在textchanged事件上编写了以下代码.
Hi i yogesh sharma. my problem is that i want to display the length of text as i type in a textbox, i have set the autopostback property of textbox to true, i write the following code on the textchanged event.
protected void txtsubject_Textchanged(Object sender,EventArgs e)
{
textbox1.text=txtsubject.Text.Length.totring();
}
现在,我在textbox1中获取txtsubject.text的长度,但是在txtsubject中的整个文本完成之后(按Tab键之后).
但是我想要textbox1中txtsubject.Text的长度,因为我在txtsubject中进行了相应的键入.
请告诉我我的代码有什么问题.我也将autopostback设置为true.我真的不想要.
请帮助我..朋友....很紧急..我的主页附有带有文本框的页面.因此,请向我建议解决方案...在主页或子页面中以及在哪个标签中使用javascript函数的位置...
NOW iam getting the length of txtsubject.text in textbox1,but after the completion of the entire text in txtsubject(after pressing tab).
But I want the length of txtsubject.Text in textbox1 as i type correspondingly in txtsubject.
Please tel me what''s the problem with my code. I have also set the autopostback to true. which i really does''nt want.
Please help me.. friends....Its urgent..I Have master page attached with my page having textbox. so please suggest me solution... where to use javascript function i:e in masterpage or in child page and in which tag...
推荐答案
<head>
<script type="text/javascript">
function Init () {
var counter = document.getElementById ("counter");
for (var i = 1; i < 1000; i++) {
var option = new Option (i, i);
counter.options.add (option);
}
counter.focus ();
}
function OnKeyPressCounter (event, counter) {
var chCode = ('charCode' in event) ? event.charCode : event.keyCode;
if (chCode == 43 /* + */) {
if (counter.selectedIndex < counter.options.length - 1) {
counter.selectedIndex++;
}
}
if (chCode == 45 /* - */) {
if (counter.selectedIndex > 0) {
counter.selectedIndex--;
}
}
}
</script>
</head>
<body onload="Init ()">
Use the + and - keys to increase/decrease the counter.
<select id="counter" onkeypress="OnKeyPressCounter (event, this)" style="width:80px"></select>
</body>
function ShowTextLength()
{
var objLbl = document.getElementById("lblTextLength");
var objTxt = document.getElementById("txtBox");
if(objLbl && objLbl)
objLbl.innerText = objTxt.value.length;
}
在文本框的Body Load和key up事件上调用此函数,
Call this function on body Load and key up event of text box,
<body önload="ShowTextLength();"></body>
在页面上添加文本框和标签
Add Text Box and Labels on the page
<asp:textbox id="txtBox" runat="server" onkeyup="ShowTextLength();">
<br />
<asp:label id="lblTextLength" runat="server">
在这里,我已使用Label来显示文本框中显示的文本的长度,您可以控制任何控件并根据控件修改javascript函数的最后一行.
Here I have used Label to display length of the text present in text box, you can take any control and modify last line of the javascript function as per control.
这篇关于文本框的Textchanged事件中的Asp.net问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!