Click here to Skip to main content
15,914,071 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi , i have a gridview and i have edit, delete and update buttons.i can use edit and delete buttons they work but update button doesn't work when i click on update button ,the page refreshes ,doesn't update.please help me.here is my code...

ASP.NET
<asp:GridView ID="GridviewTeams"
                runat="server"
                AutoGenerateColumns="false"
                OnRowUpdating="GV_RowUpdating"
                OnRowDeleting="GV_RowDeleting"
                OnRowEditing="GV_RowEditing"
                OnRowCancelingEdit="GV_RowCancelingEdit">
            <Columns>
                <asp:TemplateField HeaderText="ID">
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="tbx_ID" >
                        </asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label runat="server" ID="GV_lblTeamID" Text='<%# Eval("teamID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Team Name">
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="tbxEditTeamName" ></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label runat="server" ID="lblEditTeamName" Text='<%# Eval("team_name") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Team email">
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="tbxEditTeamEmail" ></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label runat="server" ID="lbl_edit_stadium_adress"  Text='<%# Eval("team_email") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Commands" ShowHeader="False">
                    <EditItemTemplate>
                        <asp:LinkButton ID="LnkBtnUpdate" runat="server" CausesValidation="True"
                            CommandName="Update" Text="Update"></asp:LinkButton>
                        &nbsp;<asp:LinkButton ID="LnkBtnCancel" runat="server" CausesValidation="False"
                            CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:LinkButton ID="LnkBtnEdit" runat="server" CausesValidation="False"
                            CommandName="Edit" Text="Edit"></asp:LinkButton>
                        &nbsp;<asp:LinkButton ID="LnkBtnDelete" runat="server" CausesValidation="False"
                            CommandName="Delete" Text="Delete"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>

            </Columns>
                </asp:GridView>

code behind...

C#
protected void Page_Load(object sender, EventArgs e)
      {
          Bind();
      }
      public void Bind()
      {
          try
          {
              CTeam cTeam = new CTeam();
              GridviewTeams.DataSource = cTeam.GetAllTeams();
              GridviewTeams.DataBind();
          }
          catch (Exception err)
          {
              Response.Write(err.Message);
          }
      }
      protected void BtnAdd_Click(object sender, EventArgs e)
      {
          CTeam cteam = new CTeam();
          cteam.AddTeams(tbxTeamName.Text,tbxTeamEmail.Text);
          Bind();
      }
      protected void GV_RowDeleting(object sender, GridViewDeleteEventArgs e)
      {
          CTeam cteam = new CTeam();
          int indexrow = e.RowIndex;
          short tid = short.Parse(((Label)GridviewTeams.Rows[indexrow].FindControl("GV_lblTeamID")).Text);
          cteam.DeleteTeams(tid);
          Bind();
      }
      protected void GV_RowEditing(object sender, GridViewEditEventArgs e)
      {
          GridviewTeams.EditIndex = e.NewEditIndex;
          Bind();
      }
      protected void GV_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
      {
          GridviewTeams.EditIndex = -1;
          Bind();
      }
      protected void GV_RowUpdating(object sender, GridViewUpdateEventArgs e)
      {
          CTeam cteam = new CTeam();
          int indexrow = e.RowIndex;
          string tname = ((TextBox)GridviewTeams.Rows[e.RowIndex].FindControl("tbxEditTeamName")).Text;
          string temail = ((TextBox)GridviewTeams.Rows[e.RowIndex].FindControl("tbxEditTeamEmail")).Text;
          cteam.UpdateTeams(tname, temail);

          GridviewTeams.EditIndex = -1;
          Bind();
      }
  }
Posted
Updated 24-Aug-13 2:12am
v2

Hi...
For insert,edit,update and delete data in gridview using asp.net.
http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html[^]
Thank u.
 
Share this answer
 
Dear your code is perfect but you make some small mistakes over their with your code first one is firstly just add Text='<%# Bind("teamID") %>' to every textbox inside of EditItemTemplate because when you click on edit existing text will display on textbox.

Second one is you have to add find control statement as like:

TextBox tname = (TextBox)GridviewTeams.Rows[e.RowIndex].FindControl("tbxEditTeamName");
TextBox temail = (TextBox)GridviewTeams.Rows[e.RowIndex].FindControl("tbxEditTeamEmail");


I hope it's helping to you.......:-)
 
Share this answer
 
Comments
Elshen Zamanov 24-Aug-13 8:33am    
sorry , but it also doesn't work.it shows mistake

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900