【问题标题】:Refresh data in GridView on DropDownList selected index change在 DropDownList 选定的索引更改上刷新 GridView 中的数据
【发布时间】:2015-03-03 12:01:13
【问题描述】:

我有一个 ASP 网页,其中 GridView1 连接到 SqlDataSource1,而 DropDownList1 会影响 SqlDataSource1 SQL 脚本。

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="terminalLog.aspx.cs" Inherits="_2013web.terminalLog" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2" DataTextField="Id" DataValueField="Id" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True" Height="95px" OnLoad="DropDownList1_Load" OnTextChanged="DropDownList1_TextChanged" Width="481px">
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:logsConnectionString1 %>" SelectCommand="SELECT [ClientID], [Msg], [LogLevel], [Date] FROM [logs] WHERE ([ClientID] = @ClientID) ORDER BY [Date]">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownList1" DefaultValue="80" Name="ClientID" PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Width="2104px">
        <Columns>
            <asp:BoundField DataField="ClientID" HeaderText="ClientID" SortExpression="ClientID" />
            <asp:BoundField DataField="Msg" HeaderText="Msg" SortExpression="Msg" />
            <asp:BoundField DataField="LogLevel" HeaderText="LogLevel" SortExpression="LogLevel" />
            <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:logsConnectionString1 %>" SelectCommand="SELECT [Id] FROM [clients] ORDER BY [Id]"></asp:SqlDataSource>
</asp:Content>

I need to show new data when DropDownList1 new value is selected.

我想我需要在那里写一些东西:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    SqlDataSource1.Update();
}

SqlDataSource1.Update(); 没有帮助。

DropDownList1_SelectedIndexChanged应该执行什么?

【问题讨论】:

  • GridView1.DataBind() 如果你想刷新网格。
  • DropDownList1 连接到SqlDatasource1 的控制参数和GridView 属性:&lt;asp:GridView ... DataSourceID="SqlDataSource1" 将导致gridview 在每次回发时进行数据绑定。正如 Aria 所说,不需要代码

标签: c# asp.net gridview


【解决方案1】:

否无需写入任何代码,基于您的问题AutoPostBack 987654322 @是True和当所选值更改时,GridView将自动更新。

【讨论】:

    【解决方案2】:
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridView1.DataBind();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      相关资源
      最近更新 更多