1

I have ASP webpage with GridView1 connected to SqlDataSource1, and DropDownList1 that makes influence on SqlDataSource1 SQL script.

<%@ 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.

I think I need something to be written there:

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

SqlDataSource1.Update(); not helps.

What should be executed in DropDownList1_SelectedIndexChanged ?

2
  • GridView1.DataBind() if you want to refresh the grid. Commented Mar 3, 2015 at 12:06
  • DropDownList1 is wired to SqlDatasource1's control parameter and the GridView property: <asp:GridView ... DataSourceID="SqlDataSource1" will cause the gridview to databind on every postback. No code required as Aria said Commented Mar 3, 2015 at 21:58

2 Answers 2

2

No there is no need write any code, based on your question AutoPostBack of your DropDownList1 is True and when the selected value is changed your GridView will updated automatically.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.