C# 从GridView中删除并更新DropDownList
我是初学者,你能帮我做这个案子吗。 我的情况是: aspx: 它是通过DropDownList和fill GridView进行过滤的,但对于这种情况,这可能是一种更好的性能和编程代码的方法? 我的问题是: 当我从depatarment类别中删除所有值时,我可以在DropdownList中选择已删除类别。DropDownList未更新。。。 请帮助我,如果你能解释这个案例。它是通过DropDownList和fill GridView进行过滤的,但是对于这个案例来说,这可能是一种更好的性能和编程代码的方法 尝试为GridView使用SqlDataSource,您现在正在重新发明轮子 我的问题是:当我从depatarment类别中删除所有值时,我可以在DropdownList中选择已删除的类别。DropDownList未更新C# 从GridView中删除并更新DropDownList,c#,asp.net,C#,Asp.net,我是初学者,你能帮我做这个案子吗。 我的情况是: aspx: 它是通过DropDownList和fill GridView进行过滤的,但对于这种情况,这可能是一种更好的性能和编程代码的方法? 我的问题是: 当我从depatarment类别中删除所有值时,我可以在DropdownList中选择已删除类别。DropDownList未更新。。。 请帮助我,如果你能解释这个案例。它是通过DropDownList和fill GridView进行过滤的,但是对于这个案例来说,这可能是一种更好的性能和编程代码
请确保在修改源数据后重新绑定DropdownList。删除后如何重新绑定DropdownList?我假设您正在删除服务器端的数据。在本例中,只需在数据库逻辑之后调用DropDownList1.DataBind()。结果是相同的。您是在GridView1\u rowdeleding方法中删除数据,而不是在location()中删除数据。试试那里,我找不到结果。你能帮我查一下密码吗?
<%@ 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>
<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>
<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>
<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>
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();
}
}
}