Solved

repeater control - get values

Posted on 2011-03-18
4
488 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
  • 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 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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