I'm implementing a feature that when the user press on any point in the row in a GridView the row will be selected instead of Select button.
To implement that, I'm using the following code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Set the hand mouse cursor for the selected row.
e.Row.Attributes.Add("OnMouseOver", "this.style.cursor = 'hand';");
// The seelctButton exists for ensuring the selection functionality
// and bind it with the appropriate event hanlder.
LinkButton selectButton = new LinkButton()
{
CommandName = "Select",
Text = e.Row.Cells[0].Text
};
e.Row.Cells[0].Controls.Add(selectButton);
e.Row.Attributes["OnClick"] =
Page.ClientScript.GetPostBackClientHyperlink(selectButton, "");
}
}
With the code above, there are the following problems:
- This works fine only if I
EnableEventValidation
for the page is set tofalse
. - The
SelectedIndexChanged
is only fired just in case theGrid.DataBind()
is called inPage_Load
for the page (in every postback).
Am I doing something wrong? Is there a better implementation?
Edit:
When EnableEventValidation
is set to true
, the following error will appear:
Invalid postback or callback argument. Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
Try this one add OnSelectedIndexChanged event in grid
and then on code behind
and set EnableViewState="false" but here u have to perform two more things that u have already done in your code means set EnableEventValidation to false and Grid Databinding on page Load..
You must add this on every postback and not only on databinding. Therefore you should use the RowCreated-Event of the GridView.
For example
(C#):
(VB.Net):
Instead of doing it on
RowCreated
, you could do it onRender()
. That way you could use the overload ofGetPostBackClientHyperlink
with true onregisterForEventValidation
and avoid the "invalid postback/callback argument" error.Something like this:
Try using
<asp:LinkButton>
around your<tr>
in ASPX code, set the LinkButton'sCommandName='Select'
And in item command event, process this command and set the style of a selected row!