Reputation: 77
trying to pass parameter from a label in gridview, only the label text from the first row are passed.
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");
}
}
}
}
Upvotes: 1
Views: 1380
Reputation: 77
I have used the auto generated EDIT button from gridview wizard and it is working.
sorry for your time waste.
Upvotes: 0
Reputation: 460058
You are using a foreach
loop, but you are using the loop to assign a single value to a single Session
variable. 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. 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).
Edit: Instead of RowCommand
i would use the click event of the 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");
}
Upvotes: 0
Reputation: 4962
How come you have lbl_taskID
in every row? In your foreach loop your doing-
Label lbl_taskID = (Label)row.FindControl("lbl_taskID");
You are actually only taking the value of lbl_taskID
present in your first row. 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-
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
Upvotes: 0
Reputation: 33809
If only the first row is used, then why you using foreach loop? You can simply 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
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");
}
Upvotes: 0
Reputation: 148110
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.
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");
}
}
Upvotes: 1