473,379 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Getting the value for a clicked row in a Repeater or GridView

We have been having this problem for months and always assumed it was because
of our somewhat complicated setup. I have just spent almost all of today
making this into a simple example that can be easily reproduced and it seems
like a major .NET flaw/limitation that I was hoping you could explain to me
as it is really frustrating me.

I just want to be able to get info for a row clicked, whether it is a
Repeater, GridView, etc. I have tried Command Argument, asp:HiddenField, etc.
and if I use .NET it just cannot be done as far as I can tell. I have always
had to work around it by manually making a normal HTML hidden form element
and using Request.Form.

Is there a reason this is so darn difficult? Code is attached, I humbly
request the error of my ways. You should be able to just launch it in debug,
click on a delete link and see that it doesn’t retrieve the value of the
label in the row.

If I use a GridView *with an objectdatasource* I can get the Keys feature of
the gridview to give me what I want, but that is a very cumbersome and
limited way of getting data for a row. I want to be able to quickly add a
button to a repeater that knows which row it is being clicked on.

App_Code/EntityHoliday.cs
------------------------------
using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for EntityHoliday
/// </summary>
public class EntityHoliday
{
private int mId;
public int Id
{
get { return mId; }
set { mId = value; }
}

private string mDescription;
public string Description
{
get { return mDescription; }
set { mDescription = value; }
}

public EntityHoliday(int pId, string pDescription)
{
mId = pId;
mDescription = pDescription;
}

public static List<EntityHolidayGetEntityHolidays()
{
List<EntityHolidayt = new List<EntityHoliday>();
t.Add(new EntityHoliday(1, "test1"));
t.Add(new EntityHoliday(2, "test2"));
t.Add(new EntityHoliday(3, "test3"));
t.Add(new EntityHoliday(4, "test4"));

return t;
}
}

Default.aspx
---------------

<%@ Page Language="C#" 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 id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetEntityHolidays" TypeName="EntityHoliday" />
<asp:GridView ID="UserHolidays" runat="server"
DataSourceID="ObjectDataSource1" AutoGenerateColumns="False"
OnRowDeleting="UserHolidays_RowDeleting" DataKeyNames="Id">
<Columns>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<%#Eval("Description")%>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:LinkButton id="DeleteButton" runat=server
CommandName="Delete">
<img border=0
src="../resources/icons/delete.gif" />
Delete
</asp:LinkButton>
<asp:Label runat=server
ID="TestItem"><%#Eval("Id")%></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

</form>
</body>
</html>

Default.aspx.cs
------------------

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Expires = 0;
}

//delete a row
protected void UserHolidays_RowDeleting(object sender,
GridViewDeleteEventArgs e)
{
Response.Write("<BR>Key: " + e.Keys[0]); //e.Keys[0] works here but
doesn't work unless objectdatasource is used, i.e. can't use
gridview.datasource/bind which is a hassle because objectdatasource requires
a class with no constructors and won't work with a repeater

//THIS DRIVES ME NUTS! I JUST WANT TO GET A VALUE FOR THE RECORD FOR
THE ROW CLICKED.
Response.Write("<BR>Label text: " +
(UserHolidays.Rows[e.RowIndex].FindControl("TestItem") as Label).Text);

Response.End();
}

}
Dec 27 '06 #1
0 1705

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kevin | last post by:
I've got a problem where I need to know the value of a hidden field inside a repeater once a button is clicked. Ths code inside my repeater looks like this: <input id="conf_num" type="hidden"...
3
by: renil | last post by:
I have a repeater control that displays info. from a datatable. Each row in the repeater has a checkbox. Also, I have a delete linkbutton outside the repeater control. What I'm trying to do when...
3
by: shapper | last post by:
Hello, I need to display a list of records. Each record has 3 columns: An image, a Label and a Hyperlink. I don't need paging or even raise events. All I need is to display the data as I...
3
by: Jeff | last post by:
Hey ASP.NET 2.0 Below you see the code I'm having problem with. In the Open_Message event/method I want to get the value of the HiddenField at the row in the repeater control I clicked.... my...
0
by: warren_rapson | last post by:
Hi all, I am a newbie to this awesome framework - i love it. My situation: - I have a master page with a menu on it and a ContenPlaceHolder containing a DataList - The menu is dynamic and is...
2
by: Jeff | last post by:
Hey ASP.NET 2.0 GridView have AllowPaging & PageSize for the letting the GridView span multiple pages. But is the same allowed on a Repeater control? I didn't see any properties like...
1
by: HockeyFan | last post by:
I'm having trouble getting information passed to a UserControl that I wrote. Within the control is an ObjectDataSource that populates a GridView on the control. The Select method for the...
0
by: joelgarcia007 | last post by:
I really hope someone can help me with this because it's really driving me crazy. I need to dynamically display multiple data tables on a page so I am using a Repeater to do so and in the...
5
by: =?Utf-8?B?Um9iZXJ0IFNtaXRo?= | last post by:
Hi, I wish to create a repeater control in c#.net that allows for the selection of rows, any idea how this can be done. Regards Robert
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.