Gridview删除选中,编辑

首页前台

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
             CellPadding="4"
                 onselectedindexchanged="GridView1_SelectedIndexChanged" PageSize="5"
                 ShowFooter="True" Height="120px">
             <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
             <Columns>
                 <asp:ImageField DataImageUrlField="Images" HeaderText="食物">
                 </asp:ImageField>
                 <asp:BoundField DataField="FoodName" HeaderText="食物名字" />
                 <asp:BoundField DataField="Name" HeaderText="名字" />
                 <asp:BoundField DataField="Sex" HeaderText="性别" />
                 <asp:BoundField DataField="Phone" HeaderText="手机号" />
                 <asp:BoundField DataField="AddressCity" HeaderText="城市" />
                 <asp:BoundField DataField="Addressxiaoqu" HeaderText="小区" />
                 <asp:BoundField DataField="Addressdanyuan" HeaderText="单元" />
                 <asp:BoundField DataField="CreateTime" HeaderText="订单时间" />
                 <asp:BoundField DataField="Zhuangtai" HeaderText="发货状态" />
                 <asp:TemplateField HeaderText="编辑">
                     <ItemTemplate>
                         <asp:LinkButton ID="LinkButton2" runat="server" οnclick="LinkButton2_Click" CommandArgument='<%#Eval("Id") %>'>编辑</asp:LinkButton>
                     </ItemTemplate>
                 </asp:TemplateField>
                 <asp:TemplateField HeaderText="删除">
                     <ItemTemplate>
                         <asp:LinkButton ID="LinkButton3" runat="server" οnclick="LinkButton3_Click" CommandArgument='<%#Eval("Id") %>' OnClientClick="return showinfo()">删除</asp:LinkButton>
                     </ItemTemplate>
                 </asp:TemplateField>
             </Columns>
             <EditRowStyle BackColor="#999999" />
             <EmptyDataTemplate>
                 <asp:LinkButton ID="LinkButton1" runat="server">编辑</asp:LinkButton>
             </EmptyDataTemplate>
             <FooterStyle Font-Bold="True" ForeColor="White" />
             <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
             <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
             <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
             <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
             <SortedAscendingCellStyle BackColor="#E9E7E2" />
             <SortedAscendingHeaderStyle BackColor="#506C8C" />
             <SortedDescendingCellStyle BackColor="#FFFDF8" />
             <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
         </asp:GridView>

 

后台

删除按钮

private void LoadData()
        {
            string strcon = @"Data Source=PC-WL;Initial Catalog=KFC;Persist Security Info=True;User ID=sa;Password=wanglei";
            SqlConnection conn = new SqlConnection(strcon);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            //每次都显式打开
            conn.Open();

            cmd.CommandText = "select FoodName,Name,Sex,Phone,Zhuangtai,AddressCity,Addressxiaoqu,Addressdanyuan,CreateTime from T_Orders";
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            cmd.Dispose();
            conn.Dispose();
            //将查询出来的数据绑定到GridView
            //原来需要手动拼接字符串的工作由GridView代劳了
            this.GridView1.DataSource = dt;
            //DataBind负责拼接字符串
            this.GridView1.DataBind();
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            LinkButton LinkButton3 = sender as LinkButton;
            string strcon = @"Data Source=PC-WL;Initial Catalog=KFC;Persist Security Info=True;User ID=sa;Password=wanglei";
            SqlConnection conn = new SqlConnection(strcon);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            conn.Open();

            cmd.CommandText = "DELETE FROM T_Orders WHERE Id=@id";
            cmd.Parameters.AddWithValue("@id", LinkButton3.CommandArgument);
            if (cmd.ExecuteNonQuery() > 0)
            {
                //删除之后重新加载页面,让用户看到最新的变化
                dataload();
            }
            cmd.Dispose();
            conn.Dispose();
        }

 

更改按钮

protected void LinkButton2_Click(object sender, EventArgs e)
        {
            LinkButton LinkButton2 = sender as LinkButton;
            Response.Redirect("Edit.aspx?pageindex=" + Convert.ToInt32(ViewState["pageindex"]) + "&id=" + LinkButton2.CommandArgument);
        }

 

更改前台

 

<table>
            <tr>
                <td>
                    食物名字:
                </td>
                <td>
                    <asp:TextBox ID="txtFoodName" runat="server" Enabled="False" ReadOnly="True"
                        Width="254px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                   名字:
                </td>
                <td>
                    <asp:TextBox ID="txtName" runat="server" Width="250px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                   性别:
                </td>
                <td>
                   <asp:RadioButtonList ID="rblsex" runat="server" AutoPostBack="True"  RepeatDirection="Horizontal">
            <asp:ListItem Value="1">男/Male</asp:ListItem>
            <asp:ListItem Value="2">女/Female</asp:ListItem>
        </asp:RadioButtonList>
                </td>
            </tr>
            <tr>
                <td>
                   手机号:
                </td>
                <td>
                    <asp:TextBox ID="txtPhone" runat="server" Width="250px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                  发货状态:
                </td>
                <td>
                    <asp:TextBox ID="txtNumber" runat="server" Width="250px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                   城市:
                </td>
                <td>
                    <asp:TextBox ID="txtCountry" runat="server" Width="250px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                   小区:
                </td>
                <td>
                    <asp:TextBox ID="txtqu" runat="server" Width="250px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                   单元:
                </td>
                <td>
                    <asp:TextBox ID="txtDanyuan" runat="server" Width="250px"></asp:TextBox>
                </td>
            </tr>
            <tr>
              
                <td align="center" colspan="2">
                    <asp:Button ID="btnUpdate" runat="server" Text="保存" οnclick="btnUpdate_Click"
                        Height="24px" Width="86px" />
                </td>
            </tr>
        </table>

后台

 

 protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                string  id =Request["id"].ToString();
                GetNews(id);

            }
        }
        private void GetNews(string id)
        {
            string strcon = @"Data Source=PC-WL;Initial Catalog=KFC;Persist Security Info=True;User ID=sa;Password=wanglei";
            SqlConnection conn = new SqlConnection(strcon);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            conn.Open();
            cmd.CommandText = "SELECT * FROM T_Orders WHERE Id=@id";
            cmd.Parameters.AddWithValue("@id", id);


            Response.Write(cmd.ExecuteScalar().ToString());


            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
           
            foreach (DataRow row in dt.Rows)
            {
                txtFoodName.Text = row["FoodName"].ToString();
                txtName.Text = row["Name"].ToString();
                string sex = row["Sex"].ToString();
                if (sex == "男")
                    rblsex.SelectedValue = "1";
                else
                    rblsex.SelectedValue = "2";
                txtPhone.Text = row["Phone"].ToString();
                txtNumber.Text = row["Zhuangtai"].ToString();
                txtCountry.Text = row["AddressCity"].ToString();
                txtqu.Text = row["Addressxiaoqu"].ToString();
                txtDanyuan.Text = row["Addressdanyuan"].ToString();
            }
            cmd.Dispose();
            conn.Dispose();
           
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string id = Request["id"].ToString();
            #region 获取用户输入和选择的值
            string FoodName = txtFoodName.Text;
            string Name = txtName.Text;

            string sex = rblsex.SelectedItem.Text.Substring(0, 1);
            string Phone = txtPhone.Text;
            string Zhuangtai = txtNumber.Text;
            string Country = txtCountry.Text;
            string qu = txtqu.Text;
            string Danyuan = txtDanyuan.Text;
          
            #endregion

            #region 更改数据
            string strcon = @"Data Source=PC-WL;Initial Catalog=KFC;Persist Security Info=True;User ID=sa;Password=wanglei";
            SqlConnection conn = new SqlConnection(strcon);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;           
            conn.Open();

            cmd.CommandText = "UPDATE T_Orders SET FoodName=@foodname,Name=@name,Phone=@phone,Zhuangtai=@zhuangtai,AddressCity=@addresscity,Addressxiaoqu=@addressxiaoqu,Addressdanyuan=@addressdanyuan WHERE Id=@id";
            cmd.Parameters.AddWithValue("@foodname",FoodName);
            cmd.Parameters.AddWithValue("@name", Name);
            cmd.Parameters.AddWithValue("@phone", Phone);
            cmd.Parameters.AddWithValue("@zhuangtai", Zhuangtai);
            cmd.Parameters.AddWithValue("@addresscity", Country);
            cmd.Parameters.AddWithValue("@addressxiaoqu", qu);
            cmd.Parameters.AddWithValue("@addressdanyuan", Danyuan);
            cmd.Parameters.AddWithValue("@id", id);

            if (cmd.ExecuteNonQuery() > 0)
            {
                Response.Redirect("newsguanli.aspx?id=" + id);
            }
            #endregion
            conn.Dispose();
            cmd.Dispose();
        }

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值