Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Finally i was able to create a custom delete and edit button for my gridview. But I have noticed two problems in my gridview. Hope you guys can help me on this one. Also, this is only for practice. I will implement better security in my website soon.

Problem 1: When I click the delete button for the first time, it just refresh the page and does nothing. But when i click it for the second time, it executes perfectly. Why the delete button is not working and just refreshing page when clicked for the first time and then works at the second time?
Problem 2: When I click the edit button for the first time, it works fine. Then, i can update/change the database. Lets say i want to change the name "Dean" under id "7" to "Jackson". So I just simply changed it in the textbox and clicked the update button. But the website just refresh again and the name "Dean" is still in the textbox(by the way, this is still in update/cancel mode). So I need to type "Jackson" again in the textbox and click the update button for the second time. This time it works and it did update my gridview. Similar to problem 1, why the update button is not working and just refreshing page when clicked for the first time and works at the second time?

What I have tried:

Here is the aspx code:
<h3>Guitar Brands Data:</h3>
    <div style="overflow:auto; width:1100px; max-height:500px;">
        <asp:GridView ID="GuitarBrandsGridView" runat="server" CssClass="mydatagrid" PagerStyle-CssClass="pager" HeaderStyle-CssClass="header" RowStyle-CssClass="rows" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource0" OnRowDataBound="GuitarBrandsGridView_RowDataBound" OnRowCancelingEdit="GuitarBrandsGridView_RowCancelingEdit" OnRowEditing="GuitarBrandsGridView_RowEditing" OnRowUpdating="GuitarBrandsGridView_RowUpdating"  OnRowDeleting="GuitarBrandsGridView_RowDeleting" Width="864px" Height="250px">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Button ID="GuitarBrandsGridViewBtnDelete" runat="server" CommandName="Delete" Text="Delete"/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Button ID="GuitarBrandsGridViewBtnEdit" runat="server" CommandName="Edit" Text="Edit"/>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:Button ID="GuitarBrandsGridViewBtnUpdate" runat="server" CommandName="Update" Text="Update"/>
                        <asp:Button ID="GuitarBrandsGridViewBtnCancel" runat="server" CommandName="Cancel" Text="Cancel"/>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="id" SortExpression="id">
                    <EditItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("id") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="type" SortExpression="type">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("type") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("type") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="name" SortExpression="name">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Text='<%# Bind("name") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="image" SortExpression="image">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("image") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label5" runat="server" Text='<%# Bind("image") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <HeaderStyle CssClass="header"></HeaderStyle>
            <PagerStyle CssClass="pager"></PagerStyle>
            <RowStyle CssClass="rows"></RowStyle>
        </asp:GridView>


Here is the aspx.cs code:
C#
protected void Page_Load(object sender, EventArgs e)
    {
        BindGridViewDataList.GetItemsLoad();
    }

    //Start of Gridview Code for Guitar Brands
    private void bindgridviewguitarbrands()
    {
        con1.Open();
        cmd1.CommandText = "SELECT * FROM [guitarBrands]";
        cmd1.Connection = con1;
        SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
        da1.Fill(ds1);
        con1.Close();
        GuitarBrandsGridView.DataBind();
    }

    protected void GuitarBrandsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string name = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Name"));
            Button button = (Button)e.Row.FindControl("GuitarBrandsGridViewBtnDelete");
            button.Attributes.Add("onclick", "JavaScript:return ConfirmationBox('" + name + "' )");
        }
    }

    protected void GuitarBrandsGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        int id = Convert.ToInt32(GuitarBrandsGridView.DataKeys[e.RowIndex].Value.ToString());
        Label name = (Label)GuitarBrandsGridView.Rows[e.RowIndex].FindControl("Label4");

        con1.Open();
        cmd1.CommandText = "DELETE FROM [guitarBrands] WHERE id=" + id;
        cmd1.Connection = con1;
        int a = cmd1.ExecuteNonQuery();
        con1.Close();
        if (a > 0)
        {
            bindgridviewguitarbrands();
        }

        RemoveCodeToGuitarFile.RemoveAddGuitarClass(name.Text);
        RemoveCodeToGuitarFile.RemoveConnectionClassGuitarItems(name.Text);
        RemoveCodeToGuitarFile.RemoveOverviewGuitarDataASPX(name.Text);
        RemoveCodeToGuitarFile.RemoveOverviewGuitarDataCode(name.Text);
        File.Delete(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\GuitarItems" + id + ".aspx");
        File.Delete(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\GuitarItems" + id + ".aspx.cs");
        ConnectionClassGuitarBrands.RemoveGuitarBrandsDatabase(name.Text);

    }

    protected void GuitarBrandsGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string id = GuitarBrandsGridView.DataKeys[e.NewEditIndex].Value.ToString();
        Label name = (Label)GuitarBrandsGridView.Rows[e.NewEditIndex].FindControl("Label4");

        RemoveCodeToGuitarFile.RemoveAddGuitarClass(name.Text);
        RemoveCodeToGuitarFile.RemoveConnectionClassGuitarItems(name.Text);
        RemoveCodeToGuitarFile.RemoveOverviewGuitarDataASPX(name.Text);
        RemoveCodeToGuitarFile.RemoveOverviewGuitarDataCode(name.Text);
        File.Delete(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\GuitarItems" + id + ".aspx");
        File.Delete(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\GuitarItems" + id + ".aspx.cs");
        ConnectionClassGuitarBrands.RemoveGuitarBrandsDatabase(name.Text);

        GuitarBrandsGridView.EditIndex = e.NewEditIndex;
        bindgridviewguitarbrands();
    }
    // row update event
    protected void GuitarBrandsGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string id = GuitarBrandsGridView.DataKeys[e.RowIndex].Value.ToString();
        TextBox type = (TextBox)GuitarBrandsGridView.Rows[e.RowIndex].FindControl("TextBox1");
        TextBox name = (TextBox)GuitarBrandsGridView.Rows[e.RowIndex].FindControl("TextBox2");
        TextBox image = (TextBox)GuitarBrandsGridView.Rows[e.RowIndex].FindControl("TextBox3");

        cmd1 = new SqlCommand("UPDATE [guitarBrands] SET Type = '" + type + "', Name = '" + name + "', Image = '" + image + "' WHERE ID = " + id, con1);
        con1.Open();
        cmd1.ExecuteNonQuery();
        con1.Close();

        int ID = Convert.ToInt32(id);
        ConnectionClassGuitarBrands.CreateGuitarBrandsDatabase(name.Text);
        AddCodeToGuitarFile.AddGuitarClassCode(name.Text, ID);
        AddCodeToGuitarFile.AddConnectionClassGuitarItems(name.Text);
        AddCodeToGuitarFile.AddOverviewGuitarDataASPX(name.Text, ID);
        AddCodeToGuitarFile.AddOverviewGuitarDataASPXCode(name.Text);
        AddASPXAndCSFileForGuitarBrands.AddFile(name.Text, ID);

        GuitarBrandsGridView.EditIndex = -1;
        bindgridviewguitarbrands();  
    }
    // cancel row edit event
    protected void GuitarBrandsGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        string id = GuitarBrandsGridView.DataKeys[e.RowIndex].Value.ToString();
        TextBox name = (TextBox)GuitarBrandsGridView.Rows[e.RowIndex].FindControl("TextBox2");

        int ID = Convert.ToInt32(id);
        ConnectionClassGuitarBrands.CreateGuitarBrandsDatabase(name.Text);
        AddCodeToGuitarFile.AddGuitarClassCode(name.Text, ID);
        AddCodeToGuitarFile.AddConnectionClassGuitarItems(name.Text);
        AddCodeToGuitarFile.AddOverviewGuitarDataASPX(name.Text, ID);
        AddCodeToGuitarFile.AddOverviewGuitarDataASPXCode(name.Text);
        AddASPXAndCSFileForGuitarBrands.AddFile(name.Text,ID);

        GuitarBrandsGridView.EditIndex = -1;
        bindgridviewguitarbrands();
    }

    //End of Gridview Code for Guitar Brands
Posted
Updated 8-Mar-17 16:39pm

1 solution

Add IsPostBack Condition in Page_Load event

protected void Page_Load(object sender, EventArgs e)
       {
           if (!Page.IsPostBack)
           {
               BindGridViewDataList.GetItemsLoad();
           }
       }
 
Share this answer
 
Comments
BebeSaiyan 8-Mar-17 22:50pm    
i tried it but its still having the same problem. i appreciate the response tho.
Karthik_Mahalingam 8-Mar-17 22:58pm    
edit or delete?
BebeSaiyan 8-Mar-17 23:03pm    
both
Karthik_Mahalingam 8-Mar-17 23:37pm    
GuitarBrandsGridView.DataSource = ds is missing.
BebeSaiyan 8-Mar-17 23:40pm    
actually im using an sqldatasource in my gridview thats why GuitarBrandsGridView.DataSource = ds won't work. I didnt include it above because its too long.

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