简体   繁体   中英

Attach Hiddenfield to HTML Markup

I am attaching an object to my aspx page. Since the object's type is determined at runtime, I want to be able to set the value of this object's type parameter to a hiddenfield value which I set when I run my web application.

<asp:HiddenField ID="hiddenField1" runat="server" />

<object id="myObject"
        type="hiddenfield1.value???">
</object>

As you can see in the code snippet above, I need to set the type to the hiddenfield's value. How do I go about this.

Thanks.

I don't know if I understood you correctly but you want to pass the type of an object to the client in a hidden value?

Well I'd do somehting like this:

<asp:HiddenField ID="ObjectType" runat="server" value="<%= typeOf(MyObject).toString() %>" />

I recently uninstalled visual web developer so I can't really tell you if this works or not, but it should.

If this is not what you intended or if it doesn't work, please drop me a comment.

Regards

A couple of thoughts for you:

  1. Make the Object a server control - runat="server" - then in code-behind set the 'type' Attribute to HiddenField1.Value (ie Object1.Attributes["type"] = HiddenField1.Value. I tried this in a quick test but had trouble setting the classid attribute of the object when I set runat="server". Some fiddling may be needed.
  2. Generate the HTML Object tag entirely in code-behind in a string variable then inject it into a Literal control. As you're generating the string variable, reference the HiddenField1.Value. (ie string myObject = "<object id="Object1" classid="..." type='" + HiddenField1.Value + "' ></object" ).
  3. Use JavaScript (or a JS framework) to set the 's 'type' value to that of the rendered hidden field's value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM