如何在位于详细信息视图 (ASP.NET) 中的文本框上使用动态字符计数脚本

发布于 2024-11-04 23:41:49 字数 1297 浏览 6 评论 0原文

我想要实现一个字符计数器,显示您在 ASP.NET 网页上的文本框中键入的字符数。该文本框嵌套在DetailsView 中。

下面列出的包括(字符计数脚本)+(文本框的onkeypress)+(只读输入)的正常解决方案不起作用:

脚本:

<script type="text/javascript">

    function textCounter(field, countfield, maxlimit) {
        if (field.value.length > maxlimit)
            field.value = field.value.substring(0, maxlimit);
        else
            countfield.value = maxlimit - field.value.length;
    }

</script>

文本框:

<asp:TextBox ID="TextBox1" runat="server" 
onkeyup="textCounter(TextBox1, this.DetailsView.remLen, 300);"
onkeydown="textCounter(TextBox1, this.DetailsView.remLen, 300);"
TextMode="MultiLine" Text='<%# Bind("idea") %>'></asp:TextBox>

和输入(位于详细信息视图中):

<input readonly="readonly" type="text" name="remLen" value="300" />

然后我 发现嵌套了一个文本框在详细信息视图中搞乱了文本框的命名约定。我尝试了该链接中的 C# 解决方案,但它无法工作...

我怎样才能使其工作? JQuery 似乎是一个答案,但我不知道像 var Text = $("span[id$=TextBox1]"); 这样的代码适合我的文件。需要帮助!

我正在 VB 中使用 Microsoft Visual Web Developer 2010 Express(我是一个菜鸟,主要使用程序的图形 UI,在 Android 上有一些 Java 编程经验)。如果有人有 C# 解决方案,我不会介意。

I want to achieve a character counter that displays the number of characters you've typed in a Textbox on my webpage in ASP.NET. This Textbox is nested in a DetailsView.

The normal solution of including (character count script)+(onkeypress for Textbox)+(Readonly input) as listed below wasn't working:

The script:

<script type="text/javascript">

    function textCounter(field, countfield, maxlimit) {
        if (field.value.length > maxlimit)
            field.value = field.value.substring(0, maxlimit);
        else
            countfield.value = maxlimit - field.value.length;
    }

</script>

The Textbox:

<asp:TextBox ID="TextBox1" runat="server" 
onkeyup="textCounter(TextBox1, this.DetailsView.remLen, 300);"
onkeydown="textCounter(TextBox1, this.DetailsView.remLen, 300);"
TextMode="MultiLine" Text='<%# Bind("idea") %>'></asp:TextBox>

And the Input (which is in the DetailsView):

<input readonly="readonly" type="text" name="remLen" value="300" />

Then I found out that having a Textbox nested in a DetailsView screws up the naming convention of the textbox. I tried the C# solution in that link but it couldn't work...

How can I make this work? JQuery seems to be an answer but I have no idea where some code like var Text = $("span[id$=TextBox1]"); would fit into my files. Need help!

I'm working with Microsoft Visual Web Developer 2010 Express in VB (I'm a noob who uses mainly the graphic UI of the program with some programming experience on Android in Java). Wouldn't mind a C# solution if someone had one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

公布 2024-11-11 23:41:49

如果它位于详细信息视图内,您可能必须使用 FindControl。例子:

<asp:TextBox ID="TextBox1" runat="server" 
onkeyup="textCounter('<%= ((TextBox)myDetailView.FindControl("TextBox1")).ClientID; %>', this.DetailsView.remLen, 300);"
onkeydown="textCounter('<%= ((TextBox)myDetailView.FindControl("TextBox1")).ClientID; %>', this.DetailsView.remLen, 300);"
TextMode="MultiLine" Text='<%# Bind("idea") %>'></asp:TextBox>

If it is inside of a details view you will probably have to use FindControl. Example:

<asp:TextBox ID="TextBox1" runat="server" 
onkeyup="textCounter('<%= ((TextBox)myDetailView.FindControl("TextBox1")).ClientID; %>', this.DetailsView.remLen, 300);"
onkeydown="textCounter('<%= ((TextBox)myDetailView.FindControl("TextBox1")).ClientID; %>', this.DetailsView.remLen, 300);"
TextMode="MultiLine" Text='<%# Bind("idea") %>'></asp:TextBox>
过去的过去 2024-11-11 23:41:49

如果您使用的是 ASP.NET 4.0 或更高版本,则可以将 ClientIDMode 属性设置为 static。这会保留控件的 id,而不是由 .NET 重命名它;例如,LogoImageButton 将被保留,而不是 .NET 将其呈现为 ctl00_LogoImageButton。现在您的 JavaScript 可以找到您的控件。

免责声明:
我在 ContentPlaceHolder 级别设置 ClientIDMode="Static" 以向文本框添加计数器,这样我就知道它可以工作。但根据 Rick Strahl(他是一个非常聪明的人)的说法,您不能使用控件并指定 ClientIDMode。就像我说的,我的实现工作如此...

无论如何,我建议您阅读 Rick 关于该主题的博客,因为他涵盖了一些与继承相关的重要问题。还有一些关于实现同一目标的替代方法的有趣评论。 也许其中一个也可以工作

即使您使用的是早期版本的 .NET Rick 的博客, :http://www.west-wind.com/weblog/posts/2009/Nov/07/ClientIDMode-in-ASPNET-40

IF you are using ASP.NET 4.0 or later you can set the ClientIDMode property to static.This preserves the id of the control rather than having it renamed by .NET; for example LogoImageButton will be preserved instead of .NET rendering it as ctl00_LogoImageButton. Now your JavaScript can find your control.

DISCLAIMER:
I set ClientIDMode="Static" at the ContentPlaceHolder level to add a counter to a text box so I know it works. BUT according to Rick Strahl (who is a very smart guy) you cannot use an control and specify the ClientIDMode. Like I said though, my implementation works so ...

In any case, I suggest you read Rick's blog on the subject because he covers some important issues relating to inheritance. There are also some interesting comments relating to alternative approaches to accomplishing the same objective. Maybe one of them will work even if you are working with an earlier version of .NET

Rick's blog: http://www.west-wind.com/weblog/posts/2009/Nov/07/ClientIDMode-in-ASPNET-40

鹿童谣 2024-11-11 23:41:49

您需要使用文本框的 ClientID 属性在运行时在 JavaScript 中获取它:

<asp:TextBox ID="TextBox1" runat="server" 
onkeyup="textCounter('<%= TextBox1.ClientID %>', this.DetailsView.remLen, 300);"
onkeydown="textCounter('<%= TextBox1.ClientID %>', this.DetailsView.remLen, 300);"
TextMode="MultiLine" Text='<%# Bind("idea") %>'></asp:TextBox>

You need to use the ClientID property of the text box to get it at runtime in the javascript:

<asp:TextBox ID="TextBox1" runat="server" 
onkeyup="textCounter('<%= TextBox1.ClientID %>', this.DetailsView.remLen, 300);"
onkeydown="textCounter('<%= TextBox1.ClientID %>', this.DetailsView.remLen, 300);"
TextMode="MultiLine" Text='<%# Bind("idea") %>'></asp:TextBox>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文