GridView增、删、改、查

来源:互联网 发布:炉石传说 秋日淘宝店 编辑:程序博客网 时间:2024/05/22 15:52

html代码:

<body>
    <form id="form1" runat="server">
    <center>
       <div>
          <table cellpadding="3" cellspacing="1" style="width:100%; border:solid 1px #BDC5CA;  text-align:center; background-color:#89c3e4; height:30px; font-weight:bold;">
             <tr>
                <td>
                   当前位置:后台管理&nbsp;&nbsp;&nbsp;&nbsp;友情链接&nbsp;&nbsp;&nbsp;&nbsp;<font color="red">友情链接</font>
                </td>
             </tr>
          </table>
         
          <br />
         
          <div>
            
                      <asp:GridView ID="gvFriendLink" runat="server" AutoGenerateColumns="False"
                          AllowPaging="true" AllowSorting="true" CellPadding="3"
                      Width="700px" BorderColor="#89C3E4" BorderWidth="1px"
                          onrowediting="gvFriendLink_RowEditing"
                          onrowdeleting="gvFriendLink_RowDeleting"
                          onrowcancelingedit="gvFriendLink_RowCancelingEdit"
                          onrowupdating="gvFriendLink_RowUpdating"
                          onpageindexchanging="gvFriendLink_PageIndexChanging">
                      <Columns>
                          <asp:BoundField DataField="LinksID" HeaderText="链接ID" SortExpression="LinksID" ReadOnly="true" />
                          <asp:BoundField DataField="LinksName" HeaderText="链接名称" SortExpression="LinksName" />
                          <asp:BoundField DataField="Link" HeaderText="链接地址" SortExpression="Link" />                        
                          <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                      </Columns>
                      <RowStyle BackColor="White"  ForeColor="#003399" Height="24px" />
                      <HeaderStyle BackColor="#E2F3FA" Font-Bold="True" ForeColor="Black" Height="32px"  />
                      <EmptyDataTemplate>
                      温馨提示:当前没有任何记录!
                      </EmptyDataTemplate>
                      </asp:GridView>
                  
          </div>
         
          <br />
         
          <div>
             <table cellpadding="0" cellspacing="0" style="width:700px; border:1px #89c3e4 solid;">
                <tr>
                   <td style="width:100px; border:1px #89c3e4 solid; height:28px;">添加链接:</td>
                   <td style="border:1px #89c3e4 solid; text-align:left;">
                          名称<asp:TextBox ID="TextBox1" runat="server" Width="137px"></asp:TextBox>
                          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 地址:<asp:TextBox ID="TextBox2" runat="server">http://</asp:TextBox>
                          <asp:Button ID="Button1" runat="server" Text="添加"
                              Width="74px" onclick="Button1_Click" style="height: 26px" />
                   </td>
                </tr>
               
                <tr>
                   <td style="width:100px;  border:1px #89c3e4 solid; height:28px;">&nbsp;</td>
                   <td style="border:1px #89c3e4 solid;">
                       &nbsp;</td>
                </tr>
             </table>
          </div>
         
          <br />
         
       </div>
    </center>
    </form>
</body>

 

服务器代码:

    string conn1 = ConfigurationManager.ConnectionStrings["conn"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["UserName"].ToString() == null)
            {
                DBUtility.JsHerpel.AlertAndFrame("您还尚未登录,请先登录,如果您有什么问题,请与管理员联系!", "Login.aspx", this.Page);
            }
        }
        catch
        {
            DBUtility.JsHerpel.AlertAndFrame("您还尚未登录,请先登录,如果您有什么问题,请与管理员联系!", "Login.aspx", this.Page);
        }
        if (!IsPostBack)
        {
            Bind();
        }
    }
    BLL.BShou shou = new BLL.BShou();
    Model.MShou sh = new Model.MShou();
    public void Bind()
    {
        this.gvFriendLink.DataSource = shou.SelectShou();
        this.gvFriendLink.DataBind();
    }
    protected void gvFriendLink_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvFriendLink.EditIndex = e.NewEditIndex;
        Bind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        sh.LinkName = this.TextBox1.Text.ToString();
        sh.Link = this.TextBox2.Text.ToString();
        shou.InsertBind(sh);
        Response.Write("<script>alert('添加链接成功!');location.href='AdminFriendship.aspx';</script>");
    }
    protected void gvFriendLink_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(this.gvFriendLink.Rows[e.RowIndex].Cells[0].Text.ToString());
        int cmdText = shou.DeleteBind(id);
        if (shou.DeleteBind(cmdText) > 0)
        {
            Response.Write("<script>alert('删除成功!');location.href='AdminFriendship.aspx';</script>");
        }
        Bind();
    }
    protected void gvFriendLink_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string str = ((TextBox)(gvFriendLink.Rows[e.RowIndex].Cells[1].Controls[0])).Text.Trim().ToString();
        string str1 = ((TextBox)(gvFriendLink.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim().ToString();
        int str2 = int.Parse(gvFriendLink.Rows[e.RowIndex].Cells[0].Text.ToString());
        string str3 = "update Links set LinksName='" + str + "',Link='" + str1 + "' where LinksID=" + str2;
        SqlConnection conn = new SqlConnection(conn1);
        SqlCommand cmd = new SqlCommand(str3, conn);
        conn.Open();
        int var = cmd.ExecuteNonQuery();
        if (var > 0)
        {
            ClientScript.RegisterStartupScript(GetType(), "1", "<script>alert('更新成功!')</script>");
        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(), "1", "<script>alert('更新失败!')</script>");
        }
        gvFriendLink.EditIndex = -1;
        Bind();
    }
    protected void gvFriendLink_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvFriendLink.EditIndex = -1;
        Bind();
    }
    protected void gvFriendLink_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvFriendLink.PageIndex = e.NewPageIndex;
        Bind();
    }

原创粉丝点击