[英]RowEditing command not firing
I have a gridview that adds rows in the footertemplate that works fine. 我有一个gridview,可以在footertemplate中添加行,效果很好。 I want to add RowEditing and RowDeleting to the grid.
我想将RowEditing和RowDeleting添加到网格中。 I added the RowEditing method but when I click the 'Edit' Button, the add row is displayed in the footer as if it is the "Add Row' command. When I created a prototype of a grid, I had a column just for editing and it was a commandfield. WHen you click the button, the row would change so that all of the fields were editable. How can I do that when the field is an ItemTemplate field?
我添加了RowEditing方法,但是当我单击“编辑”按钮时,添加行显示在页脚中,就好像它是“添加行”命令一样。当我创建网格原型时,我有一列仅用于编辑这是一个命令字段。当您单击按钮时,该行将更改,以便所有字段都可编辑。当该字段是ItemTemplate字段时,该怎么做?
This is my markup: 这是我的标记:
<asp:GridView ID="MappingGridView" runat="server" AllowSorting="True" AutoGenerateColumns="False" Caption="Enrollment Mapping Information" CaptionAlign="Top"
CssClass="grid" HorizontalAlign="Left" ShowFooter="True" AllowPaging="True" PageSize="4" ShowHeaderWhenEmpty="true" OnPageIndexChanging="MappingGridView_PageIndexChanging"
OnRowDataBound="MappingGridView_RowDataBound" OnRowCommand="MappingGridView_RowCommand" Width="1051px" OnRowEditing="MappingGridView_RowEditing">
<Columns>
<asp:TemplateField HeaderText="MappingID" SortExpression="mgvMappingID">
<ItemTemplate>
<asp:Label ID="mgvLblMappingID" runat="server" Text='<%# Bind("EnrollmentMappingID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SectionID">
<EditItemTemplate>
<asp:TextBox ID="mgvDdlSectionID" runat="server" Text='<%# Bind("SectionID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="mgvLblSectionID" runat="server" Text='<%# Bind("SectionID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="mgvInsertSectionID" runat="server" ReadOnly="true" Text="" Width="90%"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SchoologyCourseID" SortExpression="mgvSchoologyCourseID">
<EditItemTemplate>
<asp:TextBox ID="mgvEditSchoologyCourseID" runat="server" Text='<%# Bind("SchoologyCourseID") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="ReqFieldValEditSchoologyCourseID" ControlToValidate="mgvEditSchoologyCourseID" runat="server"
ErrorMessage="Required field." ValidationGroup="MappingGrid" Display="Dynamic"
CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegExpValEditSchoologyCourseID" ControlToValidate="mgvEditSchoologyCourseID" runat="server"
ErrorMessage="Enter only integers." ValidationGroup="MappingGrid" ValidationExpression="^\d+$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="mgvLblSchoologyCourseID" runat="server" Text='<%# Bind("SchoologyCourseID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="mgvInsertSchoologyCourseID" runat="server" Width="90%"></asp:TextBox>
<asp:RequiredFieldValidator ID="ReqFieldValSchoologyCourseID" ControlToValidate="mgvInsertSchoologyCourseID" runat="server"
ErrorMessage="Required field." ValidationGroup="MappingGrid" Display="Dynamic"
CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegExpValSchoologyCourseID" ControlToValidate="mgvInsertSchoologyCourseID" runat="server"
ErrorMessage="Enter only integers." ValidationGroup="MappingGrid" ValidationExpression="^\d+$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CECityActivityID" SortExpression="mgvCECityActivityID">
<EditItemTemplate>
<asp:TextBox ID="mgvEditCECityActivityID" runat="server" Text='<%# Bind("CECityActivityID") %>'></asp:TextBox>
<asp:RegularExpressionValidator ID="RegExpValEditCECityID" ControlToValidate="mgvEditCECityActivityID" runat="server"
ErrorMessage="Enter 0-9, A-F, and hyphens. Maximum length is 50." ValidationGroup="MappingGrid" ValidationExpression="^[0-9A-Fa-f-]{0,50}$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="mgvLblCECityActivityID" runat="server" Text='<%# Bind("CECityActivityID") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="mgvInsertCECityActivityID" runat="server" Width="90%"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegExpValCECityID" ControlToValidate="mgvInsertCECityActivityID" runat="server"
ErrorMessage="Enter only 0-9, A-F, and hyphens; maximum length is 50." ValidationGroup="MappingGrid" ValidationExpression="^[0-9A-Fa-f-]{0,50}$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Is Active" SortExpression="mgvIsActive">
<EditItemTemplate>
<asp:DropDownList ID="mgvEditIsActive" runat="server" Text='<%# Bind("IsActive") %>'>
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="mgvLblIsActive" runat="server" Text='<%# Bind("IsActive") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="mgvInsertIsActive" runat="server" Width="90%">
<asp:ListItem Selected="True">Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Max. Enrollment">
<EditItemTemplate>
<asp:TextBox ID="mgvEditMaxEnrollment" runat="server" Text='<%# Bind("MaxEnrollment") %>'></asp:TextBox>
<asp:RegularExpressionValidator ID="RegExpValEditMaxEnroll" ControlToValidate="mgvEditMaxEnrollment" runat="server"
ErrorMessage="Enter only integers." ValidationGroup="MappingGrid" ValidationExpression="^\d+$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="mgvLblMaxEnrollment" runat="server" Text='<%# Bind("MaxEnrollment") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="mgvInsertMaxEnrollment" runat="server" Width="90%"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegExpValMaxEnroll" ControlToValidate="mgvInsertMaxEnrollment" runat="server"
ErrorMessage="Enter only integers." ValidationGroup="MappingGrid" ValidationExpression="^\d+$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action" ShowHeader="False">
<EditItemTemplate>
<asp:Button ID="mgvImgUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:Button>
<asp:Button ID="mgvImgCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:Button>
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="mgvEditButton" runat="server" CausesValidation="True" ValidationGroup="MappingGrid" CommandName="Edit" Text="Edit" Width="40%"></asp:Button>
<asp:Button ID="mgvDeleteButton" runat="server" CausesValidation="false" CommandName="Delete" Text="Delete" Width="40%"></asp:Button>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<FooterTemplate>
<asp:Button ID="mgvBtnAdd" runat="server" CommandName="Add" Text="Add Mapping" Width="90%" CausesValidation="true" ValidationGroup="MappingGrid" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<tr>
<td colspan="11" style="text-align:center;">
No mapping data was found for this section. Mappings can be added to this section by clicking the 'Add Mapping' Button.
</td>
</tr>
<tr>
<td></td>
<td><asp:TextBox ID="mgvInsertSectionID" runat="server" ReadOnly="true" Width="90%"></asp:TextBox></td>
<td><asp:TextBox ID="mgvInsertSchoologyCourseID" runat="server" Width="90%"></asp:TextBox>
<asp:RequiredFieldValidator ID="ReqFieldValSchoologyCourseIDEmpty" ControlToValidate="mgvInsertSchoologyCourseID" runat="server"
ErrorMessage="Required field." ValidationGroup="MappingGrid" Display="Dynamic"
CssClass="message-error">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegExpValSchoologyCourseIDEmpty" ControlToValidate="mgvInsertSchoologyCourseID" runat="server"
ErrorMessage="Enter only integers." ValidationGroup="MappingGrid" ValidationExpression="^\d+$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</td>
<td><asp:TextBox ID="mgvInsertCECityActivityID" runat="server" Width="90%"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegExpValCECityIDEmpty" ControlToValidate="mgvInsertCECityActivityID" runat="server"
ErrorMessage="Enter only 0-9, A-F, and hyphens; maximum length is 50." ValidationGroup="MappingGrid" ValidationExpression="^[0-9A-Fa-f-]{0,50}$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</td>
<td><asp:DropDownList ID="mgvInsertIsActive" runat="server" Width="90%">
<asp:ListItem Selected="True">Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:DropDownList></td>
<td><asp:TextBox ID="mgvInsertMaxEnrollment" runat="server" Width="90%"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegExpValMaxEnrollEmpty" ControlToValidate="mgvInsertMaxEnrollment" runat="server"
ErrorMessage="Enter only integers." ValidationGroup="MappingGrid" ValidationExpression="^\d+$"
Display="Dynamic" CssClass="message-error">
</asp:RegularExpressionValidator>
</td>
<td><asp:Button ID="mgvBtnAddEmpty" runat="server" CommandName="Add" Text="Add Mapping" Width="90%" CausesValidation="true"
ValidationGroup="MappingGrid" />
</td>
</tr>
</EmptyDataTemplate>
</asp:GridView>
This is my code behind: 这是我的代码背后:
protected void MappingGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
MappingGridView.EditIndex = e.NewEditIndex;
populateEnrollMappingGrid();
}
When I click the edit button, the RowEditing method is not called and the footer row displays the insert action. 当我单击编辑按钮时,未调用RowEditing方法,并且页脚行显示插入操作。
How do I get the Edit button to make the row clicked editable? 如何获得“编辑”按钮以使单击的行可编辑?
UPDATED INFORMATION Page_Load This is the Page_Load Method: 更新的信息Page_Load这是Page_Load方法:
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
//Get the Environment Setting to determine the database to access
txtBoxEnvironment.Text = CurrentEnvironment;
DAL.setCurrentEnvironment(CurrentEnvironment);
//Get All Section information from database
CSectionInfo.getData();
//Get All Mapping informatio from database
CEnrollMappingInfo.getData();
//Populate the Mapping grid
MappingGridView.DataSource = CEnrollMappingInfo.getInitialData();
//Populate the Drop Down Section List
populateDropDownSectionList(CEnrollMappingInfo.SelectedSectionID);
//Grid must be bound after the Dropdown List is set so the value in the footer can be determined
MappingGridView.DataBind();
//Populate the Section grid
SectionGridView.DataSource = CSectionInfo.getDataUsingSectionID(CEnrollMappingInfo.SelectedSectionID);
//Get Course information from database
populateCourseGrid();
//Populate the Drop Down Course List
populateDropDownCourseList(CSectionInfo.SelectedCourseID);
//Grid must be bound after the Dropdown List is set so the value in the footer can be determined
SectionGridView.DataBind();
}
}
catch (Exception ex)
{
logger.ErrorException(ex.Message, ex);
Response.Redirect("~/Error.aspx");
}
}
Thanks. 谢谢。
Just a guess, you are databinding the GridView
on every postback. 只是一个猜测,您是在每次回发中对
GridView
进行数据绑定。
You have a Page_Load
similar to: 您具有类似于以下内容的
Page_Load
:
protected void Page_Load(object sender, EventArgs e)
{
populateEnrollMappingGrid(); // a method where you assign the DataSource and call grid.DataBind()
}
Use the IsPostBack
property: 使用
IsPostBack
属性:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
populateEnrollMappingGrid();
}
I corrected the problem... 我更正了这个问题...
In my markup, I set the Edit Button CausesValidation=true and ValidationGroup-MappingGrid which is what the insert validation is in the footer. 在我的标记中,设置“编辑”按钮CausesValidation = true和ValidationGroup-MappingGrid,这是页脚中的插入验证。 To correct, I set the CausesValidation to false and removed the ValidationGroup.
为了更正,我将CausesValidation设置为false并删除了ValidationGroup。
I set these values in the Update Button field. 我在“更新按钮”字段中设置这些值。
Work now! 在工作,在忙!
Thanks. 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.