Link to home
Start Free TrialLog in
Avatar of _Esam
_Esam

asked on

How to add datalist, hyperlink controls inside a Repeater and set the datasource for these

Hi,
I have a repeater that holds the data from the G_Term table using a session variable.
What I would also like to do is add a hyperlink, and a datalist control inside this repeater and set the datasource properties of these two controls.
From my limited knowledge, I understand that I can use FindControl method to find the child control within the repeater and set their datasource properties.
But I do know enough to do these.
I need guidance on these.

Here is the scenario:

The repeater gets the following values form G_Term (using GT_ID from a session variable)

Name:           GT_Term
Definitio:        GT_Definition
Example use:  GT_Example
Relevance:     GT_VUI_Relevance

Now on top of these info, I would like to include a hyperlink (within the repeater)  that will redirect the user to the Category page (I get the Category name from another passed sesssion variable)

And on bottom of these info, I would like to include a datalist control within this repeater,
that will show the related G_Term from another table, G_Related_Terms, and another hyperlink control to redirect the user to another page (Bibliography page) to get bibliographic info for this G_Term.

I need guidance on how I can use the FindControl method to find the child controls within this repeater and set their datasource properties.

Thanks.
_Esam


Avatar of Thalox
Thalox


you should do the "subbinding" in the ItemDataBound event of your repeater

private void rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    // the category page link
    LinkButton lbtTop = (Linkbutton) e.Item.FindControl(<NAME_OF_LINK>);
    lbtTop.Text = CategoryName;
    ...

    DataList list = e.Item.FindControl("dd");
    // get your datasource and bind it (you can access the current datarow (of the repeater) with
      DataRow row = (DataRow)e.Item.DataItem;)
   
    list.DataSource = <Your_New_DataSource>
    list.DataBind();


    // the Bibliography page link
    LinkButton lbtBottom = (Linkbutton) e.Item.FindControl(<NAME_OF_LINK>);
    lbtTop.Text = BibliographyName;
    ...
 

}


Hope this helps,


Thalox
Avatar of _Esam

ASKER

Hi Thalox,
 I understand the concepts pretty much..
 I am pretty new in this stuff..
 Could you please tell me how I can set the ItemDataBound event for this repeater?

Thx.
_Esam
Avatar of _Esam

ASKER

Hi,
I guess I can simply define within the repeater as:

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

And the TermDetails_Others sub as you showed above should take care of the matters
so far?
_Esam

in your InitializeComponent() (added by vs)
add the following code

this.<Name_of_Repeater>.ItemDataBound += new RepeaterItemEventHandler(<Name_of_Repeater>_ItemDataBound);
(after typing += you should be able to press TAB 2 times, and vs will add the rest for you)


Thalox
Avatar of _Esam

ASKER

Hi,
I am not clear about this part:
DataList list = e.Item.FindControl("dd");
    // get your datasource and bind it (you can access the current datarow (of the repeater) with
      DataRow row = (DataRow)e.Item.DataItem;)

Could you please explain what you meant by this?

Thaks.
_Esam
Avatar of _Esam

ASKER

Sorry,
what should be the syntax since I am using VB.NET

_Esam
Avatar of _Esam

ASKER

Hi,
For the moment, I am adding the controls and setting the datasources for the controls
in the sub...
Please let me know..about the syntax specifically for the ItemDataBound syntax.

Thx.
_Esam

I'm not quiet sure, what the right syntax would be, but something like this:

Dim list As DataList = FindControl(<Name_of_DataList>)

' the next part is, how you access/create your new datasource (I think, you need to get some data from the repeater, to find the related data in the other table)

Dim row As DataRow = e.Item.DataItem


Thalox
Avatar of _Esam

ASKER

I don't understand the declaration after the initializecomponent..
I tried to add Me.TermDetails.ItemDatabound +=
Nothing happened, but I did get an error message..saying that it is an event and cannot be called directly, need to raise an event?
Where did the sub go????

_Esam

yes, it works a little different in vb.

dont know, how to bring vs to help you with this, but you can do it by hand

add this to your sourcecode (and place the databinding stuff inside)

Private Sub TermDetails_Others(ByVal sender As System.Object, ByVal e As                
                     System.Web.UI.WebControls.RepeaterItemEventArgs) Handles <repaeter_name>.ItemDataBound

Dim list As DataList = FindControl(<Name_of_DataList>)
...

End Sub

Thalox
Avatar of _Esam

ASKER

Hi,
So far, I tried to do a simple subbinding, didn't work.:)
Here is what I tried:
 This is the repeater:

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

When arriving on this page, I already passed two session variables.
I tried to use one, and bind it to the TopLeveCat hyperlink...
I think there were some sort of instantiation error...

 Sub GetTermRelatedInfo(ByVal sender As System.Object, ByVal e As RepeaterItemEventArgs) Handles TermDetails.ItemDataBound
        Dim tv As String
        Dim tc As String

        tc = Session("cat").ToString

        Dim cnn As New OleDbConnection
        Dim s As String = Server.MapPath("ADVOICE.mdb")
        cnn.ConnectionString = "provider =Microsoft.Jet.OLEDB.4.0; data source = " + s
        Dim cmd As New OleDb.OleDbCommand
        Dim dr As OleDb.OleDbDataReader
        cmd.CommandType = CommandType.Text
        cmd.CommandText = _
        "SELECT GC_ID, GC_Category FROM G_Category WHERE GC_ID=" & tc
        cmd.Connection = cnn
        cnn.Open()
        dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
        dr.Read()
        Dim TopLevelLink As HyperLink = FindControl("TopLevelCat")
        TopLevelLink.Text = CStr(dr("GC_Category"))
        TopLevelLink.NavigateUrl = "VUI_G_Categories.aspx"
        cnn.Close()
        dr.Close()
        cmd.Dispose()
        cnn.Dispose()
    End Sub


The error line is this one:
TopLevelLink.Text = CStr(dr("GC_Category"))

Let me know..if I made some sort of error..

Thanks.
_Esam
Avatar of _Esam

ASKER

Do I need to add this or something like this before the page load sub?

Protected WithEvents TopLevelCat As System.Web.UI.WebControls.HyperLink

_Esam

What error do you get?

I think it is not neccassary to declare the Hyperlink control, because you get it with findcontrol().
Did you declare this hyperlink in your *.aspx page with the right id and runat="server" ?

It seems either, you don't have such a control, or your sqlcommand doesn't return the right data.

but it would be easier if you post the error-message ;)


Thalox
Avatar of _Esam

ASKER

Yes, I have it as:

<asp:HyperLink ID="TopLevelCat" Runat="server"></asp:HyperLink>

Here is the error message:

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 238:        dr.Read()
Line 239:        Dim TopLevelLink As HyperLink = FindControl("TopLevelCat")
Line 240:        TopLevelLink.Text = CStr(dr("GC_Category"))
Line 241:        TopLevelLink.NavigateUrl = "VUI_G_Categories.aspx"
Line 242:        cnn.Close()
 

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

_Esam

sorry, dont have much experience in vb (and so copied code from help)

Line 239 should be

Dim TopLevelLink As HyperLink = e.Item.FindControl("TopLevelCat")


otherwise it would search in the page and not in the repeater


Thalox
Avatar of _Esam

ASKER

Ok, so far I am being able to add the HyperLink..
with this help>>Dim TopLevelLink As HyperLink = e.Item.FindControl("TopLevelCat")

Let me try the datalist now..and see how it goes..

_Esam
Avatar of _Esam

ASKER

Ok, I was able to bind the datalist in the repeater control...
Now say, for the moment, I want to add another LinkButton control to this repeater..
I can do that..
The problem is: I would like to show the LinkButton name as the name of the Term
that I bound with the parent repeater DataItem but pass a different value (bibliographic referece id for this term)..
Any idea, how I can do that?
Help would be greatly appreciated..
Thax.
_Esam
Avatar of _Esam

ASKER

I did something like this before:

<asp:LinkButton id="SubCategoryType" Runat="server" Text= '<%#Container.DataItem("GC_Category")%>' CommandName= '<%#Container.DataItem("GC_ID")%>' />

What I want to do now is actually,

Text= '<%#Container.DataItem("GT_Term")%>
and
CommandName= '<%#Container.DataItem("BC_ID")%>'

Here the thing is: This LinKButton is a child control of the repeater.
So, if I use>>>Text= '<%#Container.DataItem("GT_Term")%>
What could be the problem?
How do I bind values to this child controls?
I think you wanted so say something about this...?
That I wasn't clear??
COuld u clarify pls.?
Thx.
_Esam

where does your ("GT_Term") come from?
is it the datasource of the repeater or another one (this from the datalist?)?

you can set the values the same way you did the Hyperlink (with FindControl ...)

or is there something different?

Thalox
Avatar of _Esam

ASKER

>where does your ("GT_Term") come from?
Yes: >it is the datasource of the repeater
But I want to bind it to another LinkButton control text inside this repeater.

Yes, it's little different now.
I want a LinkButton as a child control within the repeater, and I want to show the text as the GT_Term (this I already bound with the main repeater...as a DataItem)
And I want to pass a session variable with when this LinkButton is clicked. Thus I want to pass (BC_ID > Bibilography ID reference for this G_Term

BC_ID comes from another table where GT_ID (primary key for G_Term,,,,etc, the main table here)

So, essentially there are two tables involved with this LinkButton..
Text=from G_Term
CommanName=BC_ID
How can this be done?
Hope I am clear..

Sorry to say, I will be leaving shortly...will try to finish up this question...

_Esam
ASKER CERTIFIED SOLUTION
Avatar of Thalox
Thalox

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