When I click on any row in gridview, it is showing an error message.
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> 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.
When I set EnableEventValidation="false" in the page directive, the error mesage is not shown. But still , on the row click in gridview, the value is not populated in the corresponding textbox.
Though there is value in the gridview cell, gvStudents.SelectedRow.Cells[0].Text is showing null.
suryaacd
Member
108 Points
286 Posts
How to select a row from GridView
Feb 17, 2012 08:03 AM|LINK
Hi,
I want to select a row in gridview so that the corresponding textbox values are filled on the click .
I am not using any link button. Can anyone please tell me how this is done.
My project is in C# 4.0.
Thanks.
Surya.
Sum8
Contributor
4141 Points
931 Posts
Re: How to select a row from GridView
Feb 17, 2012 08:08 AM|LINK
Use OnSelectedIndexChanged event of the GridView.
Sumit Pathak
------------------
ThisPost = Helped == True ? "Mark As Answer" : "Elaborate your problem in more details"
suryaacd
Member
108 Points
286 Posts
Re: How to select a row from GridView
Feb 17, 2012 08:33 AM|LINK
Hi.Thanks for the reply.
Here is my code
protected void gvStudents_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow gvRow = gvStudents.SelectedRow;
txtName.Text = gvRow.Cells[0].Text;
}
My requirement is to populate the selected Gridview row value into the corresponding textbox. But here the event is not getting fired.
Am I missing anything?
Please help.
Thanks.
Surya
Sum8
Contributor
4141 Points
931 Posts
Re: How to select a row from GridView
Feb 17, 2012 09:46 AM|LINK
Have you set the OnSelectedIndexChanged property of your GridView?
Also, in the RowDataBound event of the GridView, write this:
if (e.Row.RowType == DataControlRowType.DataRow) e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gvStudents, "Select$" + e.Row.RowIndex.ToString());Sumit Pathak
------------------
ThisPost = Helped == True ? "Mark As Answer" : "Elaborate your problem in more details"
suryaacd
Member
108 Points
286 Posts
Re: How to select a row from GridView
Feb 20, 2012 03:51 AM|LINK
Here is my code
<asp:GridView ID="gvStudents" runat="server" AutoGenerateColumns="False"
AllowPaging="True" Width="315%"
onpageindexchanging="gvStudents_PageIndexChanging"
onrowdatabound="gvStudents_RowDataBound" onselectedindexchanged="gvStudents_SelectedIndexChanged"
>
protected void gvStudents_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gvStudents, "Select$" + e.Row.RowIndex.ToString());
}
}
protected void gvStudents_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow gvRow = gvStudents.SelectedRow;
txtName.Text = gvRow.Cells[0].Text;
}
When I click on any row in gridview, it is showing an error message.
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> 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.
Please help.
suryaacd
Member
108 Points
286 Posts
Re: How to select a row from GridView
Feb 20, 2012 04:37 AM|LINK
When I set EnableEventValidation="false" in the page directive, the error mesage is not shown. But still , on the row click in gridview, the value is not populated in the corresponding textbox.
Though there is value in the gridview cell, gvStudents.SelectedRow.Cells[0].Text is showing null.
can anyone please help.
Sum8
Contributor
4141 Points
931 Posts
Re: How to select a row from GridView
Feb 20, 2012 05:19 AM|LINK
Kindly make sure that you are getting an Index of the selected row in gvStudents.SelectedRow .
Does your dezired value is in the first column? If not then use respected cell index in the quoted statement.
Sumit Pathak
------------------
ThisPost = Helped == True ? "Mark As Answer" : "Elaborate your problem in more details"
suryaacd
Member
108 Points
286 Posts
Re: How to select a row from GridView
Feb 20, 2012 08:41 AM|LINK
Yes . The value should br from the first column. Eventhough the grid display values, the gvStudents.SelectedRow.Cells[0].Text is showing null.
I don't know what went wrong.
Sum8
Contributor
4141 Points
931 Posts
Re: How to select a row from GridView
Feb 20, 2012 09:07 AM|LINK
Kindly post the ASPX code of the GridView for more details.
Sumit Pathak
------------------
ThisPost = Helped == True ? "Mark As Answer" : "Elaborate your problem in more details"
suryaacd
Member
108 Points
286 Posts
Re: How to select a row from GridView
Feb 20, 2012 09:36 AM|LINK
Hi, This is my aspx code for gridview.
<asp:GridView ID="gvStudents" runat="server" AutoGenerateColumns="False"
AllowPaging="True" Width="315%"
onpageindexchanging="gvStudents_PageIndexChanging"
onrowdatabound="gvStudents_RowDataBound" onselectedindexchanged="gvStudents_SelectedIndexChanged"
>
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<%-- <td width="10%"></td>--%>
<td width="30%">Name</td>
<td width="30%">Place</td>
<td width="20%">Pincode</td>
<td width="20%">Email</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<%--<td width="10%">
<asp:LinkButton ID="lnkEdit" runat="server">Select</asp:LinkButton>
</td>--%>
<td width="30%">
<asp:Label ID="lblName" Text='<%#DataBinder.Eval(Container.DataItem, "StudentName")%>' runat="server" ></asp:Label>
</td>
<td width="30%">
<asp:Label ID="lblPlace" runat="server" Text='<%#Eval("Place")%>'></asp:Label></td>
<td width="20%">
<asp:Label ID="lblPincode" runat="server" Text='<%#Eval("Pincode") %>'></asp:Label></td>
<td width="20%">
<asp:Label ID="lblEmail" runat="server" Text='<%#Eval("EmailId") %>'></asp:Label></td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>