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;
}
}
Related
I have a grid and I need to get readonly textbox value in gridview c# from code behind
protected void grd1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (txt.Text != "")
{
txt.Attributes.Add("readonly", "readonly");
txt.Visible = false;
lbltaskname.Visible = true;
}
else
{
txt.Attributes.Remove("readonly");
}
}
}
You can do itself on aspx page.
Assuming that dataset is attached(bind) to the grid from code behind, now coming to gridview on aspx page , add textbox control to grid , in text property of texbox assign value from dataset by -
<%# Bind("your_column_name")%>
and add
readonly = true
to textbox.
Thanks
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+
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);
}
}
I have a gridview that should be editable when a row is clicked. This gridview is clickable in a row so when I clicked it, the row will be displayed in other pages for editing reason. I got an error like this
Specified argument was out of the range of valid values.
Parameter name: index.
This is happening for this line:
LinkButton _singleClickButton = (LinkButton)e.Row.Cells[1].Controls[1];
How can I fix this?
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Get reference to button field in the gridview.
LinkButton _singleClickButton = (LinkButton)e.Row.Cells[1].Controls[1];
string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "Select$" + e.Row.RowIndex);
e.Row.Style["cursor"] = "hand";
e.Row.Attributes["onclick"] = _jsSingle;
}
}
}
You should be using something like e.Row.FindControl("linkbuttonid"). This will get you the required link button from the current row, then you can attach your handlers to the same and perform your logic
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";
}
}