在DropDownList上的GridView中刷新数据选择了索引更改

时间:2015-03-03 12:01:13

标签: c# asp.net gridview

我的ASP网页上GridView1SqlDataSource1相关联,而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>

我需要在选择DropDownList1新值时显示新数据。

我想我需要写一些东西:

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

SqlDataSource1.Update();没有帮助。

DropDownList1_SelectedIndexChanged应该执行什么?

2 个答案:

答案 0 :(得分:1)

根据您AutoPostBack DropDownList1的问题True,根据您{{1}} {{1}}的问题,不需要编写任何代码,并且当所选值发生更改时,您的GridView将自动更新。

答案 1 :(得分:0)

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    GridView1.DataBind();
}