4

在gridview中按下编辑按钮时,编辑模板只有在点击2次后才会显示。还有另一个问题:要编辑的字段的值最初显示在 gridview 中,但不在编辑模板中。

ASP代码:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" 
AutoGenerateColumns="False" CellPadding="4" OnRowEditing="EditRow" 
OnRowCancelingEdit="CancelEditRow" DataKeyNames="AREA" DataMember="DefaultView">

  <Columns>
     <asp:BoundField DataField="AREA" HeaderText="AREA" ReadOnly="True" 
                                        SortExpression="AREA" />                                   

      <asp:TemplateField HeaderText="LEADER_USER" SortExpression="LEADER_USER">
                     <ItemTemplate><%#Eval("leader_user")%></ItemTemplate>
                      <EditItemTemplate>
                          <asp:TextBox ID="txtleaderuser" runat="server" Text='<%#Eval("leader_user")%>'/>
                      </EditItemTemplate>
       </asp:TemplateField>

       <asp:TemplateField>                                    
           <ItemTemplate>
                  <asp:ImageButton ID="editButton" runat="server" CommandName="Edit" 
                                                ImageUrl="images/pencil1.png" Text="Edit" ToolTip="Edit" />
            </ItemTemplate>
             <EditItemTemplate>
                   <asp:Button ID="BtnUpdate" runat="server" CommandName="Update" 
                                                Text="Update" />
                   <asp:Button ID="BtnCancel" runat="server" CommandName="Cancel" 
                                                Text="Cancel" />
              </EditItemTemplate>
             </asp:TemplateField>

      </Columns>
    </asp:GridView>

VB代码:

Protected Sub EditRow(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
        GridView1.EditIndex = e.NewEditIndex
        GridView1.DataSource = SqlDataSource1
        'If Not IsPostBack Then
        'GridView1.DataSourceID = SqlDataSource1.ID
        'GridView1.DataBind()
        'End If
 End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        SqlDataSource1.SelectCommand = "SQL"
        SqlDataSource1.ConnectionString = "My conn string"

        If Not IsPostBack Then
            'GridView1.DataSourceID = SqlDataSource1.ID
            GridView1.DataSource = SqlDataSource1
            GridView1.DataBind()
        End If    


    End Sub
4

1 回答 1

9

我认为您需要调用DataBind以重新绑定数据源。您已将其注释掉,但看起来您将它放在一个IsPostBack块中,它只会在初始页面加载时执行。

尝试:

Protected Sub EditRow(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
    GridView1.EditIndex = e.NewEditIndex
    GridView1.DataSource = SqlDataSource1
    GridView1.DataBind()        
End Sub
于 2013-09-10T16:42:19.620 回答