views:

115

answers:

2

can I add BehaviorID attribute for asp.net textbox and use it to be recognize by java script??

On other word, I want to to apply some java script function on asp.net text box and I want to let the java script find the asp.net text box by the BehaviorID.

+1  A: 

Sure. In your code-behind:

myTextBox.Attributes.Add("BehaviorID", id.ToString());

The resulting HTML will look something like:

<input type="text" BehaviorID="7" id="myTextBox" (...) />
egrunin
+2  A: 

The TextBox.Attributes.Add will add the attributes correctly but they will not be XHTML compliant. In order to add XHTML compliant attributes you can use ClientScript.RegisterExpandoAttribute method.

protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                BindData(); 
            }

        }

        private void BindData()
        {
           ClientScript.RegisterExpandoAttribute("txtName","BehaviorID",String.Empty);
        }

The above will add the BehaviorID as a JavaScript property instead of adding attribute directly into the TextBox element.

azamsharp
@azamsharp: You aren't entirely correct about it not being XHTML compliant. XHTML provides for a way to specify custom DTD for validation. See examples here (http://www.alistapart.com/articles/customdtd/) and here (http://stackoverflow.com/questions/2413147/are-custom-attributes-ok-in-xhtml).
R0MANARMY
Thanks for the link.
azamsharp