Solved

How to set datalist datasouce/event properties inside a repeater.(VB.NET)

Posted on 2004-09-24
20
430 Views
Last Modified: 2008-02-01
Hi,
 I have a datalist inside a repeater.
 I was able to add the datasource for this repeater..
 But I also need to LinkButton the datalist items,,and handle their events..
 I can implement these if the datalist was just by itself..
 My problem is now it is nested within a repeater..
 I need to know how exactly I do that..

This is what I would like to do:

<asp:repeater id="TermDetails" Runat="server" OnItemDataBound="GetTermRelatedInfo">
  <ItemTemplate>

<tr>
     <td colspan="2" align="left">See Also
          <asp:DataList ID="RelatedTerms" Runat="server">
          <ItemTemplate>
              '<%#Container.DataItem("GR_Type")%>'
           </ItemTemplate>
          </asp:DataList>
      </td>
</tr>

</ItemTemplate>
</asp:repeater>

For simplicity, I did not include the details of the main repeater.
How can I set datasource, LinkButtion the datalist items, and handle events for these datalist items.

Please let me know.
Thx.
_Esam
0
Comment
Question by:_Esam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 6
20 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 12147111
Hi _Esam,

just use this

<asp:repeater id="TermDetails" Runat="server" OnItemDataBound="GetTermRelatedInfo">
  <ItemTemplate>

<tr>
     <td colspan="2" align="left">See Also
          <asp:DataList ID="RelatedTerms" Runat="server" OnItemCommand="DataListItem_Command">
          <ItemTemplate>
              '<%#Container.DataItem("GR_Type")%>'
                <asp:LinkButton id="SelectButton"
                 Text="Select"
                 CommandName="Select"
                 runat="server"/>

           </ItemTemplate>
          </asp:DataList>
      </td>
</tr>

</ItemTemplate>
</asp:repeater>

vb.net
 Sub DataListItem_Command(sender As Object, e As DataListCommandEventArgs)
        if  e.CommandName = "Select" Then
        'do what you want with your link button
        End if
      End Sub

c#  

      void DataListItem_Command(Object sender, DataListCommandEventArgs e)
      {
        if ( e.CommandName == "Select" )
        {
        //do what you want with your link button
        }      
      }


B..M
0
 
LVL 9

Expert Comment

by:hismightiness
ID: 12147131
I have been beaten to the punch...  This is what I was going to suggest:

    Private Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemCreated
        Dim objRepeater As Repeater
        objRepeater = CType(e.Item.FindControl("myrepeatername"), Repeater)
        With objRepeater
            .DataSource = mydatasource
            .DataBind()
        End With
    End Sub
0
 

Author Comment

by:_Esam
ID: 12147242
Hi,
 Sorry there were some errors while copying, pasting...
 For the inner datalist, I would like to have something like this:
 The text property set to the GT_Term (string field from G_Term table)
 But the CommandName set to the GT_ID (ID for the GT_Term in G_Term table)
 And when the LinkButtoned datalist is clicked, I would like the send a session variable for
 this GT_ID.
 Per, B..M's suggestion, I actually don't need to find the control (datalist) inside this
 repeater?

Thx.
_Esam
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 28

Expert Comment

by:mmarinov
ID: 12147295
_Esam ,

no you  don't have to search for it the event is set for the datalist

you have to check, just be sure, if the declaration of the event should be public
because(may be) otherwise the asp.net will not find it


B..M
0
 

Author Comment

by:_Esam
ID: 12147298
This is what I was doing earlier,
Now I need to put thid datalist (another one like this) inside the repeater and pretty much do the same things as I did with this example. Hope I am clear.

<asp:datalist id="TermList" runat="server" OnItemCommand="GetTerms">
      <ItemTemplate>
      <asp:LinkButton id="TermType" Runat="server" Text= '<%
               #Container.DataItem("GT_Term")%>' CommandName= '<%
                #Container.DataItem("GT_ID")%>' />
      </ItemTemplate>                  
</asp:datalist>
                                          </tr>


Sub GetTerms(ByVal Src As Object, ByVal Args As DataListCommandEventArgs)
        Session("Term_Value") = Args.CommandName

        Response.Redirect("VUI_G_Terms.aspx")
End Sub

_Esam
0
 

Author Comment

by:_Esam
ID: 12147353
Hi B.M.
 I'm clear with event handling for the inner datalist within the repeater now.
 How do I set the datasource for this inner datalist?

_Esam
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12147391
_Esam,

you have to use the OnItemDataBound event of the repeater
and in it

if e.Item.ItemType =  ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
    Dim datalistSearched as DataList = Ctype(e.Item.FindControl("your data list control", DataList)
    datalistSearched.Datasource = '
    datalistSearched.DataBind()
End if

B..M
0
 

Author Comment

by:_Esam
ID: 12147420
Thanks B..M
Let me try it real quick and get back to you...

_Esam
0
 

Author Comment

by:_Esam
ID: 12147569
Hi,
This is what I tried to do..
Maybe there are some sort of errors..

Dim tv As String
        tv = Session("Term_Value").ToString

        Dim cns As New OleDbConnection
        Dim ss As String = Server.MapPath("ADVOICE.mdb")
        cns.ConnectionString = "provider =Microsoft.Jet.OLEDB.4.0; data source = " + ss
        cns.Open()

        Dim das As New OleDbDataAdapter("SELECT G_Term.*, G_Related_Terms.GT_ID, G_Related_Terms.GT_ID_Other FROM G_Term INNER JOIN G_Related_Terms ON G_Term.GT_ID=G_Related_Terms.GT_ID_Other " & _
        "WHERE G_Related_Terms.GT_ID =" & tv, cns)

        Dim dss As New DataSet
        das.Fill(dss, "G_Term")


        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
        Dim TermRList as DataList = Ctype(e.Item.FindControl("RelatedTerms", DataList) 'it expects a comma here?
            TermRList.DataSource = dss
            TermRList.DataMember = "G_Term"
            TermRList.DataBind()
        End If

      cns.Close()

It complaint that all the TermRList are not declared, and also expects a ' around Datalist?

_Esam
0
 
LVL 28

Accepted Solution

by:
mmarinov earned 150 total points
ID: 12147590
_Esam,

you have to modify this line
Dim TermRList as DataList = Ctype(e.Item.FindControl("RelatedTerms", DataList)
to

Dim TermRList as DataList = Ctype(e.Item.FindControl("RelatedTerms")<-this is what you've missed, DataList)

B..M
0
 

Author Comment

by:_Esam
ID: 12147686
I get an error as:

GT_ID is neither a DataColumn nor a DataRelation for table G_Term.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: GT_ID is neither a DataColumn nor a DataRelation for table G_Term.

Source Error:


Line 104:      <asp:DataList ID="RelatedTerms" Runat="server">
Line 105:      <ItemTemplate>
Line 106:               <asp:LinkButton id="TermType" Runat="server" Text= '<%
           #Container.DataItem("GT_Term")%>' CommandName= '<%#Container.DataItem
           ("GT_ID")%>' /><BR>

Line 107:      </ItemTemplate>
Line 108:      </asp:Datalist>
 

Source File: c:\inetpub\wwwroot\ADVOICE_Final\VUI_G_Terms.aspx    Line: 106

Is it because, I searched the G_Related_terms tables, but bounded the values from G_Term?
U can see that from the other post where I made the query.

_Esam
0
 

Author Comment

by:_Esam
ID: 12147726
Please note that my parent repeater is also using some fields from G_Term.
I also need to include some related terms, with this inner datalist, from the same G_Term
table comparing with another tablse as I did in the query..

_Esam
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12147735
_Esam,

so you mean that you've bound the LinkButton to values from the master control's ( repeater ) datasource, not from the datalist's datasource, correct ?

if yes, you have to use DataBinder.Eval
if you mixed - some of the values are got from the master - for them you have to use DataBinder.Eval(Container.DataItem, "column name")
for the others, because of the nested controls, you have to create ItemDataBound event of the datalist ( like the ItemCommand ), before that ( int the itemCommand event ) to store the datalist datasource in a variable, so in the ItemDataBound of the datalist control you can get this stored datasource, find the control you wanted to bind by FindControl method and set it's value to a current value

B..M
0
 

Author Comment

by:_Esam
ID: 12147812
Well,
 I did use the datalist datasource for the LinkButton..
 What I meant is that this datalist datasource get the values from the same table as
 does the master repeater datasource (I set up the repeater datasource from this G_Term table, it worked before..., problem is with the inner datalist)  That shouldn't be a problem, I think, if otherwise.
 Thus when I tried the Databinder,,,there were some errors as expected...

_Esam
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12147849
_Esam,

the fact that you use one table is the easy part - when you create the datasource for datalist control , you've already have the datasource for it
what errors actually do you receive ?

B..M
0
 

Author Comment

by:_Esam
ID: 12147866
Hi B..M.
 This is what happens..
 If I remove the GT_ID (CommandName) from this:

<asp:LinkButton id="TermType" Runat="server" Text= '<%#Container.DataItem("GT_Term")%>'     CommandName= '<%#Container.DataItem("GT_ID")%>'  />

I works, just shows me the related terms list with linkbuttons...
I also need to add the CommandName..

_Esam
0
 

Author Comment

by:_Esam
ID: 12147917
This is the error I get if I include GT_ID for my CommandName for the datalist items
I think something else need to be done here..

GT_ID is neither a DataColumn nor a DataRelation for table G_Term.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: GT_ID is neither a DataColumn nor a DataRelation for table G_Term.

Source Error:
Line 104:      <asp:DataList ID="RelatedTerms" Runat="server">
Line 105:      <ItemTemplate>
Line 106:      <asp:LinkButton id="TermType" Runat="server" Text= '<%#Container.DataItem("GT_Term")%>' CommandName= '<%#Container.DataItem("GT_ID")%>'/>
Line 107:   </ItemTemplate>
Line 108:      </asp:DataList>
 
Source File: c:\inetpub\wwwroot\ADVOICE_Final\VUI_G_Terms.aspx    Line: 106

_Esam
0
 

Author Comment

by:_Esam
ID: 12147944
To be clear,,
I haven't created any datalist event item handler yet....this should not matter now..
since at this moment I am not doing anything with the datalist items...i am taking a step at a time..

_Esam
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

728 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