如何使用ASP.NET pt.2在gridview中创建自定义删除和编辑按钮? [英] How to create custom delete and edit button in gridview using ASP.NET pt.2?

查看:51
本文介绍了如何使用ASP.NET pt.2在gridview中创建自定义删除和编辑按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最后,我能够为gridview创建自定义删除和编辑按钮。但我注意到我的gridview中存在两个问题。希望你们能帮我解决这个问题。此外,这仅适用于练习。我将很快在我的网站上实现更好的安全性。



问题1:当我第一次点击删除按钮时,它只是刷新页面,什么都不做。但是当我第二次点击它时,它会完美地执行。为什么删除按钮不起作用,只是第一次点击时刷新页面,然后第二次工作?

问题2:当我点击编辑按钮时第一次,它工作正常。然后,我可以更新/更改数据库。假设我想将ID为7的名称Dean更改为Jackson。所以我只是在文本框中更改它并单击更新按钮。但网站只是再次刷新,名称Dean仍然在文本框中(顺便说一句,这仍处于更新/取消模式)。所以我需要在文本框中再次输入Jackson,然后再次单击更新按钮。这次它工作,它确实更新了我的gridview。与问题1类似,为什么更新按钮不起作用,只是第一次点击时刷新页面并在第二次工作?



我是什么尝试过:



这是aspx代码:

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>





这是aspx.cs代码:



Here is the aspx.cs code:

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

推荐答案

添加 IsPostBack 条件 Page_Load 事件



Add IsPostBack Condition in Page_Load event

protected void Page_Load(object sender, EventArgs e)
       {
           if (!Page.IsPostBack)
           {
               BindGridViewDataList.GetItemsLoad();
           }
       }


这篇关于如何使用ASP.NET pt.2在gridview中创建自定义删除和编辑按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆