【问题标题】:How to update gridview through design code/ aspx page code如何通过设计代码/aspx页面代码更新gridview
【发布时间】:2015-12-23 08:01:32
【问题描述】:

每次更新、删除或插入任何记录时,我都会尝试更新 GridView。现在我在设计代码而不是表单代码中提供了GridView的sql数据源代码。现在我该如何从那里更新它?当我在表单代码上编写 GridView1.databind() 时,它说

> DataSource 和 DataSourceID 都在“GridView1”上定义。删除一个定义。

有人可以告诉我如何在设计视图中进行 DataBind,每次插入/更新/删除记录时更新 GridView 吗?

这里是 GridView1 代码

           <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateSelectButton="True" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Vertical" Width="284px">
                    <AlternatingRowStyle BackColor="#CCCCCC" />
                    <Columns>
                        <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
                        <asp:BoundField DataField="surname" HeaderText="surname" SortExpression="surname" />
                        <asp:BoundField DataField="amount" HeaderText="amount" SortExpression="amount" />
                    </Columns>
                    <FooterStyle BackColor="#CCCCCC" />
                    <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                    <SortedAscendingCellStyle BackColor="#F1F1F1" />
                    <SortedAscendingHeaderStyle BackColor="#808080" />
                    <SortedDescendingCellStyle BackColor="#CAC9C9" />
                    <SortedDescendingHeaderStyle BackColor="#383838" />
                </asp:GridView>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myDbConnectionString %>" SelectCommand="SELECT [name], [surname], [amount] FROM [Table1]"></asp:SqlDataSource>

【问题讨论】:

  • 简单地重新绑定你的gridview
  • 我在表单代码中绑定了它,但它说“DataSource 和 DataSourceID 都在 'GridView1' 上定义。删除一个定义。”如何在表单的设计视图中绑定它?
  • 显示你的 .cs 页面 /code

标签: asp.net webforms sql-server-2012


【解决方案1】:

将 DataSourceID 设置为 null。

GridView1.DataSourceID = null;
GridView1.DataSource = dt;
GridView1.DataBind();

【讨论】:

    【解决方案2】:

    出现该错误是因为设计视图代码和 Web 表单后端代码都绑定了数据。 我只是添加了 GridView1.DataSourceID = null;

    这样就完成了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 2010-09-16
      • 1970-01-01
      • 1970-01-01
      • 2010-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多