2

我有一个嵌套的 DataList 结构DataList2,其中DataList1有 2 个执行某些命令,我​​想调用一个将of和of作为输入的过程,但是读取of时出现问题,这是我的代码:ButtonsDataList2datakeyfieldDataList1datakeyfieldDataList2datakeyfieldDataList2

.aspx.cs 代码

protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
{
    if ((e.CommandName == "accept") && (e.CommandArgument != null))
    {
        string connStr = ConfigurationManager.ConnectionStrings["MyDbConn"].ToString();
        SqlConnection conn = new SqlConnection(connStr);
        int team_ID = (int)DataList1.DataKeys[e.Item.ItemIndex];
        SqlCommand cmd = new SqlCommand("accept_member", conn);
        string member = (string)DataList2.DataKeys[e.Item.ItemIndex];
        cmd.CommandType = CommandType.StoredProcedure;
        string email = Session["email"].ToString();
        int course_ID = Convert.ToInt32(Request.QueryString["courseID"]);
        cmd.Parameters.Add(new SqlParameter("@course_ID", course_ID));
        cmd.Parameters.Add(new SqlParameter("@team_ID", team_ID));
        cmd.Parameters.Add(new SqlParameter("@myemail", email));
        cmd.Parameters.Add(new SqlParameter("@member", member));
        cmd.Parameters.Add(new SqlParameter("@respond", 1));
        SqlParameter count = cmd.Parameters.Add("@count", SqlDbType.Int);
        count.Direction = ParameterDirection.Output;

        conn.Open();
        cmd.ExecuteNonQuery();

        if (@count.Value.ToString().Equals("1"))
        {
            Response.Write(@member + " " + "joined your team");

        }
        else
        {
            Response.Write("team is full or not found");
        }
    }
    else if ((e.CommandName == "reject") && (e.CommandArgument != null))
    {
        int team_ID = (int)DataList1.DataKeys[e.Item.ItemIndex];
        string connStr = ConfigurationManager.ConnectionStrings["MyDbConn"].ToString();
        SqlConnection conn = new SqlConnection(connStr);
        string member = (string)DataList2.DataKeys[e.Item.ItemIndex];
        SqlCommand cmd = new SqlCommand("accept_member", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        string email = Session["email"].ToString();
        int course_ID = Convert.ToInt32(Request.QueryString["courseID"]);
        cmd.Parameters.Add(new SqlParameter("@course_ID", course_ID));
        cmd.Parameters.Add(new SqlParameter("@team_ID", team_ID));
        cmd.Parameters.Add(new SqlParameter("@myemail", email));
        cmd.Parameters.Add(new SqlParameter("@member", member));
        cmd.Parameters.Add(new SqlParameter("@respond", "0"));
        SqlParameter count = cmd.Parameters.Add("@count", SqlDbType.Int);
        count.Direction = ParameterDirection.Output;

        conn.Open();
        cmd.ExecuteNonQuery();

        if (@count.Value.ToString().Equals("1"))
        {
            Response.Write(@member + " " + "has been rejected");
        }
        else
        {
            Response.Write("team is full or not found");
        }
    }

    DataList2.DataBind();
}

错误:

当前上下文中不存在名称“DataList2”

4

2 回答 2

1

假设您的数据在 DataList1 中生成不止一行,则 DataList2 不止一个。因此,ASP.NET 不会使 DataList2 成为您的页面(代码隐藏)类的成员。

幸运的是,在这种情况下,发送者(您称为源,同样的东西)应该是您想要的 DataList2。尝试将其转换为 DataList 看看会发生什么。

于 2011-12-31T21:52:14.233 回答
1

你犯了几个错误。FirstDataList2不能直接访问(正如您所注意到的)。其次,您ItemIndex对父 DataList 和子 DataList 使用相同的内容。但由于它们是嵌套的,它们没有相同的值,但需要由不同的数组索引访问。

protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
{
    //cast the source back to a the datalist
    DataList datalist2 = source as DataList;

    //get the value from the nested datalist
    string childValue = (string)datalist2.DataKeys[e.Item.ItemIndex];

    //get the parent object of datalist2
    DataListItem dli = datalist2.NamingContainer as DataListItem;

    //get the value from the parent datalist using the itemindex of the parent, not the child
    int parentValue = (int)DataList1.DataKeys[dli.ItemIndex];

    //show results
    Label1.Text = parentValue + " - " + childValue;

    //rebind datalist2
    datalist2.DataBind();
}
于 2017-06-12T10:21:07.700 回答