Example of using Repeater

Hi,
I need one example, in which it is constructing a list of Linkbuttons using Repeater, by capturing the Student ID, name, class, from the student table, as the Text of the Linkbutton.
LVL 12
HuaMin ChenProblem resolverAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
Have a read through this, which shows the basics of working with the repeater. You should be able to adapt it easily enough to use a LinkButton instead.

http://myvirtualplayground.co.uk/blog/asp_dot_net_working_with_the_repeater_control

Or we can provide further assistance if you have any queries.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Éric MoreauSenior .Net ConsultantCommented:
the windows forms version or the web version?

if Windows forms, check "The DataRepeater control" from http://emoreau.com/Entries/Articles/2008/03/Microsoft-Visual-Basic-Power-Packs-30.aspx
0
HuaMin ChenProblem resolverAuthor Commented:
Many thanks all.
Carl,
For the list similar to this below

List<BankAccount> accounts = new List<BankAccount>()
{
	new BankAccount { AccountNumber = "1234567890", AccountHolder = "Bob Smith", Balance = 100000.00M },
	new BankAccount { AccountNumber = "0001234567", AccountHolder = "Sally Jones", Balance = -100.34M },
	new BankAccount { AccountNumber = "1230001234", AccountHolder = "Jeremy Johnson", Balance = 800.34M },
	new BankAccount { AccountNumber = "0001003456", AccountHolder = "Bruce Guinness", Balance = -80.15M },
	new BankAccount { AccountNumber = "2000012789", AccountHolder = "Delia Thomson", Balance = 1200.00M }
};

Open in new window

I expect that the relevant columns are retrieved from a table upon that the page is being refreshed. how?
0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Carl TawnSystems and Integration DeveloperCommented:
Check out this other article for a basic intro to retrieving data from a database:

http://myvirtualplayground.co.uk/blog/Fundamentals_Reading_From_a_Database_Part_1

But, simplisticly, you want to query the database, pull the results into a DataReader and bind that to the repeater. Something like:
string connString = "Server=(local);Initial Catalog=AdventureWorks;Integrated Security=SSPI;";
SqlConnection conn = new SqlConnection(connString);

SqlCommand cmd = new SqlCommand("SELECT [FirstName], [LastName] FROM [Person.Contact]", conn);

conn.Open();
SqlDataReader reader = cmd.ExecuteReader();

YourRepeater.DataSource = reader;
YourRepeater.DataBind();

conn.Close();

Open in new window


You would need a bit of exception handling in there to keep things neat, but i've omitted that to focus on the database side of things.
0
HuaMin ChenProblem resolverAuthor Commented:
Many thanks.
Can I have anything like Textbox or Label, within Repeater below, then I can further refresh it with the records retrieved from the table?
<asp:Repeater ID="AccountRepeater" runat="server">
	<HeaderTemplate>
		<div class="headerFooter">Active Accounts</div>
	</HeaderTemplate>
	<SeparatorTemplate>
		<hr />
	</SeparatorTemplate>
	<FooterTemplate>
		<div class="headerFooter">This is the footer;</div>
	</FooterTemplate>
</asp:Repeater>

Open in new window

0
Carl TawnSystems and Integration DeveloperCommented:
Yes, you can use any control you like in the repeater sections. Just remember that the HeaderTemplate and FooterTemplate sections will only appear once for the whole repeater, rather than once per row of data.
0
HuaMin ChenProblem resolverAuthor Commented:
Thanks a lot.

If I want to have Linkbuttons inside the Repeater, how to update Text of Linkbutton inside the repeater, to the column values of the reader?
0
Carl TawnSystems and Integration DeveloperCommented:
You can use the Eval() syntax with the properties of any controls:
<asp:LinkButton ID="Link1" runat="server" Text='<%# Eval("YourField") %>' />

Open in new window

Note the use of apostrophes for the Text attribute.
0
HuaMin ChenProblem resolverAuthor Commented:
Thanks a lot.
Here is the repeater
    <asp:Repeater ID="Repeater" runat="server">
	    <HeaderTemplate>
		    <asp:LinkButton ID="Link1" runat="server" Text='<%# Eval("row1") %>' />
	    </HeaderTemplate>
	    <SeparatorTemplate>
		    <hr />
	    </SeparatorTemplate>
	    <FooterTemplate>
		    <div class="headerFooter">This is the footer;</div>
	    </FooterTemplate>
    </asp:Repeater>

Open in new window

Here are the codes that are executed upon that one button is clicked.
            SqlConnection conn = new SqlConnection(ConfigurationMancnt1r.ConnectionStrings["Mssqlconn2"].ConnectionString);
            SqlCommand cmd;
            SqlDataReader reader;

            conn.Open();
            try
            {
                cmd = new SqlCommand("SELECT top 5 ite_abbr+' '+ite_name+' '+ltrim(rtrim(cast(cnt1 as varchar)))+' '+pla1+' '+pla2 row1 FROM dbo.ite_tab order by 1", conn);

                reader = cmd.ExecuteReader();
                Repeater.DataSource = reader;
                Repeater.DataBind();
            }
            catch (Exception ex)
            {
                lb_msg.Text = ex.Messcnt1 + ex.Source;
            }
            finally
            {
                conn.Close();
            }

Open in new window

But I only get the repeater shown in Fig 1
t549.png
0
Carl TawnSystems and Integration DeveloperCommented:
That's because you aren't currently using the ItemTemplate section. The ItemTemplate is the section that display something per row. What you currently have as the HeaderTemplate should be the ItemTemplate.

If you look back at the article, the 3rd code sample in the "Defining the Repeater" section shows the ItemTemplate and AlternatingItemTemplate sections in use.
0
HuaMin ChenProblem resolverAuthor Commented:
Many thanks. Upon that the relevant Linkbutton row within ItemTemplate is being clicked, how can we know the current Text of the Linkbutton row?
0
Carl TawnSystems and Integration DeveloperCommented:
You just need a generic click handler for all of the LinkButtons in your repeater, and then cast the sender as a LinkButton object and retrieve its Text:
        protected void LinkButton_Click(object sender, EventArgs e)
        {
            LinkButton button = (LinkButton)sender;
            string text = button.Text;
        }

Open in new window

0
HuaMin ChenProblem resolverAuthor Commented:
Sorry, how do you put the reference to LinkButton_Click within Repeater, in the Markup page?
0
Carl TawnSystems and Integration DeveloperCommented:
<asp:LinkButton ID="MyLinkButton" runat="server" Text="Something" OnClick="LinkButton_Click" />
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.