天天看點

GridView增、删、改、查

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" οnclick="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();

    }

繼續閱讀