Solved

repeater control - get values

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp.net mvc 2 34
VB.Net  Exporting Excel Woorkbook to PDF 7 59
vb.net datagrid point 4 21
Set arraylist as datatable 2 7
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 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