none
Edit, delete and update in gridview RRS feed

  • Question

  • 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();
    }
    }
    • Moved by Damon Bu - MSFT Sunday, August 25, 2013 2:25 AM it is a ASP.NET thread
    Saturday, August 24, 2013 11:51 AM

Answers

  • Hello elshen-7,

    Welcome to MSDN forum.

    From your description, this thread is related to ASP.NET forum which is more suitable for it.

    To solve it ASAP, you'll need to post it in the dedicated ASP.NET Forum for more efficient responses, where you can contact ASP.NET experts.

    ASP.NET forum :http://forums.asp.net

    Thanks for your cooperation.

    Have a nice day!
    Damon


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Sunday, August 25, 2013 2:24 AM