Results 1 to 18 of 18

Thread: How to hide a table column within a repeater

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2005
    Posts
    32

    How to hide a table column within a repeater

    What I want to do is start by displaying a form with a table within a repeater and on this form have a link to open the same form but with the column I want to hide on the original form. Can you help me hide and unhide table columns within repeaters?

    Thanks!
    James

  2. #2
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: How to hide a table column within a repeater

    Set their CSS Style property "Display" to "None" to hide or to "" to show.

    //Next line gauranteed to be wrong, but should get you
    // on the right track. I am going from faulty memory
    // on properties here.
    myCol.Style["display"] = "none";
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2005
    Posts
    32

    Re: How to hide a table column within a repeater

    Instead of hiding a table column within a repeater I'd like to hide a asp:textbox.

    Here is the code I'm trying to use, but I keep getting this error:
    Object reference not set to an instance of an object.

    Does anyone have a clue why this would be happening?

    Thanks!

    VB Code:
    1. Public Function GetRemoveVisibility()
    2.         If txtHideUnhide.Text = "1" Then ' Or whatever logic you need
    3.             txtHideUnhide.Text = "0"
    4.             Dim control1 As Control = repMainSystem.FindControl("txtPrice1")
    5.             Dim txtbox As TextBox = DirectCast(control1, TextBox)
    6.             txtbox.Visible = False
    7.         Else
    8.             txtHideUnhide.Text = "1"
    9.             Dim control2 As Control = repMainSystem.FindControl("txtPrice1")
    10.             Dim txtbox2 As TextBox = DirectCast(control2, TextBox)
    11.             txtbox2.Visible = True
    12.         End If
    13.     End Function

  4. #4
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: How to hide a table column within a repeater

    repMainSystem.FindControl("txtPrice1")

    Did not find a control named txtPrice1

    Are you sure that there is a control called txtPrice1 and if so, that it is a direct child of repMainSystem???

    If so, can we have the ASPX for the repeater?
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2005
    Posts
    32

    Re: How to hide a table column within a repeater

    Thanks for all your help but I'm still not sure how to do this properly.

    Here is my asp.net and vb.net.

    Code:
    <asp:repeater id="repMainSystem" runat="server">
    	<ItemTemplate>
    		<table width="100%" style="FONT-SIZE: 8pt">
    			<tr>
    				<td valign="top" width="50%">
    					<asp:Label ID = "Description1" Runat = "server" text = '<%# DataBinder.Eval(Container.DataItem,"Description") %>'>
    					</asp:Label>
    				</td>
    				<td valign="top" width="25%">
    					<asp:Label ID = "ItemNumber1" Runat = "server" text = '<%# DataBinder.Eval(Container.DataItem,"ItemNumber") %>'>
    					</asp:Label>
    				</td>
    				<td valign="top" width="5%">
    					<asp:Label ID = "Quantity1" Runat = "server" text = '<%# DataBinder.Eval(Container.DataItem,"Quantity") %>'>
    					</asp:Label>
    				</td>
    				<td valign="top" width="20%" align="right">
    					<asp:Label ID = "Price1" Runat = "server" text = '<%# ChangeColor(DataBinder.Eval(Container.DataItem,"Price")) %>'>
    					</asp:Label>
    					<asp:TextBox Runat="server" ID="txtPrice1" onBlur="add()" Width="100px" />
    				</td>
    			</tr>
    		</table>
    	</ItemTemplate>
    </asp:repeater>
    and on a buttons click this executes:

    VB Code:
    1. Public Function GetRemoveVisibility()
    2.         If txtHideUnhide.Text = "1" Then ' Or whatever logic you need
    3.             txtHideUnhide.Text = "0"
    4.             Dim control1 As Control = repMainSystem.FindControl("txtPrice1")
    5.             Dim txtbox As TextBox = DirectCast(control1, TextBox)
    6.             txtbox.Visible = False
    7.         Else
    8.             txtHideUnhide.Text = "1"
    9.             Dim control2 As Control = repMainSystem.FindControl("txtPrice1")
    10.             Dim txtbox2 As TextBox = DirectCast(control2, TextBox)
    11.             txtbox2.Visible = True
    12.         End If
    13.     End Function

  6. #6
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: How to hide a table column within a repeater

    This is happening becuase the ItemTemplate isn't a static object at run time. It is created dynamically and thus does not persist through post backs generally.

    First, you usually have to re-databind any controls with such behaviour before attempting to read them again, and even not, you cannot perform the FindControl on the parent object. You need to iterate through each row of the repeater and perform it on the row itself. At the row level, the actual control can usually be found.

    For a datagrid, there is a dedicated event handler with an object EventArgs that is usually assigned arbitrarily to a variable named "e" and then you use your find control like this:
    e.Item.FindControl("name")

    But you don't do the FindControl on the datagrid itself. It will not find it.

    Same for repeaters, though the specific context is less familiar to me as I generally do not use them.

    As for your earlier question about hiding a table column, the question was slightly misleading. Seeing your code now, I can answer that one like this:

    The TD for the last column can also be runat="server" and given a name="something" and then using the same findcontrol logic, you can set the TD visible property to false and it will hide the whole column.

    Usually, the definition for
    <TD runat="server" name="name" /> has a server side identity in a namespace similar to (sorry I dont have the dev editor on hand atm) System.Web.UI.WebControls.Html.HtmlTableCell

    or close to that.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2005
    Posts
    32

    Re: How to hide a table column within a repeater

    The form I'm working with has three panels. The code that I've posted is on my third panel, so I'd like to hide the table column without posting to the server. Is this possible?

    Thanks!

  8. #8
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: How to hide a table column within a repeater

    I dont have a huge amount of time right now to write a concise answer, but yes it is.

    Javascript can change the display property of the style property to "none" or to "" on the panel, using the client side ID of the panel.

    Hope this gets you going in the right direction.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  9. #9

    Thread Starter
    Member
    Join Date
    Jun 2005
    Posts
    32

    Re: How to hide a table column within a repeater

    But sense the control is in a repeater how would you hide the control or column? This is the problem I'm really having. If I put a div around the text box and name it something the repeater make it so that I have 10-20 divs named the same thing. Then the client side code wouldn't work because it wouldn't like the form having multiple divs on one form.

    Any ideas on this?

    Thanks for all your help Lord Rat. You're awesome!

  10. #10
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: How to hide a table column within a repeater

    Ya np. I got a little more time now to work with this.

    Change your TD for the last column to:

    <td valign="top" width="20%" align="right" class="hideable">

    Then, in the HEAD section of your document, add:
    Code:
    <STYLE type="text/css">
          .hideable {display: inline}
    </STYLE>
    <SCRIPT Lang="javascript">
    function swapStyle()
    {
     var sCurrent;
     sCurrent = document.styleSheets[0].rules.item(".hideable").style.display;
     if(sCurrent == "none")
     {
      document.styleSheets[0].rules.item(".hideable").style.display="";
     }
     else{
      document.styleSheets[0].rules.item(".hideable").style.display="none";
     }
    }
    </SCRIPT>
    And then on whatever object you want to toggle the column on or off, add:
    onClick="swapStyle"
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  11. #11
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: How to hide a table column within a repeater

    BTW - the code was tested and works in IE. I didn't test other browsers. If it doesnt work for one or more of them, that is most likely because javascript manipulation of stylesheets directly isnt yet a standard.

    If this is an issue, there is a way to do it directly, but slower.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  12. #12

    Thread Starter
    Member
    Join Date
    Jun 2005
    Posts
    32

    Re: How to hide a table column within a repeater

    Like I said before, You're Awesome Lord Rat!!!!!

    I'll try this out and let you know how it went.

    THANKS!!!!

  13. #13

    Thread Starter
    Member
    Join Date
    Jun 2005
    Posts
    32

    Re: How to hide a table column within a repeater

    The weirdest thing is happening when I put your code in and click the button that executes the swapStyle function. The whole form goes blank like I put the hideable class around everything, but I only put it on the <td> .

    What could this be?
    Last edited by jimmy.hunt; Oct 27th, 2005 at 10:07 AM.

  14. #14

    Thread Starter
    Member
    Join Date
    Jun 2005
    Posts
    32

    Re: How to hide a table column within a repeater

    I just took out all the hidable classes in my form and still when I click the button that executes the swapStyle function everything goes blank. Ugh....

    Could it be that panels or the amount of tags surrounding the tag that has the class="hideable" in it?

    I did get it to work on a form with just a textbox and the button that executes the function. Hiding the textbox like this was not problem thanks to your code, but when I put it in my existing form it hides everything.

    Thanks!
    James
    Last edited by jimmy.hunt; Oct 27th, 2005 at 10:17 AM.

  15. #15
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: How to hide a table column within a repeater

    ok, can you post the entire aspx page then? The one that goes blank.

    It might be a lot of code, but I will do my best. =)
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  16. #16

    Thread Starter
    Member
    Join Date
    Jun 2005
    Posts
    32

    Re: How to hide a table column within a repeater

    it is definitely to much code to post. I think there is 4000 lines on the form because there is 3 actual 4 panels.

    If you can think of any reason for this to be happening let me know.

    Thanks!

  17. #17
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: How to hide a table column within a repeater

    I see.

    Is there only one stylesheet definition?

    You might have to change the stylesheet index.

    Did you make sure that class="hideable" exists only on the TDs for the repeater and no where else?

    If all else fails, you can mail it to me:
    [email protected]

    I should only need the ASPX page and not the code behind.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  18. #18
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: How to hide a table column within a repeater

    In reviewing his code, it appears that the largest source of issue was that apparently, in my code, you cannot index the "rules" object using a string.

    Thus, the following code is more appropriate (for anyone who might be watching/stumble upon this code):
    Code:
    function swapStyle()
    	{
    		var sCurrent;
    		var iIndex;
    		for (iIndex = 0 ; iIndex < document.styleSheets[0].rules.length ; iIndex++)
    		{
    			if(document.styleSheets[0].rules[iIndex].selectorText == ".hideable") break;
    		}
    		if(iIndex == document.styleSheets[0].rules.length)
    		{
    			alert('not found');
    			return;
    		}
    		sCurrent = document.styleSheets[0].rules[iIndex].style.display;
    		if(sCurrent == "none")
    		{
    			document.styleSheets[0].rules[iIndex].style.display="";
    		}
    		else
    		{
    			document.styleSheets[0].rules[iIndex].style.display="none";
    		}
    	}
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width