从GridView中删除并更新DropDownList

时间:2015-03-06 09:30:38

标签: c# asp.net

我是初学者,你能不能帮我解决这个问题。     我的情况是这样的:

ASPX:

 <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Testas.aspx.cs" Inherits="IT.Testas" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <p>
        &nbsp;<p>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource2" DataValueField="e_department" AppendDataBoundItems="true" onselectedindexchanged="DropDownList1_SelectedIndexChanged">
             <asp:ListItem Text="All" Value="All"></asp:ListItem>     
        </asp:DropDownList>
        &nbsp;&nbsp;<p>
        <asp:GridView ID="GridView1" runat="server" EditText="Redaguoti" OnRowEditing="GridView1_RowEditing" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting" AutoGenerateColumns="False" DataKeyNames="E_ID">
            <Columns>

                <asp:TemplateField ShowHeader="False">
                    <EditItemTemplate>           
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton> 
                        &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>                        
                    </EditItemTemplate>
                    <ItemTemplate>
                         <span onclick="Ar tikrai norite atnaujinti?">   </span>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>   
                    </ItemTemplate>
                    <ControlStyle BorderStyle="None" ForeColor="Blue" Width="80px" />
                </asp:TemplateField>

                <asp:TemplateField ShowHeader="False">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="E_ID" InsertVisible="False" SortExpression="E_ID">
                    <EditItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("E_ID") %>'></asp:Label>   
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label5" runat="server" Text='<%# Bind("E_ID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="E_NAME" SortExpression="E_NAME">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("E_NAME") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("E_NAME") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="E_AGE" SortExpression="E_AGE">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("E_AGE") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("E_AGE") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="E_CITY" SortExpression="E_CITY">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("E_CITY") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("E_CITY") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="E_DEPARTMENT" SortExpression="E_DEPARTMENT">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("E_DEPARTMENT") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Text='<%# Bind("E_DEPARTMENT") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

    <p>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:XXXConnectionString %>" SelectCommand="SELECT distinct E_DEPARTMENT FROM [EMP_DETAIL]"></asp:SqlDataSource>
        <br />
    </p>
</asp:Content>

aspx.cs:

namespace IT
{
    public partial class Testas : System.Web.UI.Page
    {
     SqlConnection strcon = new SqlConnection(@"Data Source=192.168.100.52;Initial Catalog=XXX;Integrated Security=True");


        protected void Page_Load(object sender, EventArgs e)
        {
           if (!IsPostBack)
            {
               location();
            }    

        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

            SqlConnection con = strcon;
            con.Open();

            if (DropDownList1.SelectedItem.Text == "All")
            {
                string strquery = "select e_id, e_name, e_age, e_city, e_department from emp_detail";
                SqlCommand cmd = new SqlCommand(strquery, con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataBind();
                con.Close();
            }
            else
            {
                string strquery = "select e_id, e_name, e_age, e_city, e_department from emp_detail where e_department='" + DropDownList1.SelectedItem.Text + "'";
                SqlCommand cmd = new SqlCommand(strquery, con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataBind();
                con.Close();

            }
        }


        public void location()
        {

            SqlConnection con = strcon;
            con.Open();

            if (DropDownList1.SelectedItem.Text == "All")
            {
                string strquery = "select e_id, e_name, e_age, e_city, e_department from emp_detail";
                SqlCommand cmd = new SqlCommand(strquery, con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataBind();
                con.Close();
            }
            else
            {
                string strquery = "select e_id, e_name, e_age, e_city, e_department from emp_detail where e_department='" + DropDownList1.SelectedItem.Text + "'";
                SqlCommand cmd = new SqlCommand(strquery, con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataBind();
                con.Close();
            }

        } 

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {

            GridView1.EditIndex = e.NewEditIndex;
            location();
        }

        protected void GridView1_RowCancelingEdit(object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            location();

        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            int id = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
            TextBox tname = (TextBox)row.FindControl("TextBox3");
            SqlCommand cmd = new SqlCommand("update emp_detail set e_city=@e_city from emp_detail where e_id = @e_id", strcon);
            cmd.Parameters.Add("@e_id", SqlDbType.Int).Value = id;
            cmd.Parameters.Add("@e_city", SqlDbType.VarChar, 30).Value = tname.Text.Trim();
            strcon.Open();
            cmd.ExecuteNonQuery();
            strcon.Close();
            GridView1.EditIndex = -1;
            location();
  }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            int id = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
            SqlCommand cmd = new SqlCommand("delete from emp_detail where e_id = @e_id", strcon);
            cmd.Parameters.Add("@e_id", SqlDbType.Int).Value = id;
            strcon.Open();
            cmd.ExecuteNonQuery();
            strcon.Close(); 
            GridView1.EditIndex = -1;
            location();
        }
    }
}

它是通过DropDownList过滤并填充GridView但是对于这种情况可能是更好的性能和编程代码方式? 我的问题: 当我从depatarment类别中删除所有值时,我可以在DropdownList中选择已删除的类别。 DropDownList未更新... 请帮助我,如果你能解释这个案子。

1 个答案:

答案 0 :(得分:0)

它是通过DropDownList进行过滤并填充GridView,但对于这种情况,可能是性能和编程代码的更好方法吗?

尝试为GridView使用SqlDataSource,你现在正在重新发明轮子。

我的问题:当我从depatarment类别中删除所有值时,我可以在DropdownList中选择已删除的类别。 DropDownList未更新...

确保在修改源数据后重新绑定DropdownList。