By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,601 Members | 988 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,601 IT Pros & Developers. It's quick & easy.

Insert Operation with Grid

P: 1
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 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

Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="grid.aspx.cs" Inherits="testapp.grid" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head runat="server">
  7.     <title>Grid</title>
  8. </head>
  9. <body>
  10.     <form id="form1" runat="server">
  11.     <div>
  12.         <asp:GridView ID="gv1" runat="server" AllowPaging="True" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false" ShowFooter="true" OnRowCommand="gv1_RowCommand">
  13.             <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  14.             <RowStyle BackColor="#EFF3FB" />
  15.             <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
  16.             <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
  17.             <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
  18.             <EditRowStyle BackColor="#2461BF" />
  19.             <AlternatingRowStyle BackColor="White" />
  20.             <Columns>
  21.                 <asp:TemplateField HeaderText="Col1">
  22.                     <ItemTemplate>
  23.                         <asp:Label ID="Label3" runat="server" Text='<%# GetValue(1) %>' Width="300px"></asp:Label>
  24.                     </ItemTemplate>
  25.                     <FooterTemplate>
  26.                         <asp:TextBox ID="txtNewCol1" runat="server"></asp:TextBox>
  27.                     </FooterTemplate>
  28.                 </asp:TemplateField>
  29.                 <asp:TemplateField HeaderText="Col2">
  30.                     <ItemTemplate>
  31.                         <asp:Label ID="Label3" runat="server" Text='<%# GetValue(2) %>' Width="300px"></asp:Label>
  32.                     </ItemTemplate>
  33.                     <FooterTemplate>
  34.                         <asp:TextBox ID="txtNewCol2" runat="server"></asp:TextBox>
  35.                     </FooterTemplate>
  36.                 </asp:TemplateField>                
  37.                 <asp:TemplateField HeaderText="Record Operations">
  38.                     <FooterTemplate>
  39.                         <asp:Button ID="btnAddNew" runat="server" CommandName="AddNew" Text="Add New Record" />
  40.                         <asp:Button ID="btnSaveNew" runat="server" CommandName="SaveNew" Text="Save" />
  41.                         <asp:Button ID="btnCancelNew" runat="server" CommandName="CancelNew" Text="Cancel" />
  42.                     </FooterTemplate>
  43.                 </asp:TemplateField>
  44.             </Columns>
  45.         </asp:GridView>        
  46.     </div>
  47.     </form>
  48. </body>
  49. </html>
  50.  
  51. using System;
  52. using System.Data;
  53. using System.Configuration;
  54. using System.Collections;
  55. using System.Web;
  56. using System.Web.Security;
  57. using System.Web.UI;
  58. using System.Web.UI.WebControls;
  59. using System.Web.UI.WebControls.WebParts;
  60. using System.Web.UI.HtmlControls;
  61.  
  62. namespace testapp
  63. {
  64.     public partial class grid : System.Web.UI.Page
  65.     {
  66.         protected DataTable FetchData()
  67.         {
  68.             if (Session["dt"] == null)
  69.             {
  70.                 DataTable dt = new DataTable();
  71.                 dt.Columns.Add("Col1");
  72.                 dt.Columns.Add("Col2");
  73.                 dt.Rows.Add("a", 1);
  74.                 dt.Rows.Add("b", 2);
  75.                 dt.Rows.Add("c", 3);
  76.                 dt.Rows.Add("d", 4);
  77.                 Session["dt"] = dt;
  78.             }
  79.             return (DataTable)Session["dt"];
  80.         }
  81.  
  82.         protected void InsertData(string Col1Val, string Col2Val)
  83.         {
  84.             DataTable dt = (DataTable)Session["dt"];
  85.             dt.Rows.Add(Col1Val, Col2Val);
  86.         }
  87.  
  88.         protected void FillInGrid()
  89.         {
  90.             DataTable dt = FetchData();
  91.             if (dt.Rows.Count > 0)
  92.             {
  93.                 gv1.DataSource = dt;
  94.                 gv1.DataBind();
  95.             }
  96.         }
  97.  
  98.         public string GetValue(int ndxCol)
  99.         {
  100.             return (string)Eval("Col" + ndxCol);
  101.         }
  102.  
  103.         protected void Page_Load(object sender, EventArgs e)
  104.         {
  105.             if (!IsPostBack)
  106.             {
  107.                 FillInGrid();
  108.             }
  109.             PerformCommand(null);
  110.         }
  111.  
  112.         protected void gv1_RowCommand(object sender, GridViewCommandEventArgs e)
  113.         {
  114.             PerformCommand(e.CommandName);
  115.         }
  116.  
  117.         protected void PerformCommand(string CommandName)
  118.         {
  119.             bool AddNew = CommandName == "AddNew";
  120.  
  121.             TextBox txtNewCol1 = (TextBox)gv1.FooterRow.FindControl("txtNewCol1");
  122.             TextBox txtNewCol2 = (TextBox)gv1.FooterRow.FindControl("txtNewCol2");
  123.             Button btnAddNew = (Button)gv1.FooterRow.FindControl("btnAddNew");
  124.             Button btnSaveNew = (Button)gv1.FooterRow.FindControl("btnSaveNew");
  125.             Button btnCancelNew = (Button)gv1.FooterRow.FindControl("btnCancelNew");
  126.  
  127.             txtNewCol1.Visible = AddNew;
  128.             txtNewCol2.Visible = AddNew;
  129.             btnAddNew.Visible = !AddNew;
  130.             btnSaveNew.Visible = AddNew;
  131.             btnCancelNew.Visible = AddNew;
  132.  
  133.             if (CommandName == "SaveNew")
  134.             {
  135.                 InsertData(txtNewCol1.Text, txtNewCol2.Text);
  136.                 FillInGrid();
  137.             }
  138.         }
  139.     }
  140. }
  141.  
Jan 15 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.