使用AJAX自动刷新ASP.NET

时间:2017-04-06 14:50:43

标签: asp.net ajax refresh

我想弄清楚如何使用AJAX让我的webform自动更新。在我的项目中,我有一个包含汽车模型的下拉列表。如果选择其中一个,并单击一个按钮,程序将使用从SQL语句接收的记录填充gridView。我想知道是否有任何方法可以自动刷新,如果是这样的话怎么样?这是我的asp.net代码(代码隐藏是c#)。

    <form id="form1" runat="server">
<div>
    <asp:GridView ID="gdvCars" runat="server" AutoGenerateColumns="False" DataSourceID="carConnection">
        <Columns>
            <asp:BoundField DataField="VIN" HeaderText="VIN" SortExpression="VIN" />
            <asp:BoundField DataField="Model" HeaderText="Model" SortExpression="Model" />
            <asp:BoundField DataField="Year" HeaderText="Year" SortExpression="Year" />
            <asp:BoundField DataField="Color" HeaderText="Color" SortExpression="Color" />
            <asp:BoundField DataField="MSRP" HeaderText="MSRP" SortExpression="MSRP" />
            <asp:BoundField DataField="Price_Sold" HeaderText="Price_Sold" SortExpression="Price_Sold" />
        </Columns>
    </asp:GridView>
    <asp:DropDownList ID="ddlTables" runat="server" OnSelectedIndexChanged="ddlTables_SelectedIndexChanged">
        <asp:ListItem>Select All</asp:ListItem>
        <asp:ListItem Value="SRXConnection">SRX</asp:ListItem>
        <asp:ListItem Value="CTSConnection">CTS</asp:ListItem>
        <asp:ListItem Value="CTSVConnection">CTS-V</asp:ListItem>
        <asp:ListItem Value="STSConnection">STS</asp:ListItem>
        <asp:ListItem Value="CruzeConnection">Cruze</asp:ListItem>



    </asp:DropDownList>



     <asp:SqlDataSource ID="carConnection" runat="server" ConnectionString="<%$ ConnectionStrings:newKroegedlConnectionString %>" SelectCommand="SELECT [VIN], [Model], [Year], [Color], [MSRP], [Price Sold] AS Price_Sold FROM [tCar]"></asp:SqlDataSource>
    <asp:SqlDataSource ID="SRXConnection" runat="server" ConnectionString="<%$ ConnectionStrings:newKroegedlConnectionString %>" SelectCommand="SELECT [VIN], [Model], [Color], [Year], [MSRP], [Price Sold] AS Price_Sold FROM [tCar] WHERE ([Model] = @Model)">
        <SelectParameters>
            <asp:Parameter DefaultValue="SRX" Name="Model" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="CTSConnection" runat="server" ConnectionString="<%$ ConnectionStrings:newKroegedlConnectionString %>" SelectCommand="SELECT [VIN], [Model], [Color], [Year], [MSRP], [Price Sold] AS Price_Sold FROM [tCar] WHERE ([Model] = @Model)">
        <SelectParameters>
            <asp:Parameter DefaultValue="CTS" Name="Model" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="CTSVConnection" runat="server" ConnectionString="<%$ ConnectionStrings:newKroegedlConnectionString %>" SelectCommand="SELECT [VIN], [Model], [Color], [Year], [MSRP], [Price Sold] AS Price_Sold FROM [tCar] WHERE ([Model] = @Model)">
        <SelectParameters>
            <asp:Parameter DefaultValue="CTS-V" Name="Model" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="STSConnection" runat="server" ConnectionString="<%$ ConnectionStrings:newKroegedlConnectionString %>" SelectCommand="SELECT [VIN], [Model], [Color], [Year], [Price Sold] AS Price_Sold, [MSRP] FROM [tCar] WHERE ([Model] = @Model)">
        <SelectParameters>
            <asp:Parameter DefaultValue="STS" Name="Model" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="CruzeConnection" runat="server" ConnectionString="<%$ ConnectionStrings:newKroegedlConnectionString %>" SelectCommand="SELECT [VIN], [Model], [Color], [Year], [MSRP], [Price Sold] AS Price_Sold FROM [tCar] WHERE ([Model] = @Model)">
        <SelectParameters>
            <asp:Parameter DefaultValue="Cruze" Name="Model" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>






    <asp:Button ID="btnChangeView" runat="server" Text="Button" OnClick="btnChangeView_Click" />




</div>
</form>

1 个答案:

答案 0 :(得分:-1)

您可以将gridview,下拉列表和按钮放在更新面板中。这将生成一个仅更新那些控件的回调。

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
               YOUR CONTROLS HERE
            </ContentTemplate>
        </asp:UpdatePanel>