3

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 attribute on the div, so it's not accessible from the code.

Using asp.net, c#, framework 4.0.

Any idea?

Thanks.

3
  • 3
    Why do you say that you can't set runat="server"? As I remember WebForms it's possible (you'll get untyped HtmlControl object). Commented Jul 16, 2011 at 20:21
  • I have a grid unger this div, and I loop on this grid, and changing things on the grid. if this div is set to runat=server, then this loop get messed up. Don't know why.. Commented Jul 16, 2011 at 20:24
  • @Itay.B: Then I suggest you start a separate question to find out why runat=server on the div messes up your loop on the grid. Commented Jul 16, 2011 at 20:32

5 Answers 5

4

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";
        }
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

If you want you can do this also

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

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.