GridView手动在后台增加-删除(1)

1.先要验证是否连接数据库!
SqlConnection conn = new SqlConnection("server=.;database=cs;uid=sa;pwd=");
       
       
       //try
       //{
       //    conn.Open();
       //    if (conn.State == ConnectionState.Open)
       //    {
       //        Response.Write("数据库连接成功!");
       //    }
       //}
       //catch (Exception ee)
       //{
       //    Response.Write(ee.Message);
       //}
       //finally {
       //    conn.Close();
       //}


2.增加一个GridView控件!

紧接着一定要编辑模板

<asp:GridView ID="GridView2" runat="server" DataKeyNames="id" AutoGenerateColumns="False" OnRowEditing="GridView2_RowEditing" OnRowUpdating="GridView2_RowUpdating" OnRowDataBound="GridView2_RowDataBound" OnRowCancelingEdit="GridView2_RowCancelingEdit" OnRowDeleting="GridView2_RowDeleting">
        <Columns>
            <asp:TemplateField HeaderText="ID">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%#Bind("id") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="用户名">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%#Bind("txtuser") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="密码">
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%#Bind("txtpassword") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Width="86px" Text='<%#Bind("txtpassword") %>'></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="操作">
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CommandName="edit" CommandArgument='<%#Bind("id")%>'
                        >修改</asp:LinkButton>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:LinkButton ID="LinkButton2" runat="server" CommandName="update">更新</asp:LinkButton>|<asp:LinkButton
                        ID="LinkButton3" runat="server" CommandName="cancel">取消</asp:LinkButton>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="删除">
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton4" runat="server" CommandName="delete">删除</asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>


注意:我标记红字的地方是代表3个 必须不可少的东西,万一少一个,调试出来错误,也找不到!还有text后面是单引号‘’!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值