passing parameter from gridview - c#

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

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

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

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

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

I have used the auto generated EDIT button from gridview wizard and it is working.
sorry for your time waste.

Related

Gridview row back color not changing on text change event Asp.net C#

Everybody. I know there are lots of similar question but still I am asking for it because the earlier query doesn't meet my requirement. I have look for it, please don't mark it as duplicate. I am trying to change the row color of the Gridview where txt_Id.Text is equal to the Id of the Gridview row. Below is the code :
protected void lnkSelect_Click(object sender, EventArgs e)
{
LinkButton lnkbtn = sender as LinkButton;
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
int Id = Convert.ToInt32(GridView4.DataKeys[gvrow.RowIndex].Value.ToString());
txt_Id.Text = Id.ToString();
foreach (GridViewRow row1 in GridView4.Rows)
{
if (txt_Id.Text != "")
{
if (row1.Cells[1].Text.Equals(txt_Id.Text))
{
row1.BackColor = System.Drawing.Color.Red;
row1.ForeColor = System.Drawing.Color.White;
}
}
}

Why condition in rowdatabound is getting failed?

I am trying to show a button where IsPublished is true, it works but except for the first row in the grid. Why? I have been trying it for so long but it doesn't work at all
protected void gvNITs_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
//LinkButton lb = e.Row.FindControl("btnLinkDownload") as LinkButton;
//if (lb != null)
// ScriptManager.GetCurrent(this).RegisterPostBackControl(lb);
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton Lbtn_change = (LinkButton)e.Row.FindControl("Lbtn_change");
HiddenField hdnPublishNITDate = e.Row.FindControl("hdnPublishedNITdate") as HiddenField;
DateTime? dtPublishedNITDate = string.IsNullOrEmpty(hdnPublishNITDate.Value) == true ? null : (DateTime?)hdnPublishNITDate.Value.ToDate();
HiddenField hdnIsPublishedNIT = e.Row.FindControl("hdnIsPublishedNITs") as HiddenField;
bool IsPublished = hdnIsPublishedNIT.Value.ToBool();
GridView gv = (GridView)sender;
foreach (GridViewRow gvr in gv.Rows)
{
if (IsPublished == true)
{
Lbtn_change.Visible = true;
}
}
}
}
}
It does not work because you have a nested loop. The RowDataBound event is triggered when a row is being added to the GridView. But in the RowDataBound event you loop all the rows in the GridView foreach (GridViewRow gvr in gv.Rows)
If you check gv.Rows.Count you will find that it is 0 in the first row because it has not yet been added to the GridView.
But you do not need that loop anyway since you already have access to Lbtn_change. So set the Visible property without the loop.

asp.net gridview if cell row value is equal then

I am populating a datatable, then binding it to a gridview. Now I'm reading the rows in the grid and coloring the row if value = [x].
The thing when I try to display on the page the row that is colored im getting it duplicated.
Lets say i have colored 1 row but the response.write will be like 100 times the same result.
Below is my code, hope someone can help :
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
string alert = Request.QueryString["check"];
// loop over all the Rows in the Datagridview
foreach (GridViewRow row in gv1.Rows)
{
// if condition is met color the row text
if (gv1.Rows[0].Cells[0].Text.ToString() == alert)
{
Session ["fn"] = gv1.Rows[0].Cells[2].Text;
gv1.Rows[0].ForeColor = Color.Red;
}
Response.Write(Session["fn"]);
}
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
string alert = Request.QueryString["check"];
if (e.Row.Cells[0].Text.ToString() == alert)
{
Session ["fn"] = e.Rows.Cells[2].Text;
e.Rows.ForeColor = Color.Red;
Response.Write(Session["fn"]);
}
}

getting the rowindex of gridview based on the label control value

I have a label control on my page and beneath the label control there is a Gridview.The value that exists in the label control also exists in the Gridview. The label control has value like this
3244|Yellow Ink| Test Link
In the gridview, I have a value 3244 too
3244 yello Ink Test Link
3255 Green Link Test2
I want the row Index of 3244 in my code behind as soon as the page loads. Is their any way to do it.
Not knowing what type of data source you have, one way of doing it would be with LINQ like this:
protected void Page_Load(object sender, EventArgs e)
{
// Fetch the text from your label. (I'm assuming that you have only one label with the text "3244|Yellow Ink| Test Link".
string text = Label1.Text;
// Find the first row or return null if not found.
var resultRow = GridView1.Rows.Cast<GridViewRow>().FirstOrDefault(row =>
{
// Get the id (that I'm guessing is the first (0-index) column/cell)
var id = row.Cells[0].Text;
// Return true/false if the label text starts with the same id.
return text.StartsWith(id);
});
if (resultRow != null)
{
var index = resultRow.RowIndex;
}
}
A shorter version would look like this:
var resultRow = GridView1.Rows.Cast<GridViewRow>()
.FirstOrDefault(r => text.StartsWith(r.Cells[0].Text));
protected void btnJobAppSelected_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridViewJobApplications.Rows)
{
int rowIndex = ((sender as LinkButton).NamingContainer as GridViewRow).RowIndex;
LinkButton btnJobAppSelected = (LinkButton)GridViewJobApplications.Rows[rowIndex].FindControl("btnJobAppSelected");
}
}
In your page load after populating gridView, you can do that like this:
String searchValue = "3244 ";
int rowIndex = -1;
foreach (GridViewRow row in GridViewID.Rows)
{
if (row.Cells[0].Text.ToString().Equals(searchValue))
{
rowIndex = row.RowIndex;
break;
}
}
int YourIndex = rowIndex;

I need to copy the text from a gridview texbox to the others in the column

This is my code in c#
protected void ibtCopiar_Click(object sender, ImageClickEventArgs e)
{
GridViewRow grdrows = DGPlanilla.Rows[0];
TextBox tb1 = (TextBox)grdrows.FindControl("TextBox1");
string valor = tb1.Text;
if (valor != null || valor != "0")
{
foreach (GridViewRow grdrow in DGPlanilla.Rows)
{
grdrow.Cells[5].Text = valor;
}
}
}
this my code for the button
when i debug i see that the value i have in the firts box is pass to the other textboxes in the column, but when it dysplay the page onle the first box show the value i have enter. the other texboxes donĀ“t change.
If the other cells contain textboxes as well, you will need to find them and set their text value.
foreach (GridViewRow grdrow in DGPlanilla.Rows)
{
TextBox toDisplay = (TextBox)grdrow.FindControl("TextBox1");
toDisplay.Text = valor;
}

Categories

Resources