Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I access object tags in aspx.cs page?

I desingned a page with tags, Now I want to access object tags in code behind.

This is aspx page code....

<object type="label" runat="server" class="System.Web.UI.WebControls.Label" id="label_priority" parent="-1" bindedfield="priority" empty="1" value="MyValue">

Here I am adding runat=server in object tags it is giving error as

"An object tag must contain a Class, ClassID or ProgID attribute."

then I added class="System.Web.UI.WebControls.Label", now not giving any error but not showing anything in browser.

so My question is how do I access object tags in aspx.cs page? or I want to create a label with object tag that is accessible in code behind.

Sujeet

like image 282
user641158 Avatar asked Oct 11 '22 15:10

user641158


1 Answers

When you have runat="server" on a <object/> or <script/> tag, .NET expects it to be a server side object. You can however create the entire markup on the server side. Assuming you have <div id="somecontainer" runat="server"></div> in your page:

protected void Page_Load(object sender, EventArgs e) 
{
    HtmlGenericControl myObject = new HtmlGenericControl();
    myObject.TagName = "object";
    myObject.Attributes.Add("data", "somevideo.mpeg");
    Page.FindControl("somecontainer").Controls.Add(myObject);
}

The result is:

<div id="somecontainer"><object data="somevideo.mpeg"></object></div>

You can use the same method to add elements inside the object, for instance <param/> tags.

like image 166
Pål Thingbø Avatar answered Nov 15 '22 10:11

Pål Thingbø