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
Related
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("");
}
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>";
}
}
}
}
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?
Here is my ASP.NET code snippet.
I am trying to select a GridView Row and add the selected row items in Session Variable.
// ======================== MyGridView ========================
protected void GridView_MyGridView_SelectedIndexChanged(object sender, EventArgs e)
{
// Get Selected Row Items
Items myitems = new Items();
Session["Items"] = myitems;
((Invoices)Session["Items"]).ItemNo = int.Parse(((GridViewRow)(((WebControl)(sender)).Parent.Parent)).Cells[0].Text);
}
I do NOT want to use the clickable select button that comes with the GridView Columns.
I handled by the following code:
protected void GridView_MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
e.Row.ToolTip = "Click to Select a Visit.";
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(sender as GridView, "SELECT$" + e.Row.RowIndex);
}
}
Now, when I run the program, I get the following error as soon as the gridview selected row changes:
Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlForm' to type 'System.Web.UI.WebControls.GridViewRow'.
Please, provide your feedback.
Not sure what you are trying to do but your cast is what is causing it. Your call to Parent.Parent is going too far up the tree and you have ended up with the form container and are no longer within the GridView at all.
Instead of the cast you are attempting, why not just try:
GridViewRow row = GridView_MyGridView.SelectedRow;
((Invoices)Session["Items"]).ItemNo = int.Parse(row.Cells[0].Text);
Keep in mind, this will still error out if there is no text in the cell so you may want to look at handling that with additional logic or a TryParse() instead.
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);
}
}