I am working after a long time in grid and facing an issue.
In grid there are TemplateFields having linkbutton for edit and delete. OnRowCommand event is not firing in gird. An important point is that if I autogenerate Edit or Delete buttons its RowCommand event fires.
Here is my aspx code:
<asp:GridView ID="gvContact" runat="server" AllowPaging="True" AllowSorting="True"
DataKeyNames="MailId" AutoGenerateColumns="False" CssClass="ContactsGridViewStyle"
GridLines="None" OnRowEditing="gv_RowEdit" OnRowDataBound="gv_RowDataBound" OnRowCancelingEdit="gv_CancellingEdit"
OnRowUpdating="gv_RowUpdating" Width="600px" OnPageIndexChanging="gv_PageChanging"
OnRowCommand="gv_RowCommand" EmptyDataText="No Record found" EmptyDataRowStyle-ForeColor="Red"
EmptyDataRowStyle-HorizontalAlign="Center">
<RowStyle CssClass="RowStyle" />
<EmptyDataRowStyle CssClass="EmptyRowStyle" />
<PagerStyle CssClass="PagerStyle" />
<SelectedRowStyle CssClass="SelectedRowStyle" />
<HeaderStyle CssClass="HeaderStyle" />
<EditRowStyle CssClass="EditRowStyle" />
<AlternatingRowStyle CssClass="AltRowStyle" />
<Columns>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox><br />
<asp:RequiredFieldValidator runat="server" ID="rfvName" ControlToValidate="txtName"
Display="Dynamic" Text="Name is a required field"></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email Id">
<EditItemTemplate>
<asp:TextBox ID="txtMessageTo" runat="server" Text='<%# Bind("MessageTo") %>'></asp:TextBox><br />
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator2" ControlToValidate="txtMessageTo"
Display="Dynamic" Text="Email is a required field"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" Display="Dynamic"
Text="Email address is Invalid" SetFocusOnError="true" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]开发者_开发知识库\w+)*\.\w+([-.]\w+)*"
ControlToValidate="txtMessageTo">
</asp:RegularExpressionValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblMessageTo" runat="server" Text='<%# Bind("MessageTo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton runat="server" ID="ibtn" CommandArgument='<%#Eval("RowId")%>' CommandName="EEE"
ImageUrl="~/Images/cross.png" />
<asp:LinkButton ID="lbtnEdit" runat="server" Text="Edit" CommandArgument='<%# Eval("RowId")%>'
CommandName="EE"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton runat="server" ID="lbtnUpdate" Text="Update" CommandName="UU"></asp:LinkButton>
<asp:LinkButton runat="server" ID="lbtnCancel" Text="Cancel" CommandName="CC"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbtnDelete" runat="server" Text="Delete" CommandArgument='<%# Eval("RowId")%>'
CommandName="DD"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In CS file I am binding grid in !Page.IspostBack.
Any guidance please.
I've spent basically a whole day solving this problem myself; this question seemed to show up quite a lot in my Google queries so hopefully this answer will be helpful even though the question itself is quite old.
The issue is caused by the validators on the page, such as <asp:RequiredFieldValidator>
The validation of these fields is performed when the update events are fired on the grid, not just when the "OK" button is pressed. Presumably the validation is failing at this point, and therefore the update events are being cancelled before reaching your handlers.
The solution to this is to use the ValidationGroup attribute of the validators and the action button intended to fire those validations, as described in Scott Guthrie's post here: http://weblogs.asp.net/scottgu/archive/2004/10/24/246945.aspx
You must not add the !Page.IsPostBack condition and put your grid binding into
protected override void OnInit() {
MyGrid.DataBind();
base.OnInit();
}
If you bind a grid in "code" instead of an Object/Sql/Entity|Datasource, you must add it in the OnInit(); because then, everything it needs will be registred into ViewState.
Note.: Sorry for my english, I usualy talk french.
精彩评论