Solved

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

Posted on 2004-09-23
23
269 Views
Last Modified: 2008-02-01
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


0
Comment
Question by:_Esam
  • 13
  • 8
23 Comments
 
LVL 9

Expert Comment

by:Thalox
Comment Utility

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
0
 

Author Comment

by:_Esam
Comment Utility
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
0
 

Author Comment

by:_Esam
Comment Utility
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
0
 
LVL 9

Expert Comment

by:Thalox
Comment Utility

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
0
 

Author Comment

by:_Esam
Comment Utility
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
0
 

Author Comment

by:_Esam
Comment Utility
Sorry,
what should be the syntax since I am using VB.NET

_Esam
0
 

Author Comment

by:_Esam
Comment Utility
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
0
 
LVL 9

Expert Comment

by:Thalox
Comment Utility

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
0
 

Author Comment

by:_Esam
Comment Utility
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
0
 
LVL 9

Expert Comment

by:Thalox
Comment Utility

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
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:_Esam
Comment Utility
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
0
 

Author Comment

by:_Esam
Comment Utility
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
0
 
LVL 9

Expert Comment

by:Thalox
Comment Utility

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
0
 

Author Comment

by:_Esam
Comment Utility
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
0
 
LVL 9

Expert Comment

by:Thalox
Comment Utility

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
0
 

Author Comment

by:_Esam
Comment Utility
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
0
 

Author Comment

by:_Esam
Comment Utility
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
0
 

Author Comment

by:_Esam
Comment Utility
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
0
 
LVL 9

Expert Comment

by:Thalox
Comment Utility

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
0
 

Author Comment

by:_Esam
Comment Utility
>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
0
 
LVL 9

Accepted Solution

by:
Thalox earned 250 total points
Comment Utility

I would do this in the ItemDataBound handler

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


Dim lbt As LinkButton = FindControl(<Name_of_LinkButton>)
Dim row As DataRow = e.Item.DataItem
lbt.Text = CStr(row("GT_Term"))

' now for the Command select the data from your second table
...
 "SELECT BC_ID FROM <TABLE_NAME> WHERE GT_ID=" & CStr(row("GT_ID"))
...
' exec the datareader
 dr.Read()
lbt.CommandName = CStr(dr("BC_ID"))

End Sub


This should work. Maybe you will have to change some names, so it will fit your database,
but I hope I could point you the way...


Thalox
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

11 Experts available now in Live!

Get 1:1 Help Now