如何使编辑在gridview中正常工作 [英] How do I get the edit to work properly in a gridview
问题描述
谁能让我摆脱这个麻烦的嵌套Gridview ...
我有两个嵌套Gridview的问题
1.我必须单击编辑链接两次以使其触发
2.当它触发时,编辑字段为空而不是包含要编辑的文本
删除和ADD操作无问题地工作
这是嵌套gridview的aspx代码:
<% - 此网格包含所有子元素 - %>
< asp:TemplateField>
< ItemTemplate>
...< tr>
......< td colspan =100%>
......< div id =div<%#Eval(BulletGroupID)%> class =gv-bulletItems>
.........
.........< asp:GridView ID =gvBulletItemsrunat =server
..... ....... AllowPaging =TrueAllowSorting =trueBackColor =WhiteWidth =100%Font-Size =X-Small
......... ... AutoGenerateColumns =falseFont-Names =VerdanaDataKeyNames =BulletGroupIDShowFooter =true
............ BorderStyle =DoubleBorderColor =# 0083C1
............ OnPageIndexChanging =gvBulletItems_OnPageIndexChanging
............ OnRowUpdating =gvBulletItems_OnRowUpdating
.. .......... OnRowCommand =gvBulletItems_OnRowCommand
............ OnRowEditing =gvBulletItems_OnRowEditing
.......... .. OnRowCancelingEdit =gvBulletItems_OnRowCancelingEdit
............ OnRowDataBound =gvBulletItems_OnRowDataBound
............ OnRowDeleting =gvBulletItems_OnRowDeleting> ;
............
............< RowStyle BackColor =LightCyan/>
............< AlternatingRowStyle BackColor =White/>
............< HeaderStyle BackColor =#0083C1ForeColor =White/>
............< FooterStyle BackColor =PaleTurquoise/>
............< Columns>
...............< asp:TemplateField HeaderText =BulletGroup IDVisible =FalseSortExpression =BulletGroupID>
...............< ItemTemplate>
..................< asp:Label ID =lblBulletGroupIDText ='<%#Eval(BulletGroupID)%>'可见=Falserunat =server>< / asp:Label>
...............< / ItemTemplate>
...............< / asp:TemplateField>
...............< asp:TemplateField HeaderText =BulletGroup IDVisible =FalseSortExpression =BulletID>
...............< ItemTemplate>
..................< asp:Label ID =lblBulletIDText ='<%#Eval(BulletID)%>'可见=Falserunat =server>< / asp:Label>
...............< / ItemTemplate>
...............< / asp:TemplateField>
...............< asp:TemplateField HeaderText =List OrderSortExpression =ListOrder>
...............< ItemTemplate><%#Eval(ListOrder)%> < / ItemTemplate中>
...............< EditItemTemplate>
..................< asp:TextBox ID =txtBiListOrderText ='<%#Eval(ListOrder)%>'runat = 服务器 >< / ASP:文本框>
...............< / EditItemTemplate>
...............< FooterTemplate>
..................< asp:TextBox ID =txtBiListOrderText =runat =server>< / asp:TextBox>
...............< / FooterTemplate>
...............< / asp:TemplateField>
...............< asp:TemplateField HeaderText =Bullet TextSortExpression =DisplayText>
...............< ItemTemplate><%#Eval(DisplayText)%>< / ItemTemplate>
...............< EditItemTemplate>
..................< asp:TextBox ID =txtBiDisplayTextText ='<%#Eval(DisplayText)%>'runat = 服务器 >< / ASP:文本框>
...............< / EditItemTemplate>
...............< FooterTemplate>
..................< asp:TextBox ID =txtBiDisplayTextText =runat =server>< / asp:TextBox>
...............< / FooterTemplate>
...............< / asp:TemplateField>
...............< asp:CommandField HeaderText =EditShowEditButton =True/>
...............< asp:CommandField HeaderText =删除ShowDeleteButton =True/>
...............< asp:TemplateField>
...............< FooterTemplate>
..................< asp:LinkButton ID =linkAddBulletItemCommandName =AddBulletItemrunat =server>添加< / asp:LinkButton> ;
...............< / FooterTemplate>
...............< / asp:TemplateField>
............< / Columns>
.........< / asp:GridView>
......< / div>
......< / td>
...< / tr>
< / ItemTemplate>
< / asp:TemplateField>
FYI父网格视图没有任何这些问题
我尝试了什么:
单步执行代码,并排比较parent gridview
Quote:1。我必须单击编辑链接两次以使其触发
如果您绑定GridView
在Page_Load
事件中,然后确保在中包装您的代码以进行绑定!IsPostback
块:
protected void Page_Load( object sender,EventArgs e)
{
if (!Page.IsPostBack)
{
// 您在此处绑定GridView的代码
}
}
Quote:2。当它触发时,编辑字段为空白而不是包含要编辑的文本
删除和ADD操作无问题地工作
确保在设置RowEditing
重新填充GridView
> NewEditIndex 。例如:
protected void GridViewEmployee_RowEditing ( object sender,GridViewEditEventArgs e)
{
GridViewEmployee.EditIndex = e.NewEditIndex; // 转为编辑模式
// 使用数据源重新绑定GridView以在编辑模式下显示数据
}
Who will rid me of this troublesome nested Gridview...
I have two issues with the nested Gridview
1. I have to click the Edit Link twice to get it to fire
2. When it does fire the edit fields are blank instead of containing the text to be edited
the Delete and the ADD actions work without issue
Here is the aspx code for the nested gridview:
<%-- This grid holds all the child elements --%> <asp:TemplateField> <ItemTemplate> ... <tr> ......<td colspan="100%"> ...... <div id="div<%# Eval("BulletGroupID") %>" class="gv-bulletItems" > ......... ......... <asp:GridView ID="gvBulletItems" runat="server" ............ AllowPaging="True" AllowSorting="true" BackColor="White" Width="100%" Font-Size="X-Small" ............ AutoGenerateColumns="false" Font-Names="Verdana" DataKeyNames="BulletGroupID" ShowFooter="true" ............ BorderStyle="Double" BorderColor="#0083C1" ............ OnPageIndexChanging="gvBulletItems_OnPageIndexChanging" ............ OnRowUpdating="gvBulletItems_OnRowUpdating" ............ OnRowCommand="gvBulletItems_OnRowCommand" ............ OnRowEditing="gvBulletItems_OnRowEditing" ............ OnRowCancelingEdit="gvBulletItems_OnRowCancelingEdit" ............ OnRowDataBound="gvBulletItems_OnRowDataBound" ............ OnRowDeleting="gvBulletItems_OnRowDeleting"> ............ ............ <RowStyle BackColor="LightCyan" /> ............ <AlternatingRowStyle BackColor="White" /> ............ <HeaderStyle BackColor="#0083C1" ForeColor="White" /> ............ <FooterStyle BackColor="PaleTurquoise" /> ............ <Columns> ...............<asp:TemplateField HeaderText="BulletGroup ID" Visible="False" SortExpression="BulletGroupID"> ............... <ItemTemplate> .................. <asp:Label ID="lblBulletGroupID" Text='<%# Eval("BulletGroupID") %>' Visible="False" runat="server"></asp:Label> ............... </ItemTemplate> ...............</asp:TemplateField> ...............<asp:TemplateField HeaderText="BulletGroup ID" Visible="False" SortExpression="BulletID"> ............... <ItemTemplate> .................. <asp:Label ID="lblBulletID" Text='<%# Eval("BulletID") %>' Visible="False" runat="server"></asp:Label> ............... </ItemTemplate> ...............</asp:TemplateField> ...............<asp:TemplateField HeaderText="List Order" SortExpression="ListOrder"> ............... <ItemTemplate><%# Eval("ListOrder")%> </ItemTemplate> ............... <EditItemTemplate> .................. <asp:TextBox ID="txtBiListOrder" Text='<%# Eval("ListOrder")%>' runat="server"></asp:TextBox> ............... </EditItemTemplate> ............... <FooterTemplate> .................. <asp:TextBox ID="txtBiListOrder" Text="" runat="server"></asp:TextBox> ............... </FooterTemplate> ...............</asp:TemplateField> ...............<asp:TemplateField HeaderText="Bullet Text" SortExpression="DisplayText"> ............... <ItemTemplate><%# Eval("DisplayText")%></ItemTemplate> ............... <EditItemTemplate> .................. <asp:TextBox ID="txtBiDisplayText" Text='<%# Eval("DisplayText")%>' runat="server"></asp:TextBox> ............... </EditItemTemplate> ............... <FooterTemplate> .................. <asp:TextBox ID="txtBiDisplayText" Text="" runat="server"></asp:TextBox> ............... </FooterTemplate> ...............</asp:TemplateField> ...............<asp:CommandField HeaderText="Edit" ShowEditButton="True" /> ...............<asp:CommandField HeaderText="Delete" ShowDeleteButton="True"/> ...............<asp:TemplateField > ............... <FooterTemplate> .................. <asp:LinkButton ID="linkAddBulletItem" CommandName="AddBulletItem" runat="server">Add</asp:LinkButton> ............... </FooterTemplate> ...............</asp:TemplateField> ............ </Columns> ......... </asp:GridView> ...... </div> ......</td> ... </tr> </ItemTemplate> </asp:TemplateField>
FYI the parent gridview does not have any of these issues
What I have tried:
Stepping through the code, side by side comparision of the parent gridview
Quote:1. I have to click the Edit Link twice to get it to fire
If you are binding yourGridView
atPage_Load
event, then make sure to wrap your code for binding within!IsPostback
block:
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { //Your code for binding your GridView here } }
Quote:2. When it does fire the edit fields are blank instead of containing the text to be edited
the Delete and the ADD actions work without issue
Make sure that you are repopulating yourGridView
atRowEditing
right after you set theNewEditIndex
. For example:
protected void GridViewEmployee_RowEditing(object sender, GridViewEditEventArgs e) { GridViewEmployee.EditIndex = e.NewEditIndex; // turn to edit mode // Rebind your GridView with the datasource here to show the data in edit mode }
这篇关于如何使编辑在gridview中正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!