Solved

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

Posted on 2004-09-24
20
420 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
  • 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

15 Experts available now in Live!

Get 1:1 Help Now