嵌套数据列表的问题
我在 DataList1
中有一个嵌套的 DataList 结构 DataList2
,其中在 DataList2
中有两个执行某些命令的Buttons
。
我想调用一个过程,该过程将 DataList1
的 datakeyfield
和 DataList2
的 datakeyfield
作为输入。但是,读取 DataList2
的 datakeyfield
时出现问题。这是我的代码:
.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您的数据在 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.
你犯了几个错误。首先
DataList2
无法直接访问(正如您所注意到的)。其次,您对父 DataList 和子 DataList 使用相同的ItemIndex
。但由于它们是嵌套的,因此它们不具有相同的值,但需要通过不同的数组索引进行访问。You are making a couple of mistakes. First
DataList2
is not directly accessible (as you have noticed). And second you are using the sameItemIndex
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.