使用ItemCommand和RowCommand列出视图和网格视图

本文关键字:视图 网格 RowCommand ItemCommand 使用 | 更新日期: 2023-09-27 17:50:55

好的,所以我正在学习网格视图和列表视图…目前我所拥有的是一个工作网格视图。下面的代码如下:

protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName.CompareTo("Delete") == 0)
{
    int newIndex = Convert.ToInt32(e.CommandArgument);
    string studentEmail = GridView1.Rows[newIndex].Cells[1].Text;
    AccessDataSource1.DeleteCommand = "DELETE FROM [CoursesTaken] WHERE [CourseID] = '" + studentEmail + "'";
    AccessDataSource1.Delete();
    AccessDataSource1.DataBind();
}
}

这对我来说很合适。所以我为它做了一些其他的按钮,并决定转移到列表视图并学习它。我不知道为什么我的删除按钮不工作。我目前只能做一个更新按钮。这就是我现在写的代码

string StudentIDLabel = ((Label)(e.Item.FindControl("StudentIDLabel"))).Text.Trim();
string CourseIDLabel = ((Label)(e.Item.FindControl("CourseIDLabel"))).Text.Trim();
string GradeLabel = ((Label)(e.Item.FindControl("GradeLabel"))).Text.Trim();
AccessDataSource1.DeleteCommand = "DELETE FROM [CoursesTaken] WHERE [StudentID] = '" + StudentIDLabel + "' AND [CourseID] = '" + CourseIDLabel + "' AND [Grade] ='" + GradeLabel + "'";
AccessDataSource1.Delete();
AccessDataSource1.DataBind();

我认为这可能是我的SQL语句的方式,我已经改变了很多。但现在我得到了一个参数错误。我想可能是有什么东西没有正确通过…

所以我把SQL delete语句改成了
 AccessDataSource1.DeleteCommand = "DELETE FROM [CoursesTaken] WHERE [StudentID] = '" +      StudentIDLabel + "'" + CourseIDLabel + "'" + GradeLabel + "'";

这给了我错误:

查询表达式'[StudentID] = 'aaalshehri@mail.usi.edu'CS 402'A "中的语法错误(缺少运算符)。"

StudentID是第一列,其次是CourseID,然后是Grade

我真的很纠结这个问题,也许能从一个不像我这么环保的人那里得到一点指导,我会非常感激。

谢谢!

使用ItemCommand和RowCommand列出视图和网格视图

也许StudentIDLabel, CourseIDLabel, GradeLabel是NULL。

string StudentIDLabel = ((Label)(e.Item.FindControl("StudentIDLabel"))).Text.Trim();
string CourseIDLabel = ((Label)(e.Item.FindControl("CourseIDLabel"))).Text.Trim();
string GradeLabel = ((Label)(e.Item.FindControl("GradeLabel"))).Text.Trim();

您需要向我们展示您正在执行此操作的整个方法/事件,以便我们可以看到:

string StudentIDLabel = ((Label)(e.Item.FindControl("StudentIDLabel"))).Text.Trim();
string CourseIDLabel = ((Label)(e.Item.FindControl("CourseIDLabel"))).Text.Trim();
string GradeLabel = ((Label)(e.Item.FindControl("GradeLabel"))).Text.Trim();
AccessDataSource1.DeleteCommand = "DELETE FROM [CoursesTaken] WHERE [StudentID] = '" + StudentIDLabel + "' AND [CourseID] = '" + CourseIDLabel + "' AND [Grade] ='" + GradeLabel + "'";
AccessDataSource1.Delete();
AccessDataSource1.DataBind();

——编辑——

你需要有按钮的索引。所以在aspx代码中,将这个添加到按钮的commandparameter中以获得索引:

CommandArgument='<%# Container.DataItemIndex %>'

在后台代码中,我们现在可以使用以下方式获取标签:

Label StudentIDLabel = ListView1.Item[e.CommandArgument].FindControl("StudentIDLabel");
Label CourseIDLabel = ListView1.Item[e.CommandArgument].FindControl("CourseIDLabel");
Label GradeLabel = ListView1.Item[e.CommandArgument].FindControl("GradeLabel");
AccessDataSource1.DeleteCommand = string.Format("DELETE FROM [CoursesTaken] WHERE [StudentID] = '{0}' AND [CourseID] = '{1}' AND [Grade] = '{2}'", StudentIDLabel.Text, CourseIDLabel.Text, GradeLabel.Text)
AccessDataSource1.Delete();
AccessDataSource1.DataBind();

如果Student Id是唯一的,你应该可以做

string sql= string.Format("DELETE FROM [CoursesTaken] WHERE [StudentID] ={0}",
                                                             StudentIDLabel);
AccessDataSource1.DeleteCommand = sql;