Elshen Zamanov

Elshen Zamanov

  • NA
  • 4
  • 3.4k

Edit, delete and update in gridview

Aug 24 2013 7:58 AM
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: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...

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

Answers (1)