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.