G
Guest
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<EntityHoliday> GetEntityHolidays()
{
List<EntityHoliday> t = 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();
}
}
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<EntityHoliday> GetEntityHolidays()
{
List<EntityHoliday> t = 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();
}
}