[英]passing parameter from gridview
trying to pass parameter from a label in gridview, only the label text from the first row are passed. 尝试从gridview中的标签传递参数时,仅传递第一行的标签文本。
not sure what is missing. 不知道丢失了什么。
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");
}
}
}
}
You are breaking loop with Response.Redirect
You need to put the do Response.Redirect
out side loop to set all value, you also need to concatenate value of lbl_taskID
all rows instead of over writting. 您正在使用
Response.Redirect
中断循环。您需要将do Response.Redirect
外循环放置以设置所有值,还需要将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");
}
}
You are using a foreach
loop, but you are using the loop to assign a single value to a single Session
variable. 您使用的是
foreach
循环,但使用循环将单个值分配给单个Session
变量。 So what do you expect? 那你期望什么?
However, i would assume that your last row is assigned not the first. 但是,我假设您的最后一行不是第一行。
You need to put Response.Redirect("~/tasks_edit.aspx")
outside of the loop since it will abort the current request. 您需要将
Response.Redirect("~/tasks_edit.aspx")
放在循环之外,因为它将中止当前请求。 You might want to assign the row which is currently in edit-mode: 您可能要分配当前处于编辑模式的行:
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");
Side-note: you don't need to check the DataControlRowType
since GridView.Rows
only returns rows with DataControlRowType.DataRow
anyway(unlike RowDataBound
-event). 旁注:您无需检查
DataControlRowType
因为无论如何GridView.Rows
仅返回具有DataControlRowType.DataRow
行(与RowDataBound
-event不同)。
Edit : Instead of RowCommand
i would use the click event of the LinkButton
: 编辑 :代替
RowCommand
我将使用LinkButton
的click事件:
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");
}
If only the first row is used, then why you using foreach loop? 如果仅使用第一行,那么为什么要使用foreach循环? You can simply
find the control of the 0th row.
您可以简单地
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");
}
If you mean label text from currently editing column then, take the index of the editing column
from the CommandArgument
and get the label 如果您是指当前编辑列中的标签文本 ,
take the index of the editing column
从CommandArgument
获取take the index of the editing column
并获取标签
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");
}
How come you have lbl_taskID
in every row? 您
lbl_taskID
每行都有lbl_taskID
? In your foreach loop your doing- 在您的foreach循环中,您的操作-
Label lbl_taskID = (Label)row.FindControl("lbl_taskID");
You are actually only taking the value of lbl_taskID
present in your first row. 实际上,您仅采用第一行中存在的
lbl_taskID
的值。 The next rows will not have the same element again. 接下来的行将不再具有相同的元素。 Your coding is wrong.
您的编码错误。 You will need to name your labels in each row with some thing like this-
label0
, label1
,... then in your code you can do- 您将需要在每行中用诸如
label0
名称命名标签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
I have used the auto generated EDIT button from gridview wizard and it is working. 我已经使用了从gridview向导自动生成的EDIT按钮,它正在工作。
sorry for your time waste. 很抱歉浪费您的时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.