Solved

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

Posted on 2004-09-23
23
290 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
[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
  • 13
  • 8
23 Comments
 
LVL 9

Expert Comment

by:Thalox
ID: 12131376

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
ID: 12131443
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
ID: 12131485
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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 9

Expert Comment

by:Thalox
ID: 12131495

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
ID: 12131496
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
ID: 12131504
Sorry,
what should be the syntax since I am using VB.NET

_Esam
0
 

Author Comment

by:_Esam
ID: 12131532
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
ID: 12131541

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
ID: 12131602
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
ID: 12131832

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
 

Author Comment

by:_Esam
ID: 12131953
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
ID: 12132002
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
ID: 12132183

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
ID: 12132230
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
ID: 12132321

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
ID: 12132381
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
ID: 12132681
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
ID: 12132765
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
ID: 12133165

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
ID: 12133359
>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
ID: 12133576

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to create an MVC5, Razor Widget 2 50
Technology Resume 7 88
ASP.NET - Why is accordion not working? 3 72
Build a string of emails from a gridview 2 31
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…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 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