C# Can';无法从GridView中的EditItemTemplate访问控件
尝试更新gridview时找不到所需的控件。控件是文本框和EditItemTemplate中的下拉列表。但是,如果我尝试在ItemTemplate中查找标签,它就可以正常工作。问题似乎是,在我有机会获得控件之前,它“跳出”了编辑模式 my gridview的标记:C# Can';无法从GridView中的EditItemTemplate访问控件,c#,asp.net,gridview,edititemtemplate,C#,Asp.net,Gridview,Edititemtemplate,尝试更新gridview时找不到所需的控件。控件是文本框和EditItemTemplate中的下拉列表。但是,如果我尝试在ItemTemplate中查找标签,它就可以正常工作。问题似乎是,在我有机会获得控件之前,它“跳出”了编辑模式 my gridview的标记: <asp:GridView ID="ProductGridView" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="
<asp:GridView ID="ProductGridView" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="Id" OnRowEditing="ProductGridView_RowEditing"
OnRowCancelingEdit="ProductGridView_RowCancelingEdit" OnRowUpdating="ProductGridView_RowUpdating"
OnRowDeleting="ProductGridView_RowDeleting" OnRowDataBound="ProductGridView_RowDataBound">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" CausesValidation="false" />
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity" SortExpression="Quantity">
<EditItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" Text='<%# Bind("Quantity") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblQuantity" runat="server" Text='<%# Eval("Quantity") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Family" SortExpression="Family.Name">
<EditItemTemplate>
<asp:DropDownList ID="ddlFamily" runat="server" OnInit="ddlFamily_Init">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblFamily" runat="server" Text='<%# Eval("Family.Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
尝试使用like代替commandfield
<asp:TemplateField HeaderText="Action" HeaderStyle-Width="20%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton ID="LnkManageTitle" runat="server" Text="Manage Title" CommandName="Edit"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LnkManageTitle" runat="server" Text="Save" CommandName="Update"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
不要忘记在
链接按钮上调用CommandName=“Update”
而不是commandfield,尝试使用like
<asp:TemplateField HeaderText="Action" HeaderStyle-Width="20%" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton ID="LnkManageTitle" runat="server" Text="Manage Title" CommandName="Edit"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LnkManageTitle" runat="server" Text="Save" CommandName="Update"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
别忘了在链接按钮上调用CommandName=“Update”
,也许吧
protected void ProductGridView_RowUpdating(object sender, GridViewUpdateEventArgs e){
Label lblName = (Label)ProductGridView.Rows[e.RowIndex].FindControl("lblName");
TextBox txtName = (TextBox)ProductGridView.Rows[e.RowIndex].FindControl("txtName"));
TextBox txtQuantity = (TextBox)ProductGridView.Rows[e.RowIndex].FindControl("txtQuantity");
DropDownList ddlFamily = (DropDownList)ProductGridView.Rows[e.RowIndex].FindControl("ddlFamily");
}
也许吧
谢谢你的快速回复。但是,我的代码中已经有了这一点。将添加到我的原始post.tname仍然为空。在EditItemTemplate中找不到任何控件。请在行编辑中设置gridview编辑索引。如下所示ProductGridView.EditIndex=e.Row.RowIndex;我将编辑索引设置为:
ProductGridView.EditIndex=e.NewEditIndex代码>。您提到的代码不可能,因为GridViewEditEventArgs不包含行属性。感谢您的快速回复。但是,我的代码中已经有了这一点。将添加到我的原始post.tname仍然为空。在EditItemTemplate中找不到任何控件。请在行编辑中设置gridview编辑索引。如下所示ProductGridView.EditIndex=e.Row.RowIndex;我将编辑索引设置为:ProductGridView.EditIndex=e.NewEditIndex代码>。您提到的代码是不可能的,因为GridViewEditEventArgs不包含Row属性。我尝试过稍微修改一下,每个模板中有两个链接按钮,而不是一个。在ItemTemplate中编辑和删除,在EditItemTemplate中更新和取消。单击“编辑”时,gridviewrow将进入编辑模式,但单击“更新”时,不会触发RowEditing。思想?删除一行并取消编辑工作正常。对于更新:您必须调用网格视图的RowUpdate事件。请参阅上面编辑的代码。这就是我的意思。我做了你想做的事,但还是不起作用。我的页面上出现了ViewStateMode=“Disabled”
,原因是我遇到了一个无法用其他方法解决的错误。你认为这与此有关吗?我解决了问题。我用上面的代码创建了一个新的web表单,效果很好!我尝试了一下,做了一点小小的改动,每个模板中有两个链接按钮,而不是一个。在ItemTemplate中编辑和删除,在EditItemTemplate中更新和取消。单击“编辑”时,gridviewrow将进入编辑模式,但单击“更新”时,不会触发RowEditing。思想?删除一行并取消编辑工作正常。对于更新:您必须调用网格视图的RowUpdate事件。请参阅上面编辑的代码。这就是我的意思。我做了你想做的事,但还是不起作用。我的页面上出现了ViewStateMode=“Disabled”
,原因是我遇到了一个无法用其他方法解决的错误。你认为这与此有关吗?我解决了问题。我用上面的代码创建了一个新的web表单,效果很好!
Protected Void Gridview1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
}
protected void ProductGridView_RowUpdating(object sender, GridViewUpdateEventArgs e){
Label lblName = (Label)ProductGridView.Rows[e.RowIndex].FindControl("lblName");
TextBox txtName = (TextBox)ProductGridView.Rows[e.RowIndex].FindControl("txtName"));
TextBox txtQuantity = (TextBox)ProductGridView.Rows[e.RowIndex].FindControl("txtQuantity");
DropDownList ddlFamily = (DropDownList)ProductGridView.Rows[e.RowIndex].FindControl("ddlFamily");
}