Solved

repeater control - get values

Posted on 2011-03-18
4
499 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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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