Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-08-05
14
Medium Priority
?
1,172 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
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

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.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…

916 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