sakir
sakir

Reputation: 3502

Declaring asp.net control inside HTML markup

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 />

Upvotes: 1

Views: 732

Answers (3)

Steve B
Steve B

Reputation: 37660

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" />

Upvotes: 2

Alexander
Alexander

Reputation: 2477

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.

Upvotes: 2

ttaaoossuu
ttaaoossuu

Reputation: 7884

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.

Upvotes: 2

Related Questions