Solved

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

Posted on 2004-08-05
14
1,159 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Scheduled IIS .Net2 AppPools recycle and SQL connection Hangs 33 88
Align Buttons next to each other 2 25
SQL Login 17 38
Message not shown 5 31
AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
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.…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

21 Experts available now in Live!

Get 1:1 Help Now