我有一个数据绑定类别的下拉列表。从中选择填充网格视图。当我删除一个类别时,它成功更新了我的产品gridview(没有显示任何条目),但在重新运行程序之前,他没有下载类别列表。
我的页面加载:
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
我的存储过程:
showProgress()
这是我尝试删除某个类别后的结果 当我重新运行项目时,该类别将被删除
修改
实际上,它正在删除该类别,但它保留了页面加载的原始数据绑定。所以我想我需要弄清楚如何擦除它。
答案 0 :(得分:1)
写
DropListCat.DataBind();
在BtnDeleteCat_Click
中答案 1 :(得分:0)
我通过在重新绑定数据之前清除下拉列表项来修复它,如下所示:
// Method to select all categories and display them in dropdown lists.
public static void SelectAllCat(DropDownList list)
{
// Clear any previously bound items.
list.Items.Clear();
// etc.../