Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Is it legal to have a Container.DataItem inside the HeaderTemplate ?

Posted on 2004-08-05
14
Medium Priority
?
1,171 Views
Last Modified: 2010-08-05
Hii There,

I am trying to have one of my fields displayed in the HeaderTemplate of my datalist and it's not showing event though the stored procedure returns the field & the datalist has no error?

Do I need to create an event for this value to show up as a header item?

Sean


<asp:DataList ID="dliststate" Runat="server" Width="100%" BorderWidth="0" cellpadding="5" BorderColor="#006699">
  <HeaderTemplate>
<tr width="100%"><td bgcolor="#999999" class="tablenametext" align="center" colspan="5" height="30"><%# DataBinder.Eval(Container.DataItem, "formname") %></td></tr>
    <tr bgcolor="#cccccc" class="tableheadertext" height="30">
          <td align="center" width="100">Item number</td>
          <td align="center" width="60">Manufacturer</td>
          <td align="center" width="100">Make-Model</td>
          <td align="center" width="70">Quantity</td>
          <td align="center" width="140">Specifications</td>
        </tr>
  </HeaderTemplate>
  <ItemTemplate>
    <tr bgcolor="d9d9d9" class="tabletext">
      <td align="center" width="100"><%# DataBinder.Eval(Container.DataItem, "ListingID") %></td>
      <td align="center" width="60"><%# DataBinder.Eval(Container.DataItem, "Manufacturer") %></td>
      <td align="center" width="100"><%# DataBinder.Eval(Container.DataItem, "model") %></td>
      <td align="center" width="70"><%# DataBinder.Eval(Container.DataItem, "quantity") %></td>
      <td align="center" width="140"> <asp:HyperLink CssClass="tablelink" runat="server" Text="Specfications"
     NavigateUrl='<%# "ListingDetails.aspx?ListingID=" & DataBinder.Eval(Container.DataItem, "listingID")%>' /></td>
    </tr>
  </ItemTemplate>
  <FooterTemplate>
    </table>
  </FooterTemplate>
</asp:DataList>
0
Comment
Question by:bullrout
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
14 Comments
 
LVL 15

Expert Comment

by:Thogek
ID: 11733059
What is "formname"?
0
 
LVL 5

Author Comment

by:bullrout
ID: 11733071
it's just a field that is returned from the strored procedure.

sean
0
 
LVL 15

Expert Comment

by:Thogek
ID: 11733072
DataBinder.Eval typically resolves values and elements of the current data item, as a DataList (or DataGrid, etc.) is being bound to records/rows/etc., in the data source object.  This would then be usable within the ItemTemplate, AlternatingItemTemplate, etc.

The HeaderTemplate is generated before any of the data items are traversed, which seems to suggest that no data item would be available within the HeaderTemplate.

More at http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWebUIWebControlsDataListClassTopic.asp

So... where exactly is this "formname" value expected to come from?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 15

Expert Comment

by:Thogek
ID: 11733073
So "formname" is one of the columns in the DataTable that's being bound to the DataList?

Are you trying to pull the formname value from the first record and display it as a header over the DataList?  (Or are all records expected to have the same value for this column?)
0
 
LVL 5

Author Comment

by:bullrout
ID: 11733085
that's correct I am trying to pull the formname value and display it over the rest of the list, all of the rows returned from the stored procedure have this common value "formname".

Do I need to have a event handler attached to the list?

Sean
0
 
LVL 15

Expert Comment

by:Thogek
ID: 11733106
I don't think a DataList event handler would help much with this particular issue.

You could, in your codebehind (when the DataTable/DataSet is populated, grab the value like so:

    private string strFormName = "";

    protected string FormName {
        get { return strFormName; }
    }

    // ...in whatever method is populating the DataTable...
    strFormName = (string)(dt.Rows[0]["formname"]);

replacing "dt" with your DataTable variable name.

Then, in the ASPX, try replacing
    <%# DataBinder.Eval(Container.DataItem, "formname") %>
with
    <%# FormName %>
0
 
LVL 5

Author Comment

by:bullrout
ID: 11733313
hi there,

i have declared the variable "Private strFormname as string " in my code behind page. I tried to get the value in the variable at runtime, it does not error but the value is not returning at all. What am I doing wrong?

Sean

       dr                        = dataSet.Tables(0).Rows(0)
        strFormname =   dr("formname")

!-- header template
<HeaderTemplate>
                              <%# strFormname%>
                        <tr><td>Make & Model</td><td>Price</td><td>Manufacturer</td><td>col4</td><td>col5</td></tr>
                    </HeaderTemplate>

!-- sub

 Private Sub BindTheData(Byval CategoryID as Integer)
    Dim objConn As New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
  '  Dim strSql As String = "SELECT CompanyName, ContactName, ContactTitle, Country, Phone FROM Customers"
   ' Dim dataAdapter As New SqlDataAdapter(strSql, objConn)
   
     Dim dataAdapter As New SqlDataAdapter("GetBrowseListings",ConfigurationSettings.AppSettings("connectionString"))

        DataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure

        DataAdapter.SelectCommand.Parameters.Add(New SqlParameter("@ID", SqlDbType.Int))
        DataAdapter.SelectCommand.Parameters("@ID").Value = CategoryID

    Dim dataSet As New DataSet()
    Dim dr As DataRow
    If Not Page.IsPostBack() Then
      dataAdapter.Fill(dataSet)
      lblRecordCount.Text = CStr(dataSet.Tables(0).Rows.Count)
        dr                        = dataSet.Tables(0).Rows(0)
        strFormname =   dr("formname")
      dataSet = Nothing
      dataSet = New DataSet()
    End If
    dataAdapter.Fill (dataSet, Cint(lblCurrentIndex.Text), CInt(lblPageSize.Text), "tblListings")
    dlistcat.DataSource = dataSet.Tables("tblListings").DefaultView
    dlistcat.DataBind()
     
    objConn.Close()
    ShowCounts()
  End Sub
0
 
LVL 15

Expert Comment

by:Thogek
ID: 11733356
Above, strFormname is a Private member variable.  So you either need to make it Public, or expose it through a Public property.
0
 
LVL 5

Author Comment

by:bullrout
ID: 11733403
Hi There,

I did try this  Public strFormname as string  declared at the top of the class but I still don't have a returned value? Any other ideas?

Sean
0
 
LVL 15

Accepted Solution

by:
Thogek earned 2000 total points
ID: 11739801
Might be a problem accessing the Page property from within the DataList....

You could also try moving the <%#...%> outside of the DataList, like so:

Change

    <asp:DataList ID="dliststate" Runat="server" Width="100%" BorderWidth="0" cellpadding="5" BorderColor="#006699">
        <HeaderTemplate>
        <tr width="100%"><td bgcolor="#999999" class="tablenametext" align="center" colspan="5" height="30<%# strFormname %></td></tr>

to

    <tr width="100%"><td bgcolor="#999999" class="tablenametext" align="center" colspan="5" height="30<%# strFormname %></td></tr>
    <asp:DataList ID="dliststate" Runat="server" Width="100%" BorderWidth="0" cellpadding="5" BorderColor="#006699">
        <HeaderTemplate>
0
 
LVL 15

Expert Comment

by:Thogek
ID: 11740147
Oh, and you may need to add a call to the page's DataBind method, as well.  Such as at the end of the Page_Load or Page_Render methods.

Or, you could change the
    <%# strFormname%>
to a
    <%= strFormname%>
0
 
LVL 5

Author Comment

by:bullrout
ID: 11740986
Hi Thogek,

The change to the code seemed to work using this syntax <%= strFormname%>, what is the difference between the two? is one the default .net way of doing things and the other legacy asp syntax?

Sean


    <%# strFormname%>

    <%= strFormname%>
0
 
LVL 15

Expert Comment

by:Thogek
ID: 11741556
   <%# strFormname %>
This is a data-binding syntax.  It creates a data-bound location within the HTML that, when the page's own DataBind() method is called, willbe populated with whatever value is referenced by the code within.  (If the page's DataBind() is not called, this will effectively do nothing.)

    <%= strFormname %>
This, much as in "classic" ASP, is a shorthand notation equivalent to a Response.Write method call, which simply writes out the value referenced by the code within during the page's Render state.

There's nothing default about either one; they're both perfectly valid.  There's something of a movement to wean away from <%=...%> syntax and toward more cleanly object-oriented structures (such as placing an LiteralControl object on the page, and then updating its Text value via code-behind), but sometimes some might think that doing the full object-oriented "right" way is more trouble than it needs to be so... whichever works for you, works.  :-)
0
 
LVL 5

Author Comment

by:bullrout
ID: 11750712
Hi Thogek,

I appreciate the help you have given me.

Sean
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

722 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