Javascript not firing from gridview selected index changed event - c#

I have a gridview with rowdatabound set like this:
protected void gvStatusActivities_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gvStatusActivities, "Select$" + e.Row.RowIndex);
e.Row.ToolTip = "Click to select this row.";
e.Row.Style.Add("cursor", "pointer");
//other code...
}
}
I am trying to call javascript (from inside gridview selected index changing event) to copy row data to clipboard like this:
string strScript="<script type='text/javascript'>CopyDataToClipboard('copy text');</script>";
ClientScript.RegisterStartupScript(typeof(string), "Msg", strScript);
However, the javascript function is not getting called when I select a gridview row. Any help?

Related

GridView row clicked every time page is rendered

Basically everytime I load or reload the page it treats every row in my gridview as if it has been clicked, thus calling the function in the codebehind related to it once for every row which at the time is set to 13 rows max. Thanks for any help.
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Attaching one onclick event for the entire row, so that it will
// fire SelectedIndexChanged, while we click anywhere on the row.
e.Row.Attributes["onclick"] =
ClientScript.GetPostBackClientHyperlink(this.GridView2, "Select$" + e.Row.RowIndex);
}
Console.WriteLine("");
}

how to update datatable hyperlink

I have a datatable and it contains a website url and I am displaying entire data in gridview, I want to add hyperlink to all existing urls before binding to gridview.
I am getting data dynamically from database , So I use autogenenerate= true
is it possible ?
You can listen to the OnRowDataBound event and from there trying to infer which cells contain a URL and then, turn them into a link:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
foreach(TableCell cell in e.Row.Cells)
{
if (cell.Text.StartsWith("http"))
{
cell.Text = $"<a href='{cell.Text}'>{cell.Text}</a>";
}
}
}
}

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

c# GridView RowDataBound Error

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

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

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

Categories

Resources