Solved

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

Posted on 2004-08-05
14
1,158 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
  • 8
  • 6
14 Comments
 
LVL 15

Expert Comment

by:Thogek
Comment Utility
What is "formname"?
0
 
LVL 5

Author Comment

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

sean
0
 
LVL 15

Expert Comment

by:Thogek
Comment Utility
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
 
LVL 15

Expert Comment

by:Thogek
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 15

Expert Comment

by:Thogek
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
   <%# 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
Comment Utility
Hi Thogek,

I appreciate the help you have given me.

Sean
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

10 Experts available now in Live!

Get 1:1 Help Now