DataBinder/Container Question

DataBinder/Container Question

Post by Richard Roch » Fri, 14 Mar 2003 04:06:25



I'm using a repeater control and have the name of an image
in the dataset,
(i didn't keep the blob in the database).  I want to load
the image in the
<ItemTemplate> section of the repeater control.

This Works (text display of image name)
<%# DataBinder.Eval(Container.DataItem, "Resource_Id")%>

This Works (hard coded image name)
<%GetImage("945")%>

This Fails with the message " 'Container' is not declared"

<%GetImage(DataBinder.Eval
(Container.DataItem, "Resource_Id")))%>

GetImage builds the file name and does a Response.Write
with the
appropriate info in "< ... type=Image>"

any help/suggestions or work arounds appreciated.

 
 
 

DataBinder/Container Question

Post by Bassel Tabbara [MSF » Sat, 15 Mar 2003 21:52:25


Hello Richard,
I want to let you know that we will soon post an answer for you. Please
keep tuned.

Thanks,
Bassel Tabbara
Microsoft, ASP.NET

This posting is provided "AS IS", with no warranties, and confers no rights.

--------------------
| Content-Class: urn:content-classes:message


| Subject: DataBinder/Container Question
| Date: Wed, 12 Mar 2003 11:06:25 -0800
| Lines: 28

| MIME-Version: 1.0
| Content-Type: text/plain;
|       charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Thread-Index: AcLoynqo6413/noQR7O/vAcgK3HZcA==
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: TK2MSFTNGXA04 10.40.1.52
| Path: cpmsftngxa06!cpmsftngxa08.phx.gbl
| Xref: cpmsftngxa06 microsoft.public.dotnet.framework.aspnet:129329
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| I'm using a repeater control and have the name of an image
| in the dataset,
| (i didn't keep the blob in the database).  I want to load
| the image in the
| <ItemTemplate> section of the repeater control.
|
|
|
| This Works (text display of image name)
| <%# DataBinder.Eval(Container.DataItem, "Resource_Id")%>
|
| This Works (hard coded image name)
| <%GetImage("945")%>
|
|
| This Fails with the message " 'Container' is not declared"
|
| <%GetImage(DataBinder.Eval
| (Container.DataItem, "Resource_Id")))%>
|
|
| GetImage builds the file name and does a Response.Write
| with the
| appropriate info in "< ... type=Image>"
|
| any help/suggestions or work arounds appreciated.
|
|
|

 
 
 

DataBinder/Container Question

Post by Mike Moore [MSFT » Sun, 16 Mar 2003 12:18:35


Hi Richard,

I've used similar logic and it worked fine. Here's the line that I used:
    SelectedIndex="<%# GetIndex(container.dataitem.getstring(0)) %>"
This calls a function in the code-behind and passes data from the
Container.DataItem.

You can also use the ItemDataBound event instead of =<%# ... %>. Here's a
sample I posted for a different question which shows using ItemDataBound
and also passing a container value to a method.

Please repost if you have more questions.

Here are TWO examples based on the Pubs database.

I hope this helps. If this does not address your question correctly, please
post a sample (based on the Pubs database) which demonstrates the problem.

---
For both examples:

* Add a DataGrid to a web form.
* Right-click the grid and select "Property Builder"
* In Property Builder, UN-check "create columns automatically" and add
three bound columns with these data fields:
  - au_id
  - au_fname
  - au_lname
* Also add a template column
* Exit the property builder
* Right-click the grid and select "Edit Template"  --  "Columns[3]"
* Drag a drop list into the "ItemTemplate" section
* While the drop list is still selected, press F4 to get its properties and
set its width to 250px

 ---  SAMPLE 1  ---

   Const ConnectionString = "server='localhost'; trusted_connection=true;
Database='pubs'"

   Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
      If Not IsPostBack Then
         BindGrid()
      End If
   End Sub

   Sub BindGrid()
      Dim conPubs As New
System.Data.SqlClient.SqlConnection(ConnectionString)
      Dim sql As String = "Select au_id, au_fname, au_lname from authors"
      Dim cmdSelect As New System.Data.SqlClient.SqlCommand(sql, conPubs)
      conPubs.Open()
      DataGrid1.DataSource = cmdSelect.ExecuteReader()
      DataGrid1.DataBind()
      conPubs.Close()
   End Sub

   Sub BindList(ByVal ddl As DropDownList, ByVal au_id As String)
      Dim conPubs As New
System.Data.SqlClient.SqlConnection(ConnectionString)
      Dim sql As String = "Select titleauthor.au_id, titles.title_id, title
from titles inner join titleauthor on titleauthor.title_id =
titles.title_id where titleauthor.au_id = '" & au_id & "'"
      Dim cmdSelect As New System.Data.SqlClient.SqlCommand(sql, conPubs)
      conPubs.Open()
      ddl.DataSource = cmdSelect.ExecuteReader()
      ddl.DataTextField = "title"
      ddl.DataValueField = "title_id"
      ddl.DataBind()
      conPubs.Close()
   End Sub

   Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
      If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
         Dim ddl As DropDownList
         ddl = e.Item.Cells(3).FindControl("DropDownList1")
         BindList(ddl, e.Item.DataItem("au_id").ToString)
         If ddl.Items.Count > 1 Then
            ddl.SelectedIndex = 1
         End If
      End If
   End Sub

 ---  SAMPLE 2  ---

* Continue using the same page from Sample 1
* Switch to HTML View for the ASPX page
* Add the following attribute inside the element tag for the drop list
   SelectedIndex="<%# GetIndex(container.dataitem.getstring(0)) %>"
* In the code-behind, comment out these lines:
         If ddl.Items.Count > 1 Then
            ddl.SelectedIndex = 1
         End If

* Add this function:
   Protected Function GetIndex(ByVal au_id As String) As Integer
      'This could do anything. The following code performs the
      'same query as the original so that the index can be kept
      'within the correct boundaries. Then it sets the index to the
      'last entry, or -1 if there are no entries.
      Dim i As Integer
      Dim conPubs As New
System.Data.SqlClient.SqlConnection(ConnectionString)
      Dim sql As String = "Select titles.title_id, title from titles inner
join titleauthor on titleauthor.title_id = titles.title_id where
titleauthor.au_id = '" & au_id & "'"
      Dim da As New System.Data.SqlClient.SqlDataAdapter(sql, conPubs)
      Dim ds As New DataSet()
      da.Fill(ds, "titles")
      i = ds.Tables("titles").Rows.Count - 1
      conPubs.Close()
      Return i
   End Function

Thank you, Mike Moore
Microsoft, ASP.NET

This posting is provided "AS IS", with no warranties, and confers no rights.

--------------------

>Content-Class: urn:content-classes:message


>Subject: DataBinder/Container Question
>Date: Wed, 12 Mar 2003 11:06:25 -0800
>Lines: 28

>MIME-Version: 1.0
>Content-Type: text/plain;
>    charset="iso-8859-1"
>Content-Transfer-Encoding: 7bit
>X-Newsreader: Microsoft CDO for Windows 2000
>Thread-Index: AcLoynqo6413/noQR7O/vAcgK3HZcA==
>X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
>Newsgroups: microsoft.public.dotnet.framework.aspnet
>NNTP-Posting-Host: TK2MSFTNGXA04 10.40.1.52
>Path: cpmsftngxa06!cpmsftngxa08.phx.gbl
>Xref: cpmsftngxa06 microsoft.public.dotnet.framework.aspnet:129329
>X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

>I'm using a repeater control and have the name of an image
>in the dataset,
>(i didn't keep the blob in the database).  I want to load
>the image in the
><ItemTemplate> section of the repeater control.

>This Works (text display of image name)
><%# DataBinder.Eval(Container.DataItem, "Resource_Id")%>

>This Works (hard coded image name)
><%GetImage("945")%>

>This Fails with the message " 'Container' is not declared"

><%GetImage(DataBinder.Eval
>(Container.DataItem, "Resource_Id")))%>

>GetImage builds the file name and does a Response.Write
>with the
>appropriate info in "< ... type=Image>"

>any help/suggestions or work arounds appreciated.

 
 
 

DataBinder/Container Question

Post by Richard Roch » Tue, 18 Mar 2003 09:26:48


Thanks for the info, i learned from it.

I still must be missing something,
Now i want to skip a row/column in my table

This code is in the <ItemTemplate> Section
i'd like to skip a row/column in my table if the
item 'xxx' in my
dataset is null.
i've tried using container.dataitem.getstring(0) but still
get
comple errors for both cases.

When is the '#' required and what is it's purpose?

Thanks for your assistance.

<%if DataBinder.Eval(Container.DataItem, "xxx")="" %>
<tr>
<td>
<%# DataBinder.Eval(Container.DataItem, "xxx") %>
</td>
</tr>
<%end if%>

 
 
 

DataBinder/Container Question

Post by Mike Moore [MSFT » Wed, 19 Mar 2003 07:12:36


Hi Richard,

In brief, the # symbol tells the Framework that this block of server-side
code must be executed for EACH row of data rather than once for the whole
page.

To skip lines of data, remove those lines from the datasource before
calling DataBind.

Thank you, Mike Moore
Microsoft, ASP.NET

This posting is provided "AS IS", with no warranties, and confers no rights.

--------------------

>Content-Class: urn:content-classes:message





>Subject: RE: DataBinder/Container Question
>Date: Sun, 16 Mar 2003 16:26:48 -0800
>Lines: 25

>MIME-Version: 1.0
>Content-Type: text/plain;
>    charset="iso-8859-1"
>Content-Transfer-Encoding: 7bit
>X-Newsreader: Microsoft CDO for Windows 2000
>Thread-Index: AcLsG+WZpFLAI0AsTU6hUzw4r/sAXA==
>X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
>Newsgroups: microsoft.public.dotnet.framework.aspnet
>Path: cpmsftngxa08.phx.gbl
>Xref: cpmsftngxa08.phx.gbl microsoft.public.dotnet.framework.aspnet:130972
>NNTP-Posting-Host: TK2MSFTNGXA02 10.40.1.51
>X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet

>Thanks for the info, i learned from it.

>I still must be missing something,
>Now i want to skip a row/column in my table

>This code is in the <ItemTemplate> Section
>i'd like to skip a row/column in my table if the
>item 'xxx' in my
>dataset is null.
>i've tried using container.dataitem.getstring(0) but still
>get
>comple errors for both cases.

>When is the '#' required and what is it's purpose?

>Thanks for your assistance.

><%if DataBinder.Eval(Container.DataItem, "xxx")="" %>
><tr>
><td>
><%# DataBinder.Eval(Container.DataItem, "xxx") %>
></td>
></tr>
><%end if%>

 
 
 

1. How to use Databinder.Eval(Container.DataItem, "lastname")

I am working with the article
http://msdn.microsoft.com/msdnmag/issues/02/01/cutting/cutting0201.asp.

I always get an error  BC30451: Name 'Container' is not declared.  They
never declare container in the article Binding SQL Data to DataList Control.
Well I must be stupid, but I don't know what container this is refering to.
To me a form and lot of other things are containers.  Please Help

2. security template

3. What to use? DataBinder.Eval or Container.DataItem ??

4. Log on Problems

5. Evaluate <%# Databinder.Eval(Container,"xxx")%> against a session variable

6. Use session vars in modules??

7. DataBinder.Eval vs. Container.DataItem

8. Connecting iPaq to desktop comp. via serial cable

9. Container.DataItem vs. DataBinder.Eval?

10. Container.DataItem vs DataBinder.Eval

11. alternative to using DataBinder.Eval(Container.DataItem,"columnname")%> for datalist columns displayed

12. <%# DataBinder.Eval(Container.DataItem, "fname") %> prob.

13. DataBinder.Eval Question