我有一个数据绑定类别的下拉列表。从中选择填充gridview。当我删除一个类别时,它正在成功更新我的产品网格视图(不显示任何条目),但是不会在他重新运行该程序之前删除类别的下拉列表。数据改变时下拉列表未更新
我的页面加载:
protected void Page_Load(object sender, EventArgs e)
{
// Check if loaded for first time.
if (!IsPostBack)
{
// Bind the data displayed in the Dropdownlists.
Login.SelectAllCat(DropListCat);
}
}
我的代码删除一个类别:
protected void BtnDeleteCat_Click(object sender, EventArgs e)
{
try
{
// Get int id from selectioin in drop down list.
int id = Convert.ToInt32(DropListCat.SelectedValue.ToString());
// Call method to open data base, create command from stored procedure and delete item to database.
Login.DeleteCategory(id);
// Update the data displayed in the Dropdownlists.
Login.SelectAllCat(DropListCat);
}
catch (NullReferenceException)
{
LblProdId.Text = "No Category Selected!";
}
}
我的下拉列表:
<asp:DropDownList ID="DropListCat" runat="server" BackColor="#66FFFF"
Width="200px" AutoPostBack="True" AppendDataBoundItems="True">
</asp:DropDownList>
我的连接和绑定代码。在Login class
。
// Method to select all categories and display them in dropdown lists.
public static void SelectAllCat(DropDownList list)
{
// SqlConnection.
SqlConnection con = new SqlConnection(conString);
// Create new command and parameterise.
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SelectAllCat";
//
// Adapted from
// Source link: http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/data-binding-to-dropdownlist-and-listbox-in-Asp-Net/
//
cmd.Connection = con;
try
{
// Open connection and bind data to GUI.
con.Open();
list.DataSource = cmd.ExecuteReader();
list.DataTextField = "CatName";
list.DataValueField = "CatID";
list.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
我的存储过程:
CREATE PROCEDURE SelectAllProd
AS
SELECT * FROM Prod;
GO
这是结果后,我试图删除类别。
当我重新运行项目时,该类别将被删除。
编辑
实际上,它是删除类别,但它保留了原始数据从页面加载绑定。所以我想我需要弄清楚如何消除这种情况。