1

尝试从 gridview 中的标签传递参数,仅传递第一行的标签文本。

不确定缺少什么。

 protected void GV1_OnRowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "edit")
        {


            foreach (GridViewRow row in GridView1.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    Label lbl_taskID = (Label)row.FindControl("lbl_taskID");

                    Session["TaskID"] = lbl_taskID.Text;

                    Response.Redirect("~/tasks_edit.aspx");
                }
            }
        }
    }
4

5 回答 5

1

您正在打破循环Response.Redirect您需要将 do Response.Redirectout 侧循环设置为所有值,您还需要连接lbl_taskID所有行的值而不是过度写入。

protected void GV1_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
    string taskIds = string.Empty;
    if (e.CommandName == "edit")
    {


        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                Label lbl_taskID = (Label)row.FindControl("lbl_taskID");

                if(Session["TaskID"] != null)
                  taskIds = Session["TaskID"].ToString();
                Session["TaskID"] = taskIds + lbl_taskID.Text + ",";                 
            }
        }
        Response.Redirect("~/tasks_edit.aspx");
    }
}
于 2012-12-10T09:58:27.913 回答
0

我使用了 gridview 向导中自动生成的 EDIT 按钮,它正在工作。

很抱歉浪费您的时间。

于 2012-12-18T10:46:26.713 回答
0

您正在使用foreach循环,但您正在使用循环将单个值分配给单个Session变量。那么你期待什么?

但是,我会假设您的最后一行不是第一行。

您需要放在Response.Redirect("~/tasks_edit.aspx")循环之外,因为它将中止当前请求。您可能想要分配当前处于编辑模式的行:

foreach (GridViewRow row in gridView1.Rows)
{
    if (row.RowState == DataControlRowState.Edit)
    {
        Label lbl_taskID = (Label)row.FindControl("lbl_taskID");
        Session["TaskID"] = lbl_taskID.Text;
        break;
    }
}
Response.Redirect("~/tasks_edit.aspx");

旁注:您不需要检查DataControlRowType因为GridView.Rows只返回行DataControlRowType.DataRow(与RowDataBound-event不同)。

编辑:而不是RowCommand我会使用的点击事件LinkButton

protected void EditLink_Clicked(Object sender, EventArgs e)
{
    // get the LinkButton reference
    LinkButton link = (LinkButton)  sender;
    // get the GridViewRow reference via NamingContainer
    GridViewRow row = (GridViewRow) link.NamingContainer;
    Label lbl_taskID = (Label) row.FindControl("lbl_taskID");
    Session["TaskID"] = lbl_taskID.Text;
    Response.Redirect("~/tasks_edit.aspx");
}
于 2012-12-10T10:01:12.973 回答
0

如果只使用第一行,那么为什么要使用 foreach 循环?你可以简单地find the control of the 0th row.

if (e.CommandName == "edit") //this makes sure that you have a row
{
  Label lbl_taskID = (Label)GridView1.Rows[0].FindControl("lbl_taskID");
  Session["TaskID"] = lbl_taskID.Text;  
  Response.Redirect("~/tasks_edit.aspx");
}

如果您的意思是来自当前编辑列的标签文本take the index of the editing column则从CommandArgument并获取标签

if (e.CommandName == "edit") //this makes sure that you have a row
{
      int index = Convert.ToInt32(e.CommandArgument); //currently editing row index
      Label lbl_taskID = (Label)GridView1.Rows[index].FindControl("lbl_taskID");
      Session["TaskID"] = lbl_taskID.Text;  
      Response.Redirect("~/tasks_edit.aspx");
}
于 2012-12-10T10:02:21.743 回答
0

你怎么lbl_taskID每排都有?在你的 foreach 循环中,你在做 -

Label lbl_taskID = (Label)row.FindControl("lbl_taskID");

您实际上只是lbl_taskID在第一行中获取 present 的值。接下来的行将不再具有相同的元素。你的编码是错误的。您将需要在每一行中使用类似这样的名称命名标签 - label0, label1,... 然后在您的代码中您可以执行 -

            int i=0;
            foreach(GridViewRow row in GridView1.Rows)
            {
                 Label xyz = (Label)row.FindControl("Label"+i);
                 Session["TaskID"+i] =xyz.Text; //to have unique session variables
                 i++;
            }
            Response.Redirect("~/tasks_edit.aspx"); // you should redirect only when you come out of the loop
于 2012-12-10T10:07:19.233 回答