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

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

问题描述

我今天正在学习所有关于gridviews的知识。总结一下我的gridview的内容,这里是:



Id类型名称图片

删除编辑1吉他Ibanez pic1.jpg

删除编辑2吉他挡泥板pic2.jpg



[LabelId]

[LabelName]



我想要做的是,每当我点击按钮删除或编辑和更新按钮时,它应该检索列名中的值。例如,如果我单击第二行的编辑按钮,它应该检索名称Fender并将其与[LabelId]一起显示在[LabelName]上。此外,如果我点击第一行的删除按钮,它应该检索名称Ibanez并在下面的标签上显示它。同样适用于更新按钮。每当单击编辑,删除和更新按钮时,它应始终显示名称和ID。



我尝试为此创建代码,但它只检索id而不是这个名字,它总是空白的。



得到这个代码的答案将是完成我的项目的更大部分。希望你能帮我解决这个问题。



我尝试过:



这是aspx代码:



I'm learning all about gridviews today. To summarize the contents of my gridview, here it is:

Id Type Name Image
Delete Edit 1 Guitar Ibanez pic1.jpg
Delete Edit 2 Guitar Fender pic2.jpg

[LabelId]
[LabelName]

What i'm trying to do is, whenever i click the button delete or edit and update button, it should retrieve the value in column name. For example if i clicked the edit button for 2nd row, it should retrieve the name "Fender" and display it on [LabelName] along with its [LabelId]. Also if i clicked the delete button for 1st row, it should retrieve the name "Ibanez" and display it aswell on the labels below. Same applies also for the update button. It should always display the name and id whenever the edit, delete and update button is clicked.

I tried creating a code for this but it only retrieves the id and not the name, it's always blank.

Having the answer for this code will be a greater part on finishing my project. Hopefully you can help me out on this one.

What I have tried:

Here is the aspx code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 <!DOCTYPE html>

 <html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">
<title></title>

</head>
 <body>
  <form id="form1" runat="server">
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="Button3" runat="server" Text="Delete" OnClick="Button3_Click"/>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField ShowHeader="False">
                <EditItemTemplate>
                    <asp:Button ID="ButtonUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="Update" OnClick="ButtonUpdate_Click"/>
                     <asp:Button ID="Button2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Button ID="ButtonEdit" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" OnClick="ButtonEdit_Click"/>
                </ItemTemplate>
            </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="Label1" 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="Label2" 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="Label3" 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="Label4" runat="server" Text='<%# Bind("image") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

        </Columns>
    </asp:GridView>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:BrandsDBConnectionString %>" DeleteCommand="DELETE FROM [guitarBrands] WHERE [id] = @id" InsertCommand="INSERT INTO [guitarBrands] ([id], [type], [name], [image]) VALUES (@id, @type, @name, @image)" SelectCommand="SELECT [id], [type], [name], [image] FROM [guitarBrands]" UpdateCommand="UPDATE [guitarBrands] SET [type] = @type, [name] = @name, [image] = @image 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="name" Type="String" />
            <asp:Parameter Name="image" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="type" Type="String" />
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="image" Type="String" />
            <asp:Parameter Name="id" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>

    <br/>
    <asp:Label ID="lblId" runat="server" Text="Label"></asp:Label><br/>
    <asp:Label ID="lblName" runat="server" Text="Label"></asp:Label><br/>
</form>





这里是aspx.cs代码:





And here is the aspx.cs code:

using System;
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 _Default : System.Web.UI.Page
{

SqlConnection con1;
SqlCommand cmd1;
DataSet ds1;
public _Default()
{
    con1 = new SqlConnection();
    con1.ConnectionString = ConfigurationManager.ConnectionStrings["BrandsDBConnectionString"].ToString();
    cmd1 = new SqlCommand();
    ds1 = new DataSet();
}
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        bindgridviewguitarbrands();
    }
}

//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();
    GridView1.DataBind();

}

protected void Button3_Click(object sender, EventArgs e)
{

        Button btn1 = sender as Button;
        GridViewRow gridrow = btn1.NamingContainer as GridViewRow;
        int id = Convert.ToInt32(GridView1.DataKeys[gridrow.RowIndex].Value.ToString());
        string name = GridView1.Rows[gridrow.RowIndex].Cells[4].Text;

        lblId.Text = id.ToString();
        lblName.Text = name;

}

 protected void ButtonEdit_Click(object sender, EventArgs e)
{

    Button btn2 = sender as Button;
    GridViewRow gridrow = btn2.NamingContainer as GridViewRow;
    int id = Convert.ToInt32(GridView1.DataKeys[gridrow.RowIndex].Value.ToString());
    string name = GridView1.Rows[gridrow.RowIndex].Cells[4].Text;
    lblId.Text = id.ToString();
    lblName.Text = name;

}

 protected void ButtonUpdate_Click(object sender, EventArgs e)
{
    Button btn3 = sender as Button;
    GridViewRow gridrow = btn3.NamingContainer as GridViewRow;
    int id = Convert.ToInt32(GridView1.DataKeys[gridrow.RowIndex].Value.ToString());
    string name = GridView1.Rows[gridrow.RowIndex].Cells[4].Text;

    lblId.Text = id.ToString();
    lblName.Text = name;

}

}

推荐答案

ConnectionStrings:BrandsDBConnectionString%>DeleteCommand =DELETE FROM [guitarBrands] WHERE [id] = @idInsertCommand =INSERT INTO [guitarBrands]([id],[type],[name],[image])VALUES(@ id,@ type,@ name ,@ image)SelectCommand =SELECT [id],[type],[name],[image] FROM [guitarBrands]UpdateCommand =UPDATE [guitarBrands] SET [type] = @type,[name] = @name ,[image] = @image WHERE [id] = @id>
< DeleteParameters>
< asp:参数名称=idType =Int32/>
< / DeleteParameters>
< InsertParameters>
< asp:参数名称=idType =Int32/>
< asp:参数名称=类型 Type =String/>
< asp:Parameter Name =nameType =String/>
< asp:Parameter Name =imageType =String/ >
< / InsertParameters>
< UpdatePara米>
< asp:参数名称=类型Type =String/>
< asp:参数名称=名称类型=字符串/>
< asp:参数名称=图像类型=字符串/>
< asp:参数名称=idType =Int32/>
< / UpdateParameters>
< / asp:SqlDataSource>

< br />
< asp:Label ID =lblIdrunat =serverText =Label>< / asp:Label>< br />
< asp:Label ID =lblNamerunat =serverText =Label>< / asp:Label>< br />
< / form>
ConnectionStrings:BrandsDBConnectionString %>" DeleteCommand="DELETE FROM [guitarBrands] WHERE [id] = @id" InsertCommand="INSERT INTO [guitarBrands] ([id], [type], [name], [image]) VALUES (@id, @type, @name, @image)" SelectCommand="SELECT [id], [type], [name], [image] FROM [guitarBrands]" UpdateCommand="UPDATE [guitarBrands] SET [type] = @type, [name] = @name, [image] = @image 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="name" Type="String" /> <asp:Parameter Name="image" Type="String" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="type" Type="String" /> <asp:Parameter Name="name" Type="String" /> <asp:Parameter Name="image" Type="String" /> <asp:Parameter Name="id" Type="Int32" /> </UpdateParameters> </asp:SqlDataSource> <br/> <asp:Label ID="lblId" runat="server" Text="Label"></asp:Label><br/> <asp:Label ID="lblName" runat="server" Text="Label"></asp:Label><br/> </form>





这里是aspx.cs代码:





And here is the aspx.cs code:

using System;
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 _Default : System.Web.UI.Page
{

SqlConnection con1;
SqlCommand cmd1;
DataSet ds1;
public _Default()
{
    con1 = new SqlConnection();
    con1.ConnectionString = ConfigurationManager.ConnectionStrings["BrandsDBConnectionString"].ToString();
    cmd1 = new SqlCommand();
    ds1 = new DataSet();
}
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        bindgridviewguitarbrands();
    }
}

//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();
    GridView1.DataBind();

}

protected void Button3_Click(object sender, EventArgs e)
{

        Button btn1 = sender as Button;
        GridViewRow gridrow = btn1.NamingContainer as GridViewRow;
        int id = Convert.ToInt32(GridView1.DataKeys[gridrow.RowIndex].Value.ToString());
        string name = GridView1.Rows[gridrow.RowIndex].Cells[4].Text;

        lblId.Text = id.ToString();
        lblName.Text = name;

}

 protected void ButtonEdit_Click(object sender, EventArgs e)
{

    Button btn2 = sender as Button;
    GridViewRow gridrow = btn2.NamingContainer as GridViewRow;
    int id = Convert.ToInt32(GridView1.DataKeys[gridrow.RowIndex].Value.ToString());
    string name = GridView1.Rows[gridrow.RowIndex].Cells[4].Text;
    lblId.Text = id.ToString();
    lblName.Text = name;

}

 protected void ButtonUpdate_Click(object sender, EventArgs e)
{
    Button btn3 = sender as Button;
    GridViewRow gridrow = btn3.NamingContainer as GridViewRow;
    int id = Convert.ToInt32(GridView1.DataKeys[gridrow.RowIndex].Value.ToString());
    string name = GridView1.Rows[gridrow.RowIndex].Cells[4].Text;

    lblId.Text = id.ToString();
    lblName.Text = name;

}

}


您已在9小时前发布了同一个问题:使用ASP.NET在gridview中创建自定义编辑按钮? [ ^ ]



请不要多次发布同一个问题。
You have posted this same question 9 hours ago: Creating custom edit button in gridview using ASP.NET?[^]

Please refrain from posting the same question multiple times.


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

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