Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4015550
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:37:58+00:00 2026-05-20T09:37:58+00:00

Main issue: Adding to all(!) textboxes in a MVC2 solution/project a js/jquery method. Basics:

  • 0

Main issue: Adding to all(!) textboxes in a MVC2 solution/project a js/jquery method.
Basics: How to call an ascx template?
Solved, see here
(Thanks to Darin Dimitrov!)

BUT…

Means the ascx template “Test.ascx” will be called like here shown:

<%: Html.EditorFor(model => model.Firstname, "Test", new { style = "float: left; width: 4.1em;", maxlength = "4" })%>

The “Test.ascx” template adds the js/jquery function.
Works fine, np so fare.
But: The “original” attributes style="..." maxlength="..." will be lost/overwritten.

My current solution / my “Test.ascx” file:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<input type="text" 
         id="<%: ViewData.TemplateInfo.GetFullHtmlFieldId(string.Empty) %>"
       name="<%: ViewData.TemplateInfo.HtmlFieldPrefix %>"
      value="<%: ViewData.TemplateInfo.FormattedModelValue %>" 
      style="<%: ViewData["style"] %>"
      class="<%: ViewData["class"] %>"
  maxlength="<%: ViewData["maxlength"] %>"
    onkeyup="Foo(this); return false;"/>

This works really fine and returns / renders it perfectly:

<input type="text" 
         id="Firstname"
       name="Firstname"
      value="" 
      style="float: left; width: 4.1em;"
      class=""
  maxlength="4"
    onkeyup="Foo(this); return false;"/>

Note that here are the style="..." and maxlength="..." attributes are well handled!
I use currently the “standard” < input ... /> tag because I didnt get this to work:

<%
    String tmpConfig = " new {onkeyup=\"Foo(this); return false;\"";

    String style = this.ViewData["style"] as String;
    if (!String.IsNullOrEmpty(style))
        tmpConfig += ", style=\"" + style + "\"";

    String @class = this.ViewData["class"] as String;
    if (!String.IsNullOrEmpty(@class))
        tmpConfig += ", class=\"" + @class + "\"";

    String maxlength = this.ViewData["maxlength"] as String;
    if (!String.IsNullOrEmpty(maxlength))
        tmpConfig += ", maxlength=\"" + maxlength + "\"";

    tmpConfig += " } ";
%>

<!-- doesnt work: -->
<%: Html.TextBox(   string.Empty,
                    ViewData.TemplateInfo.FormattedModelValue,
                    // new { onkeyup = "Foo(this); return false;" }    // the style, class, maxlength attributes will be lost / not rendered!
                    // new { tmpConfig }    // result would be the attribut: tempConfig=" onkeyup..."
                    // new tmpConfig        // result: error, tmpConfig isnt object
                    Html.Encode(tmpConfig)  // doesnt work ...
                )%>

This renders finally the same but WITHOUT the style="..." and maxlength="..." attributes!

But I want to use the <%: Html.TextBox(...) %> instead of the < input ... /> tag!
Because this would give me the possibility to avoid an empty attribute like class="" and like before shown.

Where is my mistake?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:37:58+00:00Added an answer on May 20, 2026 at 9:37 am

    You have to supply the Html.TextBox with a dictionary of String,Object to hold all attributes

        <%
        var dict = new Dictionary<string, object>();
    
        String style = this.ViewData["style"] as String;
        if (!String.IsNullOrEmpty(style))
            dict.Add("style", style);
    
        String @class = this.ViewData["class"] as String;
        if (!String.IsNullOrEmpty(@class))
            dict.Add("class", @class);
    
        String maxlength = this.ViewData["maxlength"] as String;
        if (!String.IsNullOrEmpty(maxlength))
            dict.Add("maxlength", maxlength);
    
        dict.Add("onkeyup", "Foo(this); return false;");
    %>
    
    <%: Html.TextBox(   string.Empty,
                        ViewData.TemplateInfo.FormattedModelValue,
                        dict
                    )%>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The main jQuery method takes a second optional argument to provide context for the
I have a really odd issue with an ItemRenderer. I have a main.mxml container
My main JavaScript framework is jQuery , so I would like my unit test
OK, so I had this issue with my jQuery subnav in IE7 where it
I am having an issue with creating and adding sales/quote_address objects to the multishipping
The main web application of my company is crying out for a nifty set
function main() { Hello(); } function Hello() { // How do you find out
The main user-case is: Create the 2D floor plan See the 3D view of
Why main must be declared as if it has external linkage? Why it should
My main browsers (FF and Chrome) are both configured for a 12 pixel font.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.