Link to home
Start Free TrialLog in
Avatar of _Esam
_Esam

asked on

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

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
Avatar of mmarinov
mmarinov

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
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
Avatar of _Esam

ASKER

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
_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
Avatar of _Esam

ASKER

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
Avatar of _Esam

ASKER

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
_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
Avatar of _Esam

ASKER

Thanks B..M
Let me try it real quick and get back to you...

_Esam
Avatar of _Esam

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of mmarinov
mmarinov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of _Esam

ASKER

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
Avatar of _Esam

ASKER

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
_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
Avatar of _Esam

ASKER

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
_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
Avatar of _Esam

ASKER

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
Avatar of _Esam

ASKER

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
Avatar of _Esam

ASKER

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