1

我有一个带有编辑和删除按钮的gridview:

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

还有一个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>

如果我使用 DatasourceID(gridview property DataSourceID="SqlDataSource1") 链接它们,两个按钮都可以工作,但我尝试使用它们后面的 vb 代码来做它不会(当我按下按钮时没有任何反应):

Protected Sub Page_Load(...

GridView1.DataSource = SqlDataSource1
GridView1.DataBind()

我需要使用后面的代码来制作它。如何让它们工作?

4

2 回答 2

1

尝试使用该DataSourceID属性并将其指向数据源的 ID,如下所示:

GridView1.DataSourceID = SqlDataSource1.ID
GridView1.DataBind()
于 2013-09-09T19:27:41.520 回答
1

您需要将它们包装在 If Not IsPostBack 中:

Protected Sub Page_Load(...
    If Not IsPostBack Then
        GridView1.DataSource = SqlDataSource1
        GridView1.DataBind()
    EndIf
于 2013-09-09T21:02:54.077 回答