?
Solved

repeater control - get values

Posted on 2011-03-18
4
Medium Priority
?
507 Views
Last Modified: 2012-05-11
I have a repeater control laid out as shown in the code attached.

Now, if the user clicks on the lnkRemoveCategory button I need the value of "Path" passed to a asp.net code procedure so that I can do some stuff.

How?
<asp:Repeater ID="rpCategories" runat="server">
                                        <ItemTemplate>                                                                         
                                           <font size="2"><%#Eval("Path")%> &nbsp;&nbsp;&nbsp;&nbsp;
                                               <asp:LinkButton ID="lnkRemoveCategory" runat="server">Remove</asp:LinkButton></font> 
                                           <hr />
                                     
                                    </ItemTemplate>
                                
                                </asp:Repeater>

Open in new window

0
Comment
Question by:vbnetcoder
[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
  • 2
  • 2
4 Comments
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35169970
You can set the CommandArgument of the LinkButton to <%# Eval("Path") %>, and then handle the LinkButton's "Command" event.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.commandargument.aspx#Y760

<script runat="server">
    private void lnkRemoveCategory_Command(object sender, CommandEventArgs e)
    {
        string path = (string)e.CommandArgument;
    }
</script>

<asp:Repeater ID="rpCategories" runat="server">
  <ItemTemplate>                                                                         
      <font size="2"><%#Eval("Path")%> &nbsp;&nbsp;&nbsp;&nbsp;
      <asp:LinkButton ID="lnkRemoveCategory" runat="server" CommandArgument='<%# Eval("Path") %>' OnCommand="lnkRemoveCategory_Command">Remove</asp:LinkButton></font> 
      <hr />                                  
  </ItemTemplate>                                
</asp:Repeater>

Open in new window

0
 

Author Comment

by:vbnetcoder
ID: 35169985
Could it call a code behind function?
0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 2000 total points
ID: 35170321
That is code-behind. Putting the C# in a <script runat="server">...</script> tag lets you put code-behind statements in the same file as the HTML markup, and skip the separate .aspx.cs file. I just did it that way so there was only one code snippet, thought it'd be easier to read.

Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
	<title></title>
</head>
<body>
	<form id="form1" runat="server">
	<div>
		
		<asp:Repeater ID="rpCategories" runat="server" DataSourceID="testDataSource">
			<ItemTemplate>
				<font size="2">
					<asp:Label ID="pathLabel" runat="server" Text='<%# Eval("JobTitle") %>' />
					<br />
					<asp:LinkButton ID="lnkRemoveCategory" runat="server" CommandArgument='<%# Eval("JobTitle") %>'
						OnCommand="lnkRemoveCategory_Command" Text="Remove" />
				</font>
				<hr />
			</ItemTemplate>
		</asp:Repeater>
	</div>
	<asp:SqlDataSource ID="testDataSource" runat="server" ConnectionString="Data Source=.\SQLExpress;Initial Catalog=TestDB;Persist Security Info=True;User ID=sa"
			ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM [JobTable]" />
	</form>
</body>
</html>

Open in new window


Default.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

/// <summary>
/// Summary description for Default
/// </summary>
public partial class _Default : Page
{
	protected void lnkRemoveCategory_Command(object sender, CommandEventArgs e)
	{
		string path = (string)e.CommandArgument;

		using (SqlConnection cnx = new SqlConnection(@"Data Source=.\SQLExpress;Initial Catalog=TestDB;Persist Security Info=True;User ID=sa"))
		{
			cnx.Open();
			using (SqlCommand cmd = cnx.CreateCommand())
			{
				cmd.CommandText = "DELETE FROM JobTable WHERE JobTitle = @jobtitle";
				cmd.Parameters.AddWithValue("@jobtitle", path).DbType = System.Data.DbType.String;
				cmd.ExecuteNonQuery();
			}
			cnx.Close();
		}

		rpCategories.DataBind();
	}
}

Open in new window

0
 

Author Closing Comment

by:vbnetcoder
ID: 35171630
Thank you!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

719 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