Gridview的替代方法编辑,删除? [英] Alternate Method For Gridview Edit, Delete?

查看:84
本文介绍了Gridview的替代方法编辑,删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在aspx中,我有:

In aspx, i have:

<asp:GridView ID="gvDetails" AutoGenerateColumns="False" AllowPaging="True" PageSize="4"

    DataKeyNames="ID" runat="server" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None"

    BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal" OnPageIndexChanging="gvDetails_PageIndexChanging"

    OnRowCancelingEdit="gvDetails_RowCancelingEdit" OnRowEditing="gvDetails_RowEditing"

    OnRowUpdating="gvDetails_RowUpdating" OnRowDataBound="gvDetails_RowDataBound"

    OnRowDeleting="gvDetails_RowDeleting">
   <Columns>
    <asp:CommandField ButtonType="Link" HeaderText="Action" ShowEditButton="true" ShowDeleteButton="true"

        ShowCancelButton="true" />
        <asp:TemplateField HeaderText="Name">
    <EditItemTemplate>
        <asp:TextBox ID="TextBoxEditName" runat="server" Text='<%# Bind("Name") %>' />
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="LabelName" runat="server" Text='<%# Bind("Name") %>' />
    </ItemTemplate>
    <FooterTemplate>
        <asp:TextBox ID="TextBoxName" runat="server" Text='<%# Bind("Name") %>' />
    </FooterTemplate>
</asp:TemplateField> 
   </Columns>
                        <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
                        <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
                        <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
                        <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
                    </asp:GridView>



在CS中:



In CS:

{
    SqlCommand cmd, upd;
    SqlConnection con;
    SqlDataAdapter da;
    DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
        con.Open();
        if (Request.Cookies["Username"] == null || Request.Cookies["Password"] == null)
        {
            Response.Redirect("../../Registration/Login.aspx");
        }
        else
        {
            lblUser.Text = Request.Cookies["Username"].Value.ToString();
            lblUser.ForeColor = System.Drawing.Color.White;
            Session["Username"] = Request.Cookies["Username"].Value.ToString();
        }
        if (!IsPostBack)
        {
            bindresults();
            gvDetails.DataSource = dt;
            gvDetails.DataBind();
        }

    }
    public DataTable bindresults()
    {
        cmd = new SqlCommand("select * from Registration", con);
        da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        gvDetails.DataSource = dt;
        gvDetails.DataBind();
        return dt;
    }
    protected void gvDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvDetails.PageIndex = e.NewPageIndex;
        bindresults();
        gvDetails.DataSource = dt;
        gvDetails.DataBind();
    }
    protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvDetails.EditIndex = e.NewEditIndex;
        bindresults();
        gvDetails.DataSource = dt;
        gvDetails.DataBind();
    }
    protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvDetails.EditIndex = -1;
        bindresults();
        gvDetails.DataSource = dt;
        gvDetails.DataBind();
    }
    protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int ID = int.Parse(gvDetails.DataKeys[e.RowIndex].Value.ToString());
        //string Username = ((TextBox)gvDetails.Rows[e.RowIndex].Cells[0].FindControl("TextBoxEditUsername")).Text;
        string Name = ((TextBox)gvDetails.Rows[e.RowIndex].Cells[1].FindControl("TextBoxEditName")).Text;
        string Gender = ((TextBox)gvDetails.Rows[e.RowIndex].Cells[2].FindControl("TextBoxEditGender")).Text;
        string Occupation = ((TextBox)gvDetails.Rows[e.RowIndex].Cells[3].FindControl("TextBoxEditOccupation")).Text;
        string Address = ((TextBox)gvDetails.Rows[e.RowIndex].Cells[4].FindControl("TextBoxEditAddress")).Text;
        upd = new SqlCommand("update Registration set Name = '" + Name + "', Gender = '" + Gender + "', Occupation = '" + Occupation + "', Address = '" + Address + "' where ID = '" + ID + "'", con);
        upd.ExecuteNonQuery();
        gvDetails.EditIndex = -1;
        bindresults();
        gvDetails.DataSource = dt;
        gvDetails.DataBind();
    }
    protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int ID = int.Parse(gvDetails.DataKeys[e.RowIndex].Value.ToString());
        cmd = new SqlCommand("delete from Registration where ID = " + ID + "", con);
        cmd.ExecuteNonQuery();
        bindresults();
        gvDetails.DataSource = dt;
        gvDetails.DataBind();
    }
    protected void gvDetails_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (!DataBinder.Eval(e.Row.DataItem, "Username").Equals(Session["Username"].ToString()))
            {
                LinkButton btn = (LinkButton)e.Row.Cells[0].Controls[0];
                LinkButton btn1 = (LinkButton)e.Row.Cells[0].Controls[2];

                if (btn != null && btn1 != null)
                {
                    btn.Visible = false;
                    btn1.Visible = false;
                }
            }
        }
    }
}


它工作正常,但时间却很长.我想知道是否有人可以提供更少的代码.我知道我听起来可能很愚蠢,但是对于许多人来说,这可能值得一问.


It is working fine, but its really lengthy. I was wondering if someone could provide a less code. I know i may be sounding foolish but for many, this may be worth asking.

推荐答案

好吧,每个操作本身都有一个方法.

对于更新或删除,您只有很少的几行可以获取数据并进行更新.没有太多的行.没有其他方法可以减少它.您似乎冗长,因为此处定义了很多操作.
Well, every operation has one method for itself.

For update or delete, you got few lines that gets the data and update it. There are not too many lines. There is no other way to reduce it. It looks lengthy you you as there are lots of operations defined here.


这篇关于Gridview的替代方法编辑,删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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