Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridView Edit Button Requires 2 Clicks

When pressing edit button in gridview, edit template is displayed only after 2 clicks. And another problem: Value of the field to edit is displayed in gridview initially, but not in edit template.

Asp code:

<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 code:

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
like image 564
phalanx Avatar asked Jan 13 '23 10:01

phalanx


1 Answers

I think you need to call DataBind to rebind the data source. You've commented it out, but it looks like you had it in a IsPostBack block, which would only execute on the initial page load.

Try:

Protected Sub EditRow(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
    GridView1.EditIndex = e.NewEditIndex
    GridView1.DataSource = SqlDataSource1
    GridView1.DataBind()        
End Sub
like image 79
mclark1129 Avatar answered Jan 14 '23 23:01

mclark1129