G
Guest
Hi All,
I have a GridView inside the EditItemTemplate of a FormView. Both FormView
and GridView are data bound using an ObjectDataSource. When the FormView's
ObjectDataSource object has a SelectParameters with a SessionParameter in
the parameters collection and the object stored in the Session is a
reference type,
the DataGrid fails to enter Line Editing mode.
Is this a bug? Is there a workaround for this problem?
I have created a minimal sample that demonstrates this problem.
I have 4 files in my sample.
1. Global.asax - initializes a Session state variable.
2. Default.aspx - holds the FormView and the GridView in it.
3. MyDataSource.cs - implements the data source object.
4. MyState.cs - a class to be saved in Session and passed in to the Select
statement using a SessionParameter.
Here is the code:
Global.asax:
-----
<%@ Application Language="C#" %>
<script runat="server">
void Session_Start(object sender, EventArgs e)
{
Session["MySessionVar"] = new MyState("MyValue");
}
</script>
----
End Global.asax
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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FormView ID="FormView1" runat="server" DataSourceID="FormViewODS"
DefaultMode="Edit">
<EditItemTemplate>
<asp:GridView ID="GridView1" runat="server" DataSourceID="GridViewODS"
AutoGenerateEditButton="true">
</asp:GridView>
<asp:ObjectDataSource ID="GridViewODS" runat="server"
TypeName="MyDataSource" SelectMethod="GridViewSelect"
UpdateMethod="GridRowUpdate">
</asp:ObjectDataSource>
</EditItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="FormViewODS" runat="server"
TypeName="MyDataSource" SelectMethod="FormViewSelectWithSession"
EnableViewState="false">
<SelectParameters>
<asp:SessionParameter Name="value" SessionField="MySessionVar"
Type="Object" />
</SelectParameters>
</asp:ObjectDataSource>
</div>
</form>
</body>
</html>
----
End Default.aspx
MyDataSource.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;
/// <summary>
/// Summary description for MyDataSource
/// </summary>
public static class MyDataSource
{
private static System.Data.DataTable data;
private static void CreateData()
{
data = new System.Data.DataTable();
data.Columns.Add("ID", typeof(int));
data.Columns.Add("Name", typeof(string));
data.Columns.Add("Description", typeof(string));
data.Rows.Add(1, "Name1", "Description1");
data.Rows.Add(2, "Name2", "Description2");
data.Rows.Add(3, "Name3", "Description3");
data.Rows.Add(4, "Name4", "Description4");
}
public static System.Data.DataTable FormViewSelect()
{
if (data == null) {
CreateData();
}
return data;
}
public static System.Data.DataTable FormViewSelectWithSession(MyState
value)
{
if (data == null) {
CreateData();
}
return data;
}
public static System.Data.DataTable GridViewSelect()
{
if (data == null) {
CreateData();
}
return data;
}
public static System.Data.DataTable GridViewSelectWithSession(MyState
value)
{
if (data == null) {
CreateData();
}
return data;
}
public static void GridRowUpdate(int id, string name, string description)
{
}
}
----
End MyDataSource.cs
MyState.cs
----
using System;
public class MyState
{
public MyState()
{
Str = String.Empty;
}
public MyState(string str)
{
Str = str;
}
public string Str
{
get { return _str; }
set { _str = value; }
}
private string _str;
}
I have a GridView inside the EditItemTemplate of a FormView. Both FormView
and GridView are data bound using an ObjectDataSource. When the FormView's
ObjectDataSource object has a SelectParameters with a SessionParameter in
the parameters collection and the object stored in the Session is a
reference type,
the DataGrid fails to enter Line Editing mode.
Is this a bug? Is there a workaround for this problem?
I have created a minimal sample that demonstrates this problem.
I have 4 files in my sample.
1. Global.asax - initializes a Session state variable.
2. Default.aspx - holds the FormView and the GridView in it.
3. MyDataSource.cs - implements the data source object.
4. MyState.cs - a class to be saved in Session and passed in to the Select
statement using a SessionParameter.
Here is the code:
Global.asax:
-----
<%@ Application Language="C#" %>
<script runat="server">
void Session_Start(object sender, EventArgs e)
{
Session["MySessionVar"] = new MyState("MyValue");
}
</script>
----
End Global.asax
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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FormView ID="FormView1" runat="server" DataSourceID="FormViewODS"
DefaultMode="Edit">
<EditItemTemplate>
<asp:GridView ID="GridView1" runat="server" DataSourceID="GridViewODS"
AutoGenerateEditButton="true">
</asp:GridView>
<asp:ObjectDataSource ID="GridViewODS" runat="server"
TypeName="MyDataSource" SelectMethod="GridViewSelect"
UpdateMethod="GridRowUpdate">
</asp:ObjectDataSource>
</EditItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="FormViewODS" runat="server"
TypeName="MyDataSource" SelectMethod="FormViewSelectWithSession"
EnableViewState="false">
<SelectParameters>
<asp:SessionParameter Name="value" SessionField="MySessionVar"
Type="Object" />
</SelectParameters>
</asp:ObjectDataSource>
</div>
</form>
</body>
</html>
----
End Default.aspx
MyDataSource.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;
/// <summary>
/// Summary description for MyDataSource
/// </summary>
public static class MyDataSource
{
private static System.Data.DataTable data;
private static void CreateData()
{
data = new System.Data.DataTable();
data.Columns.Add("ID", typeof(int));
data.Columns.Add("Name", typeof(string));
data.Columns.Add("Description", typeof(string));
data.Rows.Add(1, "Name1", "Description1");
data.Rows.Add(2, "Name2", "Description2");
data.Rows.Add(3, "Name3", "Description3");
data.Rows.Add(4, "Name4", "Description4");
}
public static System.Data.DataTable FormViewSelect()
{
if (data == null) {
CreateData();
}
return data;
}
public static System.Data.DataTable FormViewSelectWithSession(MyState
value)
{
if (data == null) {
CreateData();
}
return data;
}
public static System.Data.DataTable GridViewSelect()
{
if (data == null) {
CreateData();
}
return data;
}
public static System.Data.DataTable GridViewSelectWithSession(MyState
value)
{
if (data == null) {
CreateData();
}
return data;
}
public static void GridRowUpdate(int id, string name, string description)
{
}
}
----
End MyDataSource.cs
MyState.cs
----
using System;
public class MyState
{
public MyState()
{
Str = String.Empty;
}
public MyState(string str)
{
Str = str;
}
public string Str
{
get { return _str; }
set { _str = value; }
}
private string _str;
}