Why do I receive old data in my gridview on page load? - c#

The process I am doing is if lbl.Text is "Validated" then disable the checkbox accordingly in grid. The code works fine if paging is not there.
Now the problem is I am using paging and when I click to the next page of grid
the validated things appear with the checkbox enabled.
I checked through breakpoints. Its loading the previous gridpage values during page load event. And after pageloadevent its going design and loads the new values in grid.
//-------loading previous page values of grid here---------
protected void Page_Load(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
Label lbl = (Label)row.FindControl("Labely8");
Label Label23 = (Label)row.FindControl("Label23");
CheckBox checkbox = (CheckBox)row.FindControl("chkRows");
if (lbl.Text == "Validated")
{
checkbox.Enabled = false;
}
else
{
checkbox.Enabled = true;
}
}
}

I think you need to do enable or disable each of the checkboxes individually in the GridView.RowDataBound event rather than all at once in the Page_Load event:
void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox checkbox = (CheckBox)e.Row.FindControl("chkRows");
checkbox.Enabled = e.Row.Cells["nameOfCellWithLabel"].Text == "Validated";
}
}

Related

One GridView and many LinkButtons (asp .net WebForms)

I have a GridView and dynamically added LinkButton in GridView cell:
protected void TestGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (var l in links)
{
e.Row.Cells[6].Controls.Add(l.button);
PostBackTrigger trigger = new PostBackTrigger();
trigger.ControlID = l.button.ID;
UpPanel.Triggers.Add(trigger);
}
}
}
Links are added, its ok, but clicking on link, GridView refreshes and links disappears. When I delete this condition from page_load function, I have a problem with page index changing event - GridView does not refresh:
if (!IsPostBack)
{
TestGrid.DataBind();
}
What can I do to stop links disappearing and keep page index functional? And is there a better way to add many LinkButtons in one GridView cell? Thank you!

Hide gridview cell (value) base on other cell value

I have a Gridview with ItemTemplate using some Labels and 3 Button (Deactivate, Delete and Edit) as seen in picture below:
I want to hide these buttons from some users base on their username (Label UserName on Gridview), for example:
If UserName == "some string" then hide the Deactivate, Delete and Edit buttons
How do I do this in code behind RowDataBound event?
Yes by using Gridview RowDataBound event you can do that
protected void grd1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl=e.Row.FindControl("your cotrol Id")as Label;
if(lbl!=null && lbl.Text.Trim()=="some string")
{
e.Row.FindControl("deactivate btn Id").Visible = false;
e.Row.FindControl("delete btn Id").Visible = false;
e.Row.FindControl("edit btn Id").Visible = false;
}
}
}
You can get the row data by column index, in the RowDataBound event you need to check if its a data row, not a header or footer..etc
if(e.Row.RowType == DataControlRowType.DataRow)
{
if(e.Row.Cells[2].Text = "some string")
{
Button delete = (Button)e.Row.FindControl("control id to hide");
delete.Visisble = false;
}
}

How to disable the CheckBox in the GridView Header?

In this GridView, I want to disable all the CheckBox in the GridView during the View Mode.
I can disable the GridView Row cells like
foreach (GridViewRow GVR in gvPODetails.Rows)
{
GVR.Cells[1].Enabled =
GVR.Cells[11].Enabled = false;
}
But I dont know how to disable the checkbox in the HeaderTemplate of the Template Field. How to do this?
Recipe:
Handle OnRowDataBound on your GridView
Detect whether you are iterating through the header by using e.Row.RowType == DataControlRowType.Header and get a reference to your checkbox control using e.Row.FindControl(checkBoxID)
Set the Enabled property to False
Try this code:
void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header ) {
MyClass myObj = (myObj)e.Row.DataItem;
CheckBox cb = (CheckBox)e.Row.FindControl("myCheckBox");
cb.Enabled=false;
}
}
And if u want to do using javascript then click here
http://forums.asp.net/t/1742352.aspx/1?How+to+enable+and+disable+CheckBox+in+the+GridView+using+JavaScript+

GridView column with checkbox field with SQL

I have GridView with 2 columns.
The first column is: test-label (TemplateField)
The second: checkbox (asp:CheckBoxField) that connect to sql table with bit column (done).
I want that on page load - the page will check every row, where the checkbox = true, the test-label.visble will be false.
I know how to write code with SELECT statement to check the value from the SQL table, but don't know how to check every row on the gridview on the page-load.
how can I do that?
(i can't use findcontroll for the checkbox because it's checkboxfield and not just "checkbox".
<asp:CheckBoxField DataField="done" SortExpression="done" HeaderText="done?" />
so, what can I do here? maybe to replace that field with regular cb? (i don't know how to do there databind - on the regular cb).
you can use GridView.RowDataBound Event
so you can do something like
protected void GVRowDataBound(object sender, GridViewRowEventArgs e)
{
var check = (CheckBox) e.Row.FindControl("ID"); // ID is id of the checkbox
var lable = (Label) e.Row.FindControl("LableID");
if(check != null && lable != null)
{
if(check.Checked)
{
lable.Visible = false;
}
}
}
You can't do it in Page.Load because the GridView isn't databound yet.
Try handling GridView.RowDataBound.
Code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox cb = (CheckBox)e.Row.FindControl("checkbox");
Label lbl = (Label)e.Row.FindControl("test-label");
lbl.Visible = !(cb.Checked);
}
}

Accessing Gridview columns by Row.Cells

Hai I am using the following code to access columns in a row but always displays empty string when using Row.Cells[1].Text I am using this code in GridView Unload event handler.
Thanks inn advance.
foreach (GridViewRow row in grvSearchRingTone.Rows)
{
String coltext = row.Cells[1].Text;
}
I would suggest using the Gridview Databound event instead. Because the
Unload event occurs when the server control is unloaded from memory.
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow row in grvSearchRingTone.Rows)
{
String coltext = row.Cells[1].Text;
}
}
Databound events occur after the server control binds to a data source.
To understand how gridview events work, look at MSDN
It could be done in Gridviews RowDataBound Event ie
protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
String coltext = e.Row.Cells[1].Text
}
else if(e.Row.RowType == DataControlRowType.DataRow)
{
String coltext = e.Row.Cells[1].Text
}
}
Hope this helps.

Categories

Resources