Solved

How to Hide a Dalalist in a <tr> or <td> using asp:panel ? (VB.NET)

Posted on 2004-09-07
26
510 Views
Last Modified: 2008-02-01
Hi,
I have a Category datalist in my 1st page.
Once the user clicks on a list item, the user is sent to the 2nd page.
In 2nd page, I have 2  datalists: one for SubCategories, the other for terms for the chosen Category (Theses terms are direct childs of the Category, as are the SubCategories).

The problem is: Certain Categories might not have any SubCategories, they will have just the Terms listed under them, in the 2nd page.
While certain Categories will have some dirent SubCategories and some direct Terms of the Chosen Category.

I need to be able to hide the SubCategory datalist, alongwith an additional label (or Html Text) if there are no SubCategories.

I understand that if there is no SubCategory, the datalist will not be shown when I receive the QueryString and pass it to the SubCategory datalist.

My problem is I am using two <tr>, one for just displaying the text ""SubCategories""
and the other for the subcategory datalist.

I saw somewhere that I can use panel to hide information. But how can I use panel for two <tr>s ?

Please let me know.

Thx.
_Esam.
0
Comment
Question by:_Esam
  • 13
  • 13
26 Comments
 
LVL 8

Expert Comment

by:shovavnik
ID: 12003786
You can make your tr a server control by giving it an id and setting it's runat=server attribute.

Then on your server-side, you can hide it:

MyFirstRow.Visibile = false
MySecondRow.Visibile = false

On the server-side these will be represented as HtmlTableRow objects.
0
 

Author Comment

by:_Esam
ID: 12003858
Hi shovavnik,
Nice to hear from you again. :)
If I understand correct, I can simply user the <tr> properties to be set to Visible=false?
And this can be done for both <tr>s? and declare them as server control?
If this is the fact, then I actually don't need to use any panel for this matter. :)
I can then, just set the MyFirstRow.Visible = True in the code-behind if the query return
results?

Now I have another issue...
How can I make sure that there are results to be shown, and set the <tr>s visibility to true?

Please let me know.
In the mean time, I will be implementing your suggestions, and get back to this question soon.

Thx.
_Esam
0
 

Author Comment

by:_Esam
ID: 12003882
I believe I misunderstood your answer.
I can give the <tr> and id and server attribute.
But there is no visible? attribute.
Where exactly I declare them as r1.visible = false?
You said server side...I am confused.
Please explain...

Thx.
_Esam.
0
 
LVL 8

Expert Comment

by:shovavnik
ID: 12003942
Hey _Esam,

The HtmlTableRow class does have a visible attribute:
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWebUIHtmlControlsHtmlTableRowMembersTopic.asp?frame=true

However, if that doesn't work, set the style.  The control will be rendered to the client (unlike when Visible=false).

MyFirstRow.Style[ "display" ] = "none";

Basically, to determine when to show the rows, you need to check the number of results.

if( MyDataTable.Rows.Count == 0 ) MyFirstRow.Style[ "display" ] = "none";
else MyFirstRow.Style[ "display" ] = "block";

Let me know if that works.  The Visible property is preferred because it will prevent rendering the row altogether.  (That is, it won't be visible in View Source.)
0
 

Author Comment

by:_Esam
ID: 12003978
Yes, you are right, there is visible property for an HtmlTalbeRow or Cell.
But I believe I misused it.
I just tried to use it for the <td> element, but I had some error saying that:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 32:         Dim gcat As String
Line 33:         Dim subcategory_1 As HtmlTableCell
Line 34:         subcategory_1.Visible = False  'Error line

I set it up like this, and then simply appended the Visible=true at the end of the query.

I am not sure how shoud I instantiate this object.

Please let me know..

Thx.
_Esam
0
 
LVL 8

Expert Comment

by:shovavnik
ID: 12004037
I understand.

1. In your aspx file, give your row the name you want, say "SubCatRow".

<tr id="SubCatRow" runat="server"></tr>

In your .vb file, where VS.NET automtically adds all the references to objects on the page, you should have an object that looks like

Dim SubCatRow as HtmlTableRow

If you don't see it, add it.
Then in you code, try setting the visible property.

If MyDataTable.Rows.Count == 0 Then SubCatRow.Visible = false
else SubCatRow.Visible = true

The problem you're sdeeing is that you only declared the HtmlTableCell, but didn't instantiate it (that is, you didn't create it), so it's null.
Also, your objects in the code behind need to match up with the objects in your page, so they need to be global members of your class in this case.
0
 

Author Comment

by:_Esam
ID: 12004059
I am probably not being clear:

Here are my two <tr> or <td> elements.

<tr>
 <td align="left" id="subcategory_1" runat=server>
      <h3><b>Sub Categories</b></h3>
 </td>
</tr>

<tr>
   <td><asp:datalist id="SubCatList" runat="server" OnItemCommand="GetTerms">
     <ItemTemplate>
        <asp:LinkButton id="SubCategoryType" Runat="server" Text= '<%  
        #Container.DataItem("GSC_Name")%>' CommandName= '<%
        #Container.DataItem
        ("GSC_ID")%>' />
    </ItemTemplate>
  </asp:datalist>
 </td>
</tr>

All I need to do, it to hide these <tr>s or <td>s if there are so subcategories.

Thx.
_Esam
0
 

Author Comment

by:_Esam
ID: 12004087
I actually ended up using a dataset for my query result as:

Dim ds As New DataSet
                da.Fill(ds, "G_SUB_CATEGORY")
                SubCatList.DataSource = ds
                SubCatList.DataMember = "G_SUB_CATEGORY"
                SubCatList.DataBind()
                cn.Close()

If this is the case, How do I use the test condition for visibilty?
I really don't want to go back to using DataTable unless there is no way to count results...

Thx.
_Esam
0
 
LVL 8

Expert Comment

by:shovavnik
ID: 12004475
The dataset is fine.  When you bind to the dataset, it's actually the first table in the dataset that's bound to the control.

So your check can be like this:

if ds.Tables(0).Rows.Count = 0 Then ...

OR:

DataTable MyDataTable = ds.Table(0)
if( MyDataTable.Rows.Count == 0 ) ...
0
 
LVL 8

Expert Comment

by:shovavnik
ID: 12004476
Sorry.  In VB:

Dim MyDataTable as DataTable = ds.Table(0)
if MyDataTable.Rows.Count = 0 Then ...
0
 
LVL 8

Expert Comment

by:shovavnik
ID: 12004486
You're being clear (I think :).

If you have

<tr>
 <td align="left" id="subcategory_1" runat=server>
     <h3><b>Sub Categories</b></h3>
 </td>
</tr>

and you want to hide subcategory_1, then on the server-side:

if ds.Tables(0).Rows.Count = 0 Then subcategory_1.Visible = false
else subcategory_1.Visible = true
0
 

Author Comment

by:_Esam
ID: 12004662
I don't know..
I tried few things different ways, I still get null exception like:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 70:                     subcat1.Visible = False
Line 71:                 Else
Line 72:                     subcat1.Visible = True 'error line
Line 73:                 End If
Line 74:             End If

This is the row:
<tr id=subcat1 runat=server>
 <td align="left">
   <h3><b>Sub Categories</b></h3>
 </td>
</tr>

And this is the server-side:
At the top of the Page_Load I have:
    Dim subcat1 As HtmlTableRow
At the end of the connection closing, I have:
    If ds.Tables(0).Rows.Count = 0 Then
       subcat1.Visible = False
    Else
       subcat1.Visible = True
    End If

I must be doing some sort of error, instantiating the object. :)
Please let me know.

Thx.
_Esam
 
0
 
LVL 8

Expert Comment

by:shovavnik
ID: 12004700
Ok.  Then I wasn't being clear, and I apologize.

In your .vb file, look for the declaration of SubCatList.

It should look something like:
Dim SubCatList as System.Web.UI.WebControls.DataList

Right under it, add the line:
Dim SubCat1 As HtmlTableRow

This declaration won't be *inside* a function but a global member of the class.
It'll even work if you put it right outside your Page_Load function.

I think that'll work.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:_Esam
ID: 12004926
I do not see this line:

Dim SubCatList as System.Web.UI.WebControls.DataList

But when I tried to modify as you said,
I had an error in my .aspx html file that says this object is being declared in the same page (I dimed the subcat1 first after the webform class)

Now I am little lost.
Am I doing the whole thing wrong?

What I really wanted to do is:

1st .aspx page has some category list..
Once one is chosen, the user is redirected to the SubCategory Page.
For certain categories, there might not be any subcategory.
If there is no subcategory, then I just didn't want to show the <tr>
In my 1st page, I simply listed the Category using just one connection.
In my 2nd page I listed both SubCategories and Terms datalist.
I just wanted to make sure that if there is no subcategory, then the <tr> is not shown.

Now where do and how I declare this <tr> id (subcat1)?
I do not see how I can declare it in the 1st page!

Maybe I should have just put a HeaderTemplate in my SubCategory datalist.
Then, if there was no subcategory list to be listed, then probably (?) even the Hearder would not have been show? I'm not sure.

Please let me know.
Also, let me know if you need to know more details.

Thx.
_Esam
0
 
LVL 8

Expert Comment

by:shovavnik
ID: 12004962
Let me see if I understand you correctly.

You only have the subcategory list on the second page?

If so, then that's the only place you need to put the declaration.  You dont' need it on the first page, because it doesn't actually have the control.
0
 

Author Comment

by:_Esam
ID: 12005070
That is right.
I have the subcategory datalist in the 2nd page.
I tried as you said to declare the <tr> as an HtmlTableRow outside the Page_Load since it is a global variable.
But I still have the same error.
Object reference not set to an instance of an object.
I wonder what am I doing wrong?

Let me know if you need anymore clarification from my side.

Thx
0
 
LVL 8

Expert Comment

by:shovavnik
ID: 12005092
Can you post the code for the .aspx and the aspx.vb files of the second page?
0
 

Author Comment

by:_Esam
ID: 12005142
What I have not is set the <tr> id=subcat1

Before the Page_Load I have:

Dim subcat1 As HtmlTableRow

Right after the connection closing, I have:
            cn.Close()
                Dim MyDataTable As DataTable = ds.Tables(0)
                If MyDataTable.Rows.Count = 0 Then
                    subcat1.Visible = False
                Else
                    subcat1.Visible = True
                End If

I still have the same object instantiation error.

Thx.
0
 

Author Comment

by:_Esam
ID: 12005277
Please note that I discovered something when I was trying to paste the files.
Although I was able to all the information from the code-behind, I do not see the top section of the Page_Load sub in the code-behind of the VS.NET.
How should I be able to see that portion.
I believe I need to Dim the subcat1 somewhere after SubCatList etc. as you mentioned.
If this is the case, please let me know.


<%@ Page Language="vb" AutoEventWireup="false" Codebehind="VUI_Glossary_SubCategories.aspx.vb" Inherits="SubCategories"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
      <HEAD>
            <title></title>
            <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
            <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
            <meta content="JavaScript" name="vs_defaultClientScript">
            <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table cellSpacing="0" cellPadding="0" width="100%" bgColor="#ffffff" border="0">
 <tr align="center">
   <td vAlign="middle" bgColor="#006400" height="112">
    <h2><font color="white">Sub Categories</font></h2>
  </td>
  </tr>
</table>
<table width="100%">
 <tr>
  <td vAlign="top" width="25%" bgColor="#808000">
    <table width="100%">
     <tr>
      <td ></td>
    </tr>
    <tr>
      <td></td>
   </tr>
   <tr>
    <td ></td>
   </tr>
    <tr>
      <td></td>
   </tr>
  </table>
 </td>
 <td width="75%">
    <table width="100%">
     <tr>
          <td align="left"><h3><b>Category</b></h3></td>
     </tr>
    <tr><td></td></tr>

This is where the trouble is:
<tr id=subcat1 runat=server>
      <td align="left"><h3><b>Sub Categories</b></h3></td>
</tr>
<tr>
  <td><asp:datalist id="SubCatList" runat="server" OnItemCommand="GetTerms">
            <ItemTemplate>
               <asp:LinkButton id="SubCategoryType" Runat="server" Text= '<%
             #Container.DataItem("GSC_Name")%>' CommandName= '<%
             #Container.DataItem
            ("GSC_ID")%>' />
          </ItemTemplate>
       </asp:datalist>
   </td>
</tr>
<tr>
  <td align="left"><h3><b>Terms</b></h3></td>
</tr>
<tr>
   <td><asp:datalist id="TermList" runat="server">
      <ItemTemplate>
        <asp:LinkButton id="TermType" Runat="server" Text= '<%#Container.DataItem
          ("Term")%>' CommandName= '<%#Container.DataItem("GT_ID")%>' />
     </ItemTemplate>
     </asp:datalist>
   </td>
</tr>
</table>
</td>
</tr>
</table>
                  


            </form>
      </body>
</HTML>


The Code Behind is:


Imports System.Data
Imports System.Data.OleDb



Public Class VUI_Glossary_SubCategories
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents SubCatList As System.Web.UI.WebControls.DataList
    Protected WithEvents TermList As System.Web.UI.WebControls.DataList
    Protected WithEvents SubCatRow As System.Web.UI.HtmlControls.HtmlTableRow

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Dim subcat1 As HtmlTableRow
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        Dim subcat As String

        Dim gcat As String








        subcat = Request.QueryString("cat")
        gcat = Request.QueryString("cat")

        If Not Page.IsPostBack Then
            If subcat <> "" Then
                Dim cn As New OleDbConnection
                Dim s As String = Server.MapPath("Glossary.mdb")
                cn.ConnectionString = "provider =Microsoft.Jet.OLEDB.4.0; data source = " + s
                cn.Open()
                Dim da As New OleDbDataAdapter("SELECT GSC_ID, GSC_Name FROM G_SUB_CATEGORY " & _
    "WHERE GC_ID = " & subcat & " ORDER BY GSC_Name", cn)
                Dim ds As New DataSet
                da.Fill(ds, "G_SUB_CATEGORY")
                SubCatList.DataSource = ds
                SubCatList.DataMember = "G_SUB_CATEGORY"
                SubCatList.DataBind()
                cn.Close()
                Dim MyDataTable As DataTable = ds.Tables(0)
                If MyDataTable.Rows.Count = 0 Then
                    subcat1.Visible = False
                Else
                    subcat1.Visible = True
                End If
            End If

        If gcat <> "" Then
            Dim cnt As New OleDbConnection
            Dim st As String = Server.MapPath("Glossary.mdb")
            cnt.ConnectionString = "provider =Microsoft.Jet.OLEDB.4.0; data source = " + st
            cnt.Open()
            Dim dat As New OleDbDataAdapter("SELECT GT_ID, Term FROM G_TERM " & _
"WHERE GC_ID = " & gcat & " ORDER BY Term", cnt)
            Dim dst As New DataSet
            dat.Fill(dst, "G_TERM")
            TermList.DataSource = dst
            TermList.DataMember = "G_TERM"
            TermList.DataBind()
            cnt.Close()

        End If
            End If
    End Sub

    Sub GetTerms(ByVal Src As Object, ByVal Args As DataListCommandEventArgs)
        Response.Redirect("VUI_Glossary_SubCategories_Terms.aspx?sbcat=" & Args.CommandName)

    End Sub

    Private Sub SubCatList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SubCatList.SelectedIndexChanged

    End Sub
End Class
0
 
LVL 8

Expert Comment

by:shovavnik
ID: 12005288
Does your tr have the runat=server attribute?

<tr runat="server" id="subcat1">
0
 
LVL 8

Expert Comment

by:shovavnik
ID: 12005293
Ignore the last post.  I'll post in a minute.
0
 
LVL 8

Accepted Solution

by:
shovavnik earned 350 total points
ID: 12005328
Ok.  I think I found the problem.

Remove the declaration I told you to put in.  Instead, find the line:

Protected WithEvents SubCatRow As System.Web.UI.HtmlControls.HtmlTableRow

and change the name to subcat1 instead of SubCatRow.
0
 

Author Comment

by:_Esam
ID: 12005353
Hi shovavnik,
 I apologize, I had to edit some text contents because of (c) issues for the other side
 (not mine). :)

Thx.
0
 

Author Comment

by:_Esam
ID: 12005407
Yes, it is working.
Thank you Gracefully.
But could you please tell me why the previous control was still there?
And what should I do to prevent this in the future?
Anyway, I'm gonna have to increase the points for you because of your extended and persistent effort.
This was a great help for me since I will need to use this approach at few places.

Thanks again.
_Esam
0
 
LVL 8

Expert Comment

by:shovavnik
ID: 12005479
I appreciate the points.

You're most welcome in any case.

The previous control was still there because of the way VS.NET parses the aspx page in order to determine what to put automatically in the code behind file.

The way to avoid this is:
Whenever you work in the html view of the aspx page, and you add or remove server-side controls, be sure to switch over to design view beofre you go to the codebehind page.  This gives VS.NET an opportunity to reparse the page.  It's not full-proof but it usually works.

You can test it by going to the html view, manually adding a server-side control, and then switching immediately to the vb file.  You'll notice it's not there.  Then go back to your aspx file and switch to the design view, and again to the vb file.  You'll notice that it suddenly appeared.

Happy coding!
0
 

Author Comment

by:_Esam
ID: 12005665
Thanks for the clarification.
It will certainly help reduce this type of errors in the near future.

_Esam
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now