P
patogenic
I want to use the grid for record insertion. Everything works fine
except after saving the new record;
all controls for record insertion are still visible (although they
are not set to be) besides the "Add New" button.
I think there is a viewstate issue here, but i could not find out
where to update the state of the insertion controls.
I don't want to set visibility for the insertion controls
declaratively. I am not open to other solutions than what i am trying
to do here,
because i want to grasp exactly how viewstate works for the gridview
and its child controls.
(i have read articles from infinitiesloop and other famous ones about
viewstates.)
Thanks
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="grid.aspx.cs" Inherits="testapp.grid" %>
<!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>Grid</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gv1" runat="server" AllowPaging="True"
CellPadding="4" ForeColor="#333333" GridLines="None"
AutoGenerateColumns="false" ShowFooter="true"
OnRowCommand="gv1_RowCommand">
<FooterStyle BackColor="#507CD1" Font-Bold="True"
ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<PagerStyle BackColor="#2461BF" ForeColor="White"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True"
ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True"
ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Col1">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<
%# GetValue(1) %>' Width="300px"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewCol1" runat="server"></
asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Col2">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<
%# GetValue(2) %>' Width="300px"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewCol2" runat="server"></
asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Record Operations">
<FooterTemplate>
<asp:Button ID="btnAddNew" runat="server"
CommandName="AddNew" Text="Add New Record" />
<asp:Button ID="btnSaveNew" runat="server"
CommandName="SaveNew" Text="Save" />
<asp:Button ID="btnCancelNew" runat="server"
CommandName="CancelNew" Text="Cancel" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
namespace testapp
{
public partial class grid : System.Web.UI.Page
{
protected DataTable FetchData()
{
if (Session["dt"] == null)
{
DataTable dt = new DataTable();
dt.Columns.Add("Col1");
dt.Columns.Add("Col2");
dt.Rows.Add("a", 1);
dt.Rows.Add("b", 2);
dt.Rows.Add("c", 3);
dt.Rows.Add("d", 4);
Session["dt"] = dt;
}
return (DataTable)Session["dt"];
}
protected void InsertData(string Col1Val, string Col2Val)
{
DataTable dt = (DataTable)Session["dt"];
dt.Rows.Add(Col1Val, Col2Val);
}
protected void FillInGrid()
{
DataTable dt = FetchData();
if (dt.Rows.Count > 0)
{
gv1.DataSource = dt;
gv1.DataBind();
}
}
public string GetValue(int ndxCol)
{
return (string)Eval("Col" + ndxCol);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillInGrid();
}
PerformCommand(null);
}
protected void gv1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
PerformCommand(e.CommandName);
}
protected void PerformCommand(string CommandName)
{
bool AddNew = CommandName == "AddNew";
TextBox txtNewCol1 =
(TextBox)gv1.FooterRow.FindControl("txtNewCol1");
TextBox txtNewCol2 =
(TextBox)gv1.FooterRow.FindControl("txtNewCol2");
Button btnAddNew =
(Button)gv1.FooterRow.FindControl("btnAddNew");
Button btnSaveNew =
(Button)gv1.FooterRow.FindControl("btnSaveNew");
Button btnCancelNew =
(Button)gv1.FooterRow.FindControl("btnCancelNew");
txtNewCol1.Visible = AddNew;
txtNewCol2.Visible = AddNew;
btnAddNew.Visible = !AddNew;
btnSaveNew.Visible = AddNew;
btnCancelNew.Visible = AddNew;
if (CommandName == "SaveNew")
{
InsertData(txtNewCol1.Text, txtNewCol2.Text);
FillInGrid();
}
}
}
}
except after saving the new record;
all controls for record insertion are still visible (although they
are not set to be) besides the "Add New" button.
I think there is a viewstate issue here, but i could not find out
where to update the state of the insertion controls.
I don't want to set visibility for the insertion controls
declaratively. I am not open to other solutions than what i am trying
to do here,
because i want to grasp exactly how viewstate works for the gridview
and its child controls.
(i have read articles from infinitiesloop and other famous ones about
viewstates.)
Thanks
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="grid.aspx.cs" Inherits="testapp.grid" %>
<!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>Grid</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gv1" runat="server" AllowPaging="True"
CellPadding="4" ForeColor="#333333" GridLines="None"
AutoGenerateColumns="false" ShowFooter="true"
OnRowCommand="gv1_RowCommand">
<FooterStyle BackColor="#507CD1" Font-Bold="True"
ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<PagerStyle BackColor="#2461BF" ForeColor="White"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True"
ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True"
ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Col1">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<
%# GetValue(1) %>' Width="300px"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewCol1" runat="server"></
asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Col2">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<
%# GetValue(2) %>' Width="300px"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewCol2" runat="server"></
asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Record Operations">
<FooterTemplate>
<asp:Button ID="btnAddNew" runat="server"
CommandName="AddNew" Text="Add New Record" />
<asp:Button ID="btnSaveNew" runat="server"
CommandName="SaveNew" Text="Save" />
<asp:Button ID="btnCancelNew" runat="server"
CommandName="CancelNew" Text="Cancel" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
namespace testapp
{
public partial class grid : System.Web.UI.Page
{
protected DataTable FetchData()
{
if (Session["dt"] == null)
{
DataTable dt = new DataTable();
dt.Columns.Add("Col1");
dt.Columns.Add("Col2");
dt.Rows.Add("a", 1);
dt.Rows.Add("b", 2);
dt.Rows.Add("c", 3);
dt.Rows.Add("d", 4);
Session["dt"] = dt;
}
return (DataTable)Session["dt"];
}
protected void InsertData(string Col1Val, string Col2Val)
{
DataTable dt = (DataTable)Session["dt"];
dt.Rows.Add(Col1Val, Col2Val);
}
protected void FillInGrid()
{
DataTable dt = FetchData();
if (dt.Rows.Count > 0)
{
gv1.DataSource = dt;
gv1.DataBind();
}
}
public string GetValue(int ndxCol)
{
return (string)Eval("Col" + ndxCol);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillInGrid();
}
PerformCommand(null);
}
protected void gv1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
PerformCommand(e.CommandName);
}
protected void PerformCommand(string CommandName)
{
bool AddNew = CommandName == "AddNew";
TextBox txtNewCol1 =
(TextBox)gv1.FooterRow.FindControl("txtNewCol1");
TextBox txtNewCol2 =
(TextBox)gv1.FooterRow.FindControl("txtNewCol2");
Button btnAddNew =
(Button)gv1.FooterRow.FindControl("btnAddNew");
Button btnSaveNew =
(Button)gv1.FooterRow.FindControl("btnSaveNew");
Button btnCancelNew =
(Button)gv1.FooterRow.FindControl("btnCancelNew");
txtNewCol1.Visible = AddNew;
txtNewCol2.Visible = AddNew;
btnAddNew.Visible = !AddNew;
btnSaveNew.Visible = AddNew;
btnCancelNew.Visible = AddNew;
if (CommandName == "SaveNew")
{
InsertData(txtNewCol1.Text, txtNewCol2.Text);
FillInGrid();
}
}
}
}