Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用asp.net的gridview自定义按钮不起作用?_C#_Asp.net_Sql Server_Gridview_Visual Studio 2015 - Fatal编程技术网

C# 使用asp.net的gridview自定义按钮不起作用?

C# 使用asp.net的gridview自定义按钮不起作用?,c#,asp.net,sql-server,gridview,visual-studio-2015,C#,Asp.net,Sql Server,Gridview,Visual Studio 2015,我已经尝试在gridview中使用创建自己的自定义按钮 asp.net。我想复制通常的删除按钮 我们在gridview中使用,然后添加新功能,如 能够删除存储在my visual studio 2015中的文件(即 位于文档中)。所以当管理员点击删除按钮时, 它将删除该行中的所有数据并删除相应的 与之关联的文件 假设我想删除id#10下的所有信息。我已经 在默认的编辑和删除链接旁边添加了一个新按钮 现在的问题是,当我点击我创建的删除按钮时, 它给了我这个例外 这也是我的代码(aspx文件):

我已经尝试在gridview中使用创建自己的自定义按钮 asp.net。我想复制通常的删除按钮 我们在gridview中使用,然后添加新功能,如 能够删除存储在my visual studio 2015中的文件(即 位于文档中)。所以当管理员点击删除按钮时, 它将删除该行中的所有数据并删除相应的 与之关联的文件

假设我想删除id#10下的所有信息。我已经 在默认的编辑和删除链接旁边添加了一个新按钮

现在的问题是,当我点击我创建的删除按钮时, 它给了我这个例外

这也是我的代码(aspx文件):


不确定代码,但最好的方法是将每行放在网格列中。单击它并获取行号。找到特定行的数据并执行操作。如果您能给我一个实际示例,我将不胜感激。我还是一个制作网站的新手。此链接将为您提供所需的内容
<asp:GridView ID="GridView20" runat="server" OnRowCommand="FireRowCommand" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" CellSpacing="3" DataKeyNames="id" ForeColor="Black" GridLines="Vertical" Height="250px" Width="864px">
        <AlternatingRowStyle BackColor="#CCCCCC" />
        <Columns>
             <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="Button1" runat="server" CommandArgument='<%# Eval("id") %>' CommandName="cDelete" Text="Delete"></asp:Button>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" />
            <asp:BoundField DataField="type" HeaderText="type" SortExpression="type" />
            <asp:BoundField DataField="model" HeaderText="model" SortExpression="model" />
            <asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
            <asp:BoundField DataField="image1" HeaderText="image1" SortExpression="image1" />
            <asp:BoundField DataField="image2" HeaderText="image2" SortExpression="image2" />
        </Columns>
        <FooterStyle BackColor="#CCCCCC" />
        <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#808080" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#383838" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:GuitarItemsDBConnectionString2 %>" DeleteCommand="DELETE FROM [guitarItemIbanez] WHERE [id] = @id" InsertCommand="INSERT INTO [guitarItemIbanez] ([id], [type], [model], [price], [image1], [image2]) VALUES (@id, @type, @model, @price, @image1, @image2)" SelectCommand="SELECT [id], [type], [model], [price], [image1], [image2] FROM [guitarItemIbanez]" UpdateCommand="UPDATE [guitarItemIbanez] SET [type] = @type, [model] = @model, [price] = @price, [image1] = @image1, [image2] = @image2 WHERE [id] = @id">
        <DeleteParameters>
            <asp:Parameter Name="id" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="id" Type="Int32" />
            <asp:Parameter Name="type" Type="String" />
            <asp:Parameter Name="model" Type="String" />
            <asp:Parameter Name="price" Type="Double" />
            <asp:Parameter Name="image1" Type="String" />
            <asp:Parameter Name="image2" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="type" Type="String" />
            <asp:Parameter Name="model" Type="String" />
            <asp:Parameter Name="price" Type="Double" />
            <asp:Parameter Name="image1" Type="String" />
            <asp:Parameter Name="image2" Type="String" />
            <asp:Parameter Name="id" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
 using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Configuration;
 using System.Data;
 using System.Data.SqlClient;
 using System.Linq;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;

 public partial class Pages_OverviewData : System.Web.UI.Page
{
string _connStr = ConfigurationManager.ConnectionStrings["GuitarItemsDBConnectionString2"].ConnectionString;
private SqlConnection conn;
private SqlCommand cmd;
private SqlDataAdapter ad;
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        GetData();
    }

}

private void GetData()
{
    DataTable table = new DataTable();
    // get the connection
    using (conn = new SqlConnection(_connStr))
    {

        string sql = "SELECT * FROM guitarItemIbanez ORDER By id";

        using (cmd = new SqlCommand(sql, conn))
        {

            using (ad = new SqlDataAdapter(cmd))
            {

                ad.Fill(table);
            }
        }
    }

    GridView20.DataSource = table;
    GridView20.DataBind();
}


protected void FireRowCommand(object sender, GridViewCommandEventArgs e)
{
    string command3 = e.CommandName;
    string autoId = e.CommandArgument.ToString();

    switch (command3)
    {
        case "cDelete":
            string query = string.Format(@"DELETE FROM [guitarItemIbanez] WHERE [id] = @id");
            cmd.CommandText = query;

            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
            }
            finally
            {
                conn.Close();
            }

            System.IO.File.Delete(@"C: \Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\GuitarItemsIbanez10.aspx");
            System.IO.File.Delete(@"C: \Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\GuitarItemsIbanez10.aspx.cs");
            break;
    }



    }

}