【发布时间】:2014-04-15 12:01:11
【问题描述】:
我试图从列表框和数据表中删除一个项目,但我正在努力做到这一点。我目前可以将其从列表框中删除,但我不知道如何将其从表中删除。
这是我的代码
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//if this is the first time th page is displayed
if (IsPostBack == false)
{
//update the list box
DisplayToDoList();
}
}
//this function populates the list box with to do list items
//it returns the number of records in the list
Int32 DisplayToDoList()
{
//var to store the record count
Int32 RecordCount;
//create an instance of the todo class
clsToDoList MyList = new clsToDoList();
//get the count of records
RecordCount = MyList.Count;
//var to store the index
Int32 Index = 0;
//var to store the primary key
Int32 ToDoNo;
//var to store the to do text
String ToDo;
//clear the list
lstToDo.Items.Clear();
//loop through each record
while (Index < RecordCount)
{
//get the primary key
ToDoNo = MyList.List[Index].ToDoNo;
//get the text
ToDo = MyList.List[Index].ToDo;
//create a new entry for the list
ListItem NewEntry = new ListItem(ToDo, Convert.ToString(ToDoNo));
//add the entry to the list box
lstToDo.Items.Add(NewEntry);
//increment the indez
Index++;
}
//return the number of records
return RecordCount;
}
protected void btnAdd_Click(object sender, EventArgs e)
{
//add value from textbox to listbox
lstToDo.Items.Add(txtToDo.Text);
}
protected void btnDelete_Click(object sender, EventArgs e)
{
if (this.lstToDo.SelectedIndex >= 0)
this.lstToDo.Items.RemoveAt(this.lstToDo.SelectedIndex);
}
clsToDoList 的代码
公共类 clsToDoList { 公共 Int32 计数 { 得到 { Int32 记录计数; clsDataConnection MyConnection = 新的 clsDataConnection(); MyConnection.Execute("sproc_tblToDo_SelectAll"); RecordCount = MyConnection.Count; 返回记录计数;
}
}
public List<clsToDoItem>List
{
get
{
List<clsToDoItem> NewList = new List<clsToDoItem>();
Int32 RecordCount;
Int32 Index = 0;
clsDataConnection MyConnection = new clsDataConnection();
MyConnection.Execute("sproc_tblToDo_SelectAll");
RecordCount = MyConnection.Count;
while (Index < RecordCount)
{
clsToDoItem BlankPage = new clsToDoItem();
BlankPage.ToDoNo = Convert.ToInt32(MyConnection.DataTable.Rows[Index]["ToDoNo"]);
BlankPage.ToDo = Convert.ToString(MyConnection.DataTable.Rows[Index]["ToDo"]);
NewList.Add(BlankPage);
Index++;
}
return NewList;
}
}
}
【问题讨论】:
-
我们在说什么表? SQL 表或其他一些数据源?
-
创建一个方法,该方法将访问您的数据库并删除您的行,然后更新列表。你会在你的
btnDelete_Click方法中调用这个方法 -
你能显示你班级的代码
MyList。您可以从那里获得帮助。