TiagoM
TiagoM

Reputation: 3526

Change Visibility of Divs from Code Behind (ASPX)

I have a table in html, and inside that table there are multiple rows 'tr', inside each row there are multiple columns 'td'.

What I need it to change between two td's to show inside a particular row.

I have both of them specified in an aspx file, like the example below.

<div id="DirCaDiv1" runat="server" visible="false"> 
    <td class="heading4" align="left" style="width: 21%" id="DirAreaDiv_Text" runat="server" >
        &nbsp;Dir.Área:&nbsp;</td>
</div>


<div id="DirBaDiv1" runat="server" visible="false">
    <td class="heading4" align="left" style="width: 21%" id="DirCoordDiv_Text" runat="server" >
        &nbsp;Dir.Coord.:&nbsp;</td>
</div>

And in my aspx file I have a combo box event (combobox_changed) that change the visibility of each div. It puts one DirCaDiv1.visible="false" and another one DirBaDiv1.Visible="true"

But I don't know why, it doesn't work, I change the item that is selected on the other combobox that is using the event, and nothing happens...

Upvotes: 3

Views: 25450

Answers (4)

SHS
SHS

Reputation: 147

I have had a similar problem, where I could not set a div element visible (divSomeName.Visible=true;), which puzzled me for a while. Then, I found out that the reason why was in the parent element, which Visible property was set to false. I resolved my problem by moving the div element out of the invisible parent.

Upvotes: 0

Jon P
Jon P

Reputation: 19797

Realizing this is a really old question...

First, the HTML is horribly broken, you can't have a div as a parent of a td.

Second, check the OnPageLoad event is not setting the combobox and therefore changing the selected value. Any initialization should take place inside if(!IsPostback){/*Do Your Stuff here*/}

I'm going to set the text of a literal control, this will ensure no additional HTML is rendered to the page. Note there are no div and only one td. This is a good thing! Alternatively you could just set the innerText or innerHtml of the TD directly

ASPX

<td class="heading4" align="left" style="width: 21%" id="DirLoc_Text" runat="server" >
    &nbsp;<asp:Literal runat="server" id="litLocText" />:&nbsp;
</td>

C# Code Behind in the Combobox event handler

litLocText.Text = comboBox.value == [your condition] ? "Dir.Área" : "Dir.Coord";

Upvotes: 1

Akshay
Akshay

Reputation: 11

   <div runat="server" visible='<%# (Eval(condition)) ? true : false %>'></div> 

that works for me

Upvotes: 1

Mike Beeler
Mike Beeler

Reputation: 4101

Actually it's because the visible property is a bool not a string so instead of using "true" or "false" use DirCaDiv1.Visible = true;

See also

http://msdn.microsoft.com/en-us/library/system.web.ui.control.visible(v=vs.100).aspx

Upvotes: 4

Related Questions