Insert Operation with GridView

Insert Operation with GridView

am 15.01.2008 16:26:43 von 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" %>

www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



Grid




CellPadding="4" ForeColor="#333333" GridLines="None"
AutoGenerateColumns="false" ShowFooter="true"
OnRowCommand="gv1_RowCommand">
ForeColor="White" />

HorizontalAlign="Center" />
ForeColor="#333333" />
ForeColor="White" />








asp:TextBox>







asp:TextBox>




CommandName="AddNew" Text="Add New Record" />
CommandName="SaveNew" Text="Save" />
CommandName="CancelNew" Text="Cancel" />









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();
}
}
}
}