GridView not entering Line Editing mode when inside FormView


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;
}
 
Ad

Advertisements

S

Steven Cheng[MSFT]

Hi Adar,

Regarding on this issue, I have tested the page you provided and did repro
the same behavior as you mentioned. When the outer FormView choose to use
the Session Variable based select statement, the inner nested Gridview can
not turn into Edit mode. Currently, I'll do some further research on this
and will update you if I have any new progress.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Steven Cheng[MSFT]

Hi Adar,

Regarding on this issue, I've performed some further research and haven't
got any progress so far. Currently, I will submit a bug request internally
for this issue. If there is any new information, I'll update you as soon as
possible.

Thanks for your understanding!

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hi Steven,

Thanks for the update.

The solution for this issue is not urgent for me. The behavior is not what I
expected, but I do have a workaround. I removed the SessionParameter from
the ObjectDataSource parameter collection and from the method being invoked
and when I need to use the Session variable to get my data, I just access the
Session variable using HttpContext.Current.Session.

This is less clean, but it works ...

Thanks again.
 
S

Steven Cheng[MSFT]

Thanks for your reply Adar,

Since I have submit the bug request, I'll continue to wait for some
further information on this from product team. I'll update the thread
whenever I get any further feedback from product team side. If you have
interests, still welcome to followup here.

Thanks again for your posting!

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
Ad

Advertisements

S

Steven Cheng[MSFT]

Hi Adar,

I just got some update from the product team engineer about the reported
bug of this issue. Here is some comments and a possible workaround from
them:

=========================================
Looks like it¡¯s an issue with the order that events fire. The problem is
that when the FormView is being databound, the GridView is re-loaded.
You¡¯ll have to set the EditIndex after the controls are bound. Here¡¯s
the order of events:

FormView Init
GridView Init
FormView ItemCreated
FormView Load
GridView Load
GridView OnRowEditing ? EditIndex set here
FormView DataBinding
GridView Unload ?All your settings lost here
GridView Init
GridView Load
FormView ItemCreated
GridView OnDataBinding
Etc.

So even if you handle the OnRowEditing event on the GridView and force the
EditIndex property, when the FormView is databound, the GridView is
unloaded and you lose your changes.

I came up with a workaround that may work for your customer. See the
updated default.aspx page below. It stores the EditIndex in a private
variable and then sets the datagrid during the page Render.

<%@ Page Language="C#" AutoEventWireup="true" %>
<!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>
<script language="c#" runat="server">
private int _editIndex;

protected void GridView1_OnRowEditing(object sender,
GridViewEditEventArgs e)
{
_editIndex = e.NewEditIndex;
}

protected override void Render(HtmlTextWriter writer)
{
if(Page.IsPostBack)
((GridView)FormView1.FindControl("GridView1")).EditIndex =
_editIndex;
base.Render(writer);

}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FormView ID="FormView1" runat="server"
DataSourceID="FormViewODS" DefaultMode="Edit">
<EditItemTemplate>
<asp:GridView ID="GridView1"
OnRowEditing="GridView1_OnRowEditing" 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>
=================================

Hope this helps.


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top