user2508738
user2508738

Reputation: 33

GridView dissapears when clicking on Edit-Button

I've searched for two days now and can't find a solution for my problem. I'm programming a webpart with connection to a database with Visual Studio.

So I've made a GridView to show some data listed in the database and the user is able to edit and update the data in it. Before you see the GridView, you have to click the button "load table". But when I click on "edit" on the GridView, the GridView dissapears and only shows up when I click the button "load table" again, with the GridView in edit-mode.

ASP

<asp:Button ID="btnload" runat="server" Text="load table" 
            onclick="btnload_Click" />
<asp:GridView ID="gridtable" runat="server" BackColor="White" 
              BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px"
              CellPadding="4" EnableModelValidation="True" 
              OnRowCancelingEdit="gridtable_RowCancelingEdit" 
              OnRowEditing="gridtable_RowEditing" 
              OnRowUpdating="gridtable_RowUpdating" >
  <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
  <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
  <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
  <RowStyle BackColor="White" ForeColor="#330099" />
  <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
  <Columns>
    <asp:TemplateField HeaderText="P_number">
      <ItemTemplate>
        <%#Eval("P_number")%>
      </ItemTemplate>
      <EditItemTemplate>
        <asp:TextBox ID="textbox1" runat="server" Text='<%#Eval("P_number")%>'>
        </asp:TextBox>
      </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="lastname">
      <ItemTemplate>
        <%#Eval("lastname")%>
      </ItemTemplate>
      <EditItemTemplate>
        <asp:TextBox ID="textbox2" runat="server" Text='<%#Eval("lastname")%>'>
        </asp:TextBox>
      </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="PN_ch">
      <ItemTemplate>
        <%#Eval("PN_ch")%>
      </ItemTemplate>
      <EditItemTemplate>
        <asp:TextBox ID="textbox3" runat="server" Text='<%#Eval("PN_ch")%>'>
        </asp:TextBox>
      </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="lastname_ch">
      <ItemTemplate>
        <%#Eval("lastname_ch")%>
      </ItemTemplate>
      <EditItemTemplate>
        <asp:TextBox ID="textbox4" runat="server" Text='<%#Eval("lastname_ch")%>'>
        </asp:TextBox>
      </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="workplace">
      <ItemTemplate>
        <%#Eval("workplace")%>
      </ItemTemplate>
      <EditItemTemplate>
        <asp:TextBox ID="textbox5" runat="server" Text='<%#Eval("workplace")%>'>
        </asp:TextBox>
      </EditItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>

C#

public SqlDataSource datasource;

protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnload_Click(object sender, EventArgs e)
{        

    openConnection(getconstring());
    gridtable.AutoGenerateColumns = false;
    gridtable.AutoGenerateEditButton = true;
    datasource = new SqlDataSource(getconstring(), "SELECT * FROM    T_Employees");
    BindData();
}

public void BindData()
{

    try
    {
        gridtable.DataSource = datasource;
        gridtable.DataBind();
    }
    catch (Exception e)
    {
        //Do something
    }
}

protected void gridtable_RowEditing(object sender, GridViewEditEventArgs e)
{ 

    gridtable.EditIndex = e.NewEditIndex;
    BindData();   
}

protected void gridtable_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

    //get EditIndex
    GridViewRow row = gridtable.Rows[e.RowIndex];

    // save changes
    string pnumber = ((row.Cells[1].Controls[0]).ToString());
    string lastname = ((row.Cells[2].Controls[0]).ToString());
    string pn_ch = ((row.Cells[3].Controls[0]).ToString());
    string lastname_ch = ((row.Cells[4].Controls[0]).ToString());
    string workplace = ((row.Cells[5].Controls[0]).ToString());

    //Update
    datasource.UpdateCommand = "UPDATE T_Employees SET P_number='"+pnumber+"', lastname='" 
                                +lastname+"', PN_ch='"+pn_ch+"', lastname_ch='"
                                +lastname_ch+"', workplace='"+workplace+"'";
    datasource.Update();

    //reset EditIndex 
    gridtabelle.EditIndex = -1;

    //Bind data
    BindData();
}

protected void gridtable_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{

    gridtable.EditIndex = -1;
    BindData();
}

Upvotes: 0

Views: 2355

Answers (2)

hutchonoid
hutchonoid

Reputation: 33306

You need to store the datasource in session in the button click:

protected void btnload_Click(object sender, EventArgs e)
    {        
            openConnection(getconstring());
            gridtable.AutoGenerateColumns = false;
            gridtable.AutoGenerateEditButton = true;
            datasource = new SqlDataSource(getconstring(), "SELECT * FROM    T_Employees");
            Session["datasource "] = datasource 
            BindData();
    }

Then change your bind to this:

public void BindData()
    {
        try
        {
            gridtable.DataSource = Session["datasource "] ;
            gridtable.DataBind();

        }
        catch (Exception e)
        {
           //Do something
        }
    }

You also need to add this to the web.config:

<system.web>
      <pages enableSessionState="true"> 
 </system.web>

Or this to the page:

<%@Page enableSessionState="true"> 

Upvotes: 2

LittleSweetSeas
LittleSweetSeas

Reputation: 7054

You may just declare your datasource as in your aspx file and you're done. If you then need to bind your grid after a button click, then you have to store somewhere (eg. viewstate) a flag (boolean value) to indicate if your grid has to be binded or not. Then bind the grid only if that flag is true. Don't need to use session at all.

Eg:

protected void Page_Load(object sender, EventArgs e)
{
   if (!isPostBack){
      ViewState["FLAG"] = false;
   }
   else{
      if ((bool)ViewState["FLAG"]) BindData();
   }
}

protected void btnload_Click(object sender, EventArgs e)
{        
   ViewState["FLAG"] = true;
}

Looking @ previous answers, please consider that even if you choose ViewState or Session (each of these have their own advantages / disadvantages), you don't need to store ridundant data. In your case, just a single bit is enough, as you have the code - in your page - to build/bind the data source without storing it interely.

Upvotes: 0

Related Questions