รีเฟรชข้อมูลใน GridView บนการเปลี่ยนแปลงดัชนีที่เลือก DropDownList

ฉันมีหน้าเว็บ 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>

ฉันต้องแสดงข้อมูลใหม่เมื่อเลือก DropDownList1 ค่าใหม่

ฉันคิดว่าฉันต้องการบางสิ่งที่จะเขียนที่นั่น:

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

SqlDataSource1.Update();ไม่ช่วยอะไร

สิ่งที่ควรดำเนินการใน DropDownList1_SelectedIndexChanged ?


person vico    schedule 03.03.2015    source แหล่งที่มา
comment
GridView1.DataBind() หากคุณต้องการรีเฟรชกริด   -  person Ondrej Svejdar    schedule 03.03.2015
comment
DropDownList1 เชื่อมต่อกับพารามิเตอร์ควบคุมของ SqlDatasource1 และคุณสมบัติ GridView: <asp:GridView ... DataSourceID="SqlDataSource1" จะทำให้ gridview เชื่อมโยงข้อมูลในทุก postback ไม่ต้องใช้รหัสตามที่ Aria กล่าว   -  person fnostro    schedule 04.03.2015


คำตอบ (1)


ไม่จำเป็นต้องเขียนโค้ดใดๆ ตามคำถาม AutoPostBack ของ DropDownList1 ของคุณคือ True และเมื่อค่าที่เลือกมีการเปลี่ยนแปลง GridView ของคุณจะอัปเดตโดยอัตโนมัติ

person Aria    schedule 03.03.2015