简体   繁体   中英

Access html control from server side

How can i access html control from code behind in asp.net. I do not want to use "runat=server" as it is causing problems.

I have made an empty html table and from my code behind, I want to add <td> to it.

Please help.

Thanks, Prachi

This is not possible without runat="server".

You say this was causing problems. Please say what problems it was causing.


You say you were accessing the table using getElementById? The ID of a server control changes based on what controls it's inside of. You need to get the changed ID to use:

var tab = getElementById("<%= myTable.ClientId %>");

or something to that effect.

simple example.

just you place the runat="server" then you can access .

html controls are cannot access at server side. if you place runat server then it access in the server.

You should use runat=server attribute if you want to access it at code behind.

But maybe you can generate some javascript code that adds the td/tr to your table at codebehind. and you can register it yo your page to run at startup.

runat="server"

to the table and the tr elements, there is no way the code behind can access them (as this tells the frame work to expose the elements to the server).

What are you actually trying to do? Have you looked at either DataGrid , GridView or simple Repeater control? These allow you to define a table structure and dynamically add controls to it (usually through data binding, although there are ways to add items to thier ItemCollections).

one way that you could do this is to add some inline scripting and a public string variable. in your code behind, make a public class-level variable:

public String myColumns

then in your Page_Load event, create your HTML as a string and save it in your myColumns variable:

protected void Page_Load() {
//do stuff
myColumns = someStringWithTDTagsInIt
}

Then in your .aspx page, do the following:

<table id=maintable"><tr><%=myColumns %></tr></table>

It should render your HTML as you wanted it to in your Page_Load event (or whatever event you want, if you want it to load on a button click or something) without using a "runat=server" tag.

Please note that this is not the recommended way to achieve this (controls are typically more efficient) but this should be a valid solution to the question you posed.

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