嵌套数据列表的问题

发布于 2024-12-23 17:49:39 字数 3273 浏览 1 评论 0原文

我在 DataList1 中有一个嵌套的 DataList 结构 DataList2,其中在 DataList2 中有两个执行某些命令的Buttons

我想调用一个过程,该过程将 DataList1datakeyfieldDataList2datakeyfield 作为输入。但是,读取 DataList2datakeyfield 时出现问题。这是我的代码:

.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”在当前上下文中不存在

I have a nested DataList structure DataList2 inside DataList1 where I have two Buttons inside DataList2 that perform certain commands.

I want to call a procedure that takes as input the datakeyfield of DataList1 and datakeyfield of DataList2. However there is a problem with reading the datakeyfield of DataList2. Here is my code:

.aspx.cs code

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();
}

Error

The name "DataList2" does not exist in the current context

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梓梦 2024-12-30 17:49:39

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

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

Assuming that your data produces more than one row in DataList1, there is more than one DataList2. ASP.NET therefore doesn't make DataList2 a member of your page (codebehind) class.

Fortunately, in this instance sender (which you called source, same thing) should be the DataList2 that you want. Try casting it to DataList and see what happens.

若能看破又如何 2024-12-30 17:49:39

你犯了几个错误。首先 DataList2 无法直接访问(正如您所注意到的)。其次,您对父 DataList 和子 DataList 使用相同的 ItemIndex。但由于它们是嵌套的,因此它们不具有相同的值,但需要通过不同的数组索引进行访问。

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();
}

You are making a couple of mistakes. First DataList2 is not directly accessible (as you have noticed). And second you are using the same ItemIndex for both the parent and child DataList. But since they are nested they do not have the same values but need to be accesses by a different array index.

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文