1

Is it possible to write something like this?

    <input class="form-control" id="name" name="name"
     placeholder='<asp:Label runat="server" ID="lblFormName"></asp:Label>' type="text" required autofocus />
2
  • 4
    Why don't you try by yourself? If you get error, return here with a specific problem. Commented Jan 31, 2014 at 8:50
  • 1
    You'd have more success with an asp:Literal, personally I find it easier to render the whole tag as a HtmlGenericControl Commented Jan 31, 2014 at 8:55

3 Answers 3

2

Solution 1: let ASP.Net render extra attributes

You can use the native TextBox control :

<asp:TextBox runat="server"
             ID="name" 
             required="required" 
             autofocus="autofocus" 
             CssClass="form-control" 
             placeholder="myplaceholder" />

Extra attributes (ones that are not properties of the TextBox class), will be rendered as is:

Html result:

<input name="ctl00$MainContent$name" 
       type="text"  
       id="MainContent_name"  
       class="form-control"  
       required="required"  
       autofocus="autofocus"  
       placeholder="myplaceholder" />

If the generated id must be explicit, you can add CliendIDMode="Static":

<asp:TextBox runat="server" 
             ID="name" 
             required="required" 
             autofocus="autofocus" 
             CssClass="form-control" 
             placeholder="myplaceholder" 
             ClientIDMode="Static" />

Result:

<input name="ctl00$MainContent$name"  
       type="text"  
       id="name"  
       class="form-control"  
       required="required"  
       autofocus="autofocus"  
       placeholder="myplaceholder" />

Solution 2: write your own control

An even better approach is to extend the textbox class:

using System.Web.UI.WebControls;

namespace WebApplication1.Controls
{
    public class TextBoxEx : TextBox
    {

        protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
        {
            if (AutoFocus) writer.AddAttribute("autofocus", "autofocus");
            if (Required) writer.AddAttribute("required", "required");
            if (!string.IsNullOrEmpty(PlaceHolder)) writer.AddAttribute("placeholder", PlaceHolder);

            base.AddAttributesToRender(writer);
        }

        public string PlaceHolder
        {
            get  {
                var obj = ViewState["PlaceHolder"];
                return obj != null ? (string)obj : default(string);
            }
            set { ViewState["PlaceHolder"] = value; }
        }

        public bool AutoFocus
        {
            get {
                var obj = ViewState["AutoFocus"];
                return obj != null ? (bool)obj : default(bool);
            }
            set { ViewState["AutoFocus"] = value; }
        }

        public bool Required
        {
            get {
                var obj = ViewState["Required"];
                return obj != null ? (bool)obj : default(bool);
            }
            set { ViewState["Required"] = value; }
        }
    }
}

Then you can register and use the control:

<%@ Register Assembly="WebApplication1" TagPrefix="local" Namespace="WebApplication1.Controls" %>

....

<local:TextBoxEx runat="server" required="true" autofocus="true" PlaceHolder="my placeholder" />
Sign up to request clarification or add additional context in comments.

Comments

2

You want to assign some value to one of HTML element's properties?

<asp:HiddenField runat="server" ID="lblFormName" />
<input class="form-control" id="name" name="name" placeholder='<%# lblFormName.Value %>' ...

Then you pass lblFormName.Value from CodeBehind.

Comments

2

You cannot declare a single ASP.NET control in a pure HTML page. It must be a ASP.NET page (aspx) which is processed by the server.

2 Comments

Technically, an html page can be processed by asp.net. But it requires extra configuration.
Yes, but in any case the ASP.NET backend is required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.