Solved

repeater control - get values

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now