1

I have a gridview with edit and delete buttons:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" style= "-moz-border-radius: 15px;border-radius: 15px;"
                            AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
                            DataKeyNames="AREA" DataMember="DefaultView" 
                            ForeColor="#333333" Height="90%" Width="90%">
                            <RowStyle BackColor="#EFF3FB" />
                            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                            <EditRowStyle BackColor="#2461BF" />
                            <AlternatingRowStyle BackColor="White" />
  <Columns>
                                <asp:BoundField DataField="AREA" HeaderText="AREA" ReadOnly="True" 
                                    SortExpression="AREA" />
                                <asp:BoundField DataField="LIDER_USUARIO" HeaderText="LIDER_USUARIO" 
                                    SortExpression="LIDER_USUARIO" />
    <asp:TemplateField>
        <ItemTemplate>
            <asp:ImageButton ID="editButton" runat="server" CommandName="Edit" 
                ImageUrl="images/pencil1.png" Text="Edit"  />
        </ItemTemplate>
        <EditItemTemplate>
            <asp:Button ID="BtnUpdate" runat="server" CommandName="Update" 
                Text="Edit" />
            <asp:Button ID="BtnCancel" runat="server" CommandName="Cancel" 
                Text="Cancel" />
        </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:ImageButton ID="deleteButton" runat="server" CommandName="Delete" 
                ImageUrl="images/DeleteRed1.png" 
                OnClientClick="return confirm('Are u sure?');" Text="Delete" />
        </ItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>

And a sqldatasource:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:DBUserInterfaceConnectionString %>" 
    DeleteCommand="DELETE FROM [TABLE] WHERE ...;" 
    SelectCommand="SELECT * FROM [TABLE]" 
    UpdateCommand="UPDATE TABLE SET ...;">
</asp:SqlDataSource>

If I link them using DatasourceID(gridview property DataSourceID="SqlDataSource1") both buttons work, but I try to do it using vb code behind they DON'T(when I press buttons nothing happens):

Protected Sub Page_Load(...

GridView1.DataSource = SqlDataSource1
GridView1.DataBind()

I need to make it using code behind. How to make them work?

2 Answers 2

1

Try using the DataSourceID property and point it to the ID of the data source, like this:

GridView1.DataSourceID = SqlDataSource1.ID
GridView1.DataBind()
Sign up to request clarification or add additional context in comments.

1 Comment

EDIT: Sorry, It works. I had forgotten to set server validation to true in <%@ Page>. Thanks.
1

You need to wrap them in If Not IsPostBack:

Protected Sub Page_Load(...
    If Not IsPostBack Then
        GridView1.DataSource = SqlDataSource1
        GridView1.DataBind()
    EndIf

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.