How to add expando attribute to user control?

I am creating some custom javascript functions in an existing ASP.NET user control. User control must be aware of the property of the control in which it is nested. So, I decided to use the expando attribute, not the global javascript variable, like this:

Page.ClientScript.RegisterExpandoAttribute(Me.ClientID, "validatorsenabled", Me.ValidatorsEnabled)

However, although the expando file is displayed to the client properly, the user control itself does not have the corresponding element in HTML. Thus, the following JavaScript, auto-generated by ASP.NET, gets an "object is null" error:

var ctl00_MainContentHolder_Payment_CreditCardInput1 = document.all ? document.all["ctl00_MainContentHolder_Payment_CreditCardInput1"] : document.getElementById("ctl00_MainContentHolder_Payment_CreditCardInput1");
ctl00_MainContentHolder_Payment_CreditCardInput1.validatorsenabled = "False";

I investigated a bit and found this page , indicating that the implementation of the IWebPart interface can do this, but I tried with no luck.

Is there a way to make usercontrol to display a tag such as a server control? Or is this the only option that converts the whole thing into a server control (which in this case contradicts the texture of the website)?

I am open to other suggestions for declaring a common JavaScript property in a user control, if anyone has any other ideas.

+5
source share
1 answer

I found the best solution indicated after my original

I found a workaround.

(- ), expando . , ASP.NET, JavaScript , ( enabled errormessage)? , . Page.ClientScript.RegisterExpandoAttribute() - , HTML, () javascript. HTML DOM, HTML.

, . 2 usercontrol - , usercontrol ( usercontrol ), , :

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="CreditCardInput.ascx.vb" Inherits="BVModules_Controls_CreditCardInput" %>
<asp:Literal ID="litControlBegin" runat="server" />

<!-- User Control Content Here -->

<asp:Literal ID="litControlEnd" runat="server" />

codebehind :

Me.litControlBegin.Text = String.Format("<div id=""{0}"" class=""creditcardinput"">", Me.ClientID)
Me.litControlEnd.Text = "</div>"

HTML, (ClientID) UserControl. expando - usercontrol. expando :

Page.ClientScript.RegisterExpandoAttribute(Me.ClientID, "validatorsenabled", Me.ValidatorsEnabled)

, HTML, . , usercontrol, , , .

, , , . - Render ( ) HtmlTextWriter .

Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
    writer.AddAttribute(HtmlTextWriterAttribute.Id, Me.ClientID)
    writer.RenderBeginTag(HtmlTextWriterTag.Div)
    MyBase.Render(writer)
    writer.RenderEndTag()
End Sub

- DIV usercontrol. , , Page_Load, , .

expando ( javascript) usercontrol:

Page.ClientScript.RegisterExpandoAttribute(Me.ClientID, "validatorsenabled", Me.ValidatorsEnabled)
+2

All Articles