开发者

Access div element from code

I have a div element which I would like to change it'e dir attribute from the code. The problem is that I can't set the runat=server attribu开发者_运维问答te on the div, so it's not accessible from the code.

Using asp.net, c#, framework 4.0.

Any idea?

Thanks.


If you don't want to set the div to runat="server" so you can do the following:

<div dir='<%= GetDir() %>'>
    Your text
</div>

and in the code behind you can set the direction using the following code:

if(You Condition)
        {
            return "ltr";
        }
        else
        {
            return "rtl";
        }


Yes, you can't access to the client elements on the page, only to the server-ones (asp.net is a server-oriented framework). The client controls are compiled to the Literal controls with html in them.

If you don't want to set div for the runat="server" attribute, you can register client script to edit your divs content.
If you set the runat="server" attribute, and set the ID="YOUR_DIV_ID_HERE" for it, it will be accessible from code under YOUR_DIV_ID_HERE name.


If you want you can do this also

<div><%= your server variable %> </div>


Use Jquery and call back function to call server side function.


You can set the runat="server" attribute on a HTML element. Just did a local demo, works just fine. It will be represented as an HtmlGenericControl (http://msdn.microsoft.com/en-us/library/7512d0d0(v=VS.100).aspx).

EDIT: You say you cannot use runat="server" to access the element from code. I am not sure why that would be a problem, as the html rendering should be no different. Maybe it has to do with the elements client side id beeing changed? If you depend on this, you could try setting ClientIdMode=Static for your div.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜