如何使用更新 DropDownList 的传递参数值自动更新 SqlDataSource,以便它在 "Page_Load" 期间显示

How can I automatically update SqlDataSource, with a passed parameter value that updates a DropDownList, so that it displays during "Page_Load"

用户希望在首次显示 ASPX 页面时自动显示在 MVC 页面上选择的设备的数据。我 运行 遇到的问题是我在 Page_Load 上显示 DropDownList 值和列标题,但是,图表和 GridView 数据没有显示请求的数据。

我通过 MVC 页面上的 ActionLink 单击将设备参数传递到 ASPX 页面,并将值设置到 !IsPostBack 中的 DropDownList.SelectedValue (1)。 DropDownList 值用作列标题,但它也在多个 SqlDataSource SelectCommand 中用作输入 ControlParameter。这些查询的结果用于创建折线图和 GridView table,但它们不显示数据。

ASPX 页面也是一个独立的页面,在没有初始参数的情况下可以正常工作。带一个参数,最终会在PostBack事件发生时显示数据;例如数据刷新计时器启动时。由于 SqlDataSource 命令 return 在 PostBack 上请求的数据,我相信我需要在初始 Page_Load 期间以某种方式使所有 SqlDataSource 到 "runat" 服务器。 (只是 DataBind'ing 当前的 SqlDataSource 并没有削减它。)

我还没有找到如何做到这一点。有人可以指点我 link 解释如何这样做,向他们自己解释如何做,或者告诉我 what/where "correct" 解决方案是(如果我完全没有根据)?

这是我目前的情况:

MVC ActionLink 到具有设备参数的 ASPX 页面:

<div style="max-height:335px;overflow:auto;">
    <table class="table table-bordered table-condensed table-striped">

        @foreach (var item in Model)
        {
        <tr>
            <td>
                @*@Html.DisplayFor(modelItem => item.CIRCUIT)*@
                @Html.ActionLink(item.CIRCUIT, null, "AmpLoadDaily", new { device = item.CIRCUIT }, null)
            </td>

ASPX Timer 和 DropDownList 一:

<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>

        <asp:Timer ID="Timer1" runat="server" Interval="90000" OnTick="Timer1_Tick">
        </asp:Timer>

        <asp:DropDownList runat="server" ID="ddlDevice1"  DataSourceID ="SqlDataSource_ddlDevice1" DataTextField="CIRCUIT" DataValueField="CIRCUIT" 
                          AppendDataBoundItems="true" AutoPostBack="true" Width="95" OnSelectedIndexChanged="ddlDevice1_SelectedIndexChanged">
            <asp:ListItem Text="-Device1-" Value=" " Selected="True">
            </asp:ListItem>
        </asp:DropDownList>

ASPX 图表

        <div class="auto-style1">
            <span class="auto-style4">
                <strong>Amp Load - Daily

                    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                        <Triggers>
                            <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
                        </Triggers>

                        <ContentTemplate>
                            <asp:Chart ID="Chart1" runat="server" Width="900px" Height="500px">
                                <Series>
                                    <asp:Series ChartType="Spline" Name="ALL IA" XValueMember="Times" YValueMembers="ALL IA" Color="Red" BORDERWidth="2">
                                    </asp:Series>

                                    <asp:Series ChartType="Spline" Name="ALL IB" XValueMember="Times" YValueMembers="ALL IB" Color="Green" BORDERWidth="2">
                                    </asp:Series>

                                    <asp:Series ChartType="Spline" Name="ALL IC" XValueMember="Times" YValueMembers="ALL IC" Color="Blue" BORDERWidth="2">
                                    </asp:Series>

                                </Series>

                                <ChartAreas>
                                    <asp:ChartArea Name="ChartArea1">
                                        <AxisY Enabled="True" Interval="10" Title="AMPS" TitleFont="Times New Roman, 14.25pt" LineColor="RoyalBlue" TitleForeColor="#0033CC">
                                            <MajorGrid Interval="10" />
                                            <LabelStyle ForeColor="Blue" />
                                        </AxisY>

                                        <AxisX Interval="12" IsLabelAutoFit="False" Enabled="TRUE" IntervalOffset="1">
                                            <MajorGrid Interval="4" IntervalOffset="1" />
                                            <LabelStyle IntervalOffset="1" />
                                        </AxisX>
                                    </asp:ChartArea>
                                </ChartAreas>

                                <Legends>
                                    <asp:Legend Alignment="Center" BackImageAlignment="TopRight" Docking="Bottom" Name="Legend1"></asp:Legend>
                                </Legends>
                            </asp:Chart>
                        </ContentTemplate>
                    </asp:UpdatePanel>
                </strong>
            </span>

ASPX 列标题:

                    <td class="auto-style6">
                        <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                            <Triggers>
                                <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
                            </Triggers>
                            <ContentTemplate>
                                <%= ddlDevice1.SelectedValue %>
                            </ContentTemplate>
                        </asp:UpdatePanel>
                    </td>

ASPX GridView一:

                    <td>
                        <asp:UpdatePanel ID="UpdatePanel32" runat="server">
                            <Triggers>
                                <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
                            </Triggers>
                            <ContentTemplate>
                                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" GridLines="None" ShowHeader="False" CssClass="auto-style7" >
                                    <Columns>
                                        <asp:BoundField DataField="Value" HeaderText="Value" ReadOnly="True" SortExpression="Value" ItemStyle-ForeColor="Red"/>
                                    </Columns>
                                </asp:GridView>
                            </ContentTemplate>
                        </asp:UpdatePanel>
                    </td>

ASPX SqlDataSource 示例(图表):

       <asp:SqlDataSource ID="SqlDataSource_Chart" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"
            SelectCommand="SELECT FORMAT(CONVERT(DATETIME,T.[Time_values]), 'hh:mm tt') AS [Times],
                                  SUM(C.[IA]) AS [ALL IA],
                                  SUM(C.[IB]) AS [ALL IB],
                                  SUM(C.[IC]) AS [ALL IC]
                           FROM [SCADA].[dbo].[TIMEVALUES] T
                           LEFT OUTER JOIN
                               (SELECT FORMAT([RUNTIME],'hh:mm tt') AS [TIME],
                                       [IA],
                                       [IB],
                                       [IC]
                                FROM  [CIRCUITS1DAY] 
                                WHERE FORMAT([RUNTIME], 'yyyy/MM/dd') = FORMAT(GETDATE(), 'yyyy/MM/dd') AND
                                     ([CIRCUIT]=@ddlDevice1  OR [CIRCUIT]=@ddlDevice2  OR [CIRCUIT]=@ddlDevice3  OR [CIRCUIT]=@ddlDevice4  OR
                                      [CIRCUIT]=@ddlDevice5  OR [CIRCUIT]=@ddlDevice6  OR [CIRCUIT]=@ddlDevice7  OR [CIRCUIT]=@ddlDevice8  OR
                                      [CIRCUIT]=@ddlDevice9  OR [CIRCUIT]=@ddlDevice10 OR [CIRCUIT]=@ddlDevice11 OR [CIRCUIT]=@ddlDevice12 OR 
                                      [CIRCUIT]=@ddlDevice13 OR [CIRCUIT]=@ddlDevice14 OR [CIRCUIT]=@ddlDevice15 OR [CIRCUIT]=@ddlDevice16 OR 
                                      [CIRCUIT]=@ddlDevice17 OR [CIRCUIT]=@ddlDevice18 OR [CIRCUIT]=@ddlDevice19 OR [CIRCUIT]=@ddlDevice20 OR 
                                      [CIRCUIT]=@ddlDevice21 OR [CIRCUIT]=@ddlDevice22 OR [CIRCUIT]=@ddlDevice23 OR [CIRCUIT]=@ddlDevice24 OR 
                                      [CIRCUIT]=@ddlDevice25 OR [CIRCUIT]=@ddlDevice26 OR [CIRCUIT]=@ddlDevice27 OR [CIRCUIT]=@ddlDevice28 OR 
                                      [CIRCUIT]=@ddlDevice29 OR [CIRCUIT]=@ddlDevice30)) C
                           ON
                               FORMAT(CONVERT(DATETIME,T.[Time_values]), 'hh:mm tt') = C.[TIME]
                           GROUP BY CONVERT(DATETIME,T.[Time_values])">
            <SelectParameters>
                <asp:ControlParameter ControlID="ddlDevice1"  PropertyName="SelectedValue" Name="ddlDevice1"  Type="String" />
                <asp:ControlParameter ControlID="ddlDevice2"  PropertyName="SelectedValue" Name="ddlDevice2"  Type="String" />
                <asp:ControlParameter ControlID="ddlDevice3"  PropertyName="SelectedValue" Name="ddlDevice3"  Type="String" />
                <asp:ControlParameter ControlID="ddlDevice4"  PropertyName="SelectedValue" Name="ddlDevice4"  Type="String" />
                <asp:ControlParameter ControlID="ddlDevice5"  PropertyName="SelectedValue" Name="ddlDevice5"  Type="String" />
                <asp:ControlParameter ControlID="ddlDevice6"  PropertyName="SelectedValue" Name="ddlDevice6"  Type="String" />
                <asp:ControlParameter ControlID="ddlDevice7"  PropertyName="SelectedValue" Name="ddlDevice7"  Type="String" />
                <asp:ControlParameter ControlID="ddlDevice8"  PropertyName="SelectedValue" Name="ddlDevice8"  Type="String" />
                <asp:ControlParameter ControlID="ddlDevice9"  PropertyName="SelectedValue" Name="ddlDevice9"  Type="String" />
                <asp:ControlParameter ControlID="ddlDevice10" PropertyName="SelectedValue" Name="ddlDevice10" Type="String" />
                <asp:ControlParameter ControlID="ddlDevice11" PropertyName="SelectedValue" Name="ddlDevice11" Type="String" />
                <asp:ControlParameter ControlID="ddlDevice12" PropertyName="SelectedValue" Name="ddlDevice12" Type="String" />
                <asp:ControlParameter ControlID="ddlDevice13" PropertyName="SelectedValue" Name="ddlDevice13" Type="String" />
                <asp:ControlParameter ControlID="ddlDevice14" PropertyName="SelectedValue" Name="ddlDevice14" Type="String" />
                <asp:ControlParameter ControlID="ddlDevice15" PropertyName="SelectedValue" Name="ddlDevice15" Type="String" />
                <asp:ControlParameter ControlID="ddlDevice16" PropertyName="SelectedValue" Name="ddlDevice16" Type="String" />
                <asp:ControlParameter ControlID="ddlDevice17" PropertyName="SelectedValue" Name="ddlDevice17" Type="String" />
                <asp:ControlParameter ControlID="ddlDevice18" PropertyName="SelectedValue" Name="ddlDevice18" Type="String" />
                <asp:ControlParameter ControlID="ddlDevice19" PropertyName="SelectedValue" Name="ddlDevice19" Type="String" />
                <asp:ControlParameter ControlID="ddlDevice20" PropertyName="SelectedValue" Name="ddlDevice20" Type="String" />
                <asp:ControlParameter ControlID="ddlDevice21" PropertyName="SelectedValue" Name="ddlDevice21" Type="String" />
                <asp:ControlParameter ControlID="ddlDevice22" PropertyName="SelectedValue" Name="ddlDevice22" Type="String" />
                <asp:ControlParameter ControlID="ddlDevice23" PropertyName="SelectedValue" Name="ddlDevice23" Type="String" />
                <asp:ControlParameter ControlID="ddlDevice24" PropertyName="SelectedValue" Name="ddlDevice24" Type="String" />
                <asp:ControlParameter ControlID="ddlDevice25" PropertyName="SelectedValue" Name="ddlDevice25" Type="String" />
                <asp:ControlParameter ControlID="ddlDevice26" PropertyName="SelectedValue" Name="ddlDevice26" Type="String" />
                <asp:ControlParameter ControlID="ddlDevice27" PropertyName="SelectedValue" Name="ddlDevice27" Type="String" />
                <asp:ControlParameter ControlID="ddlDevice28" PropertyName="SelectedValue" Name="ddlDevice28" Type="String" />
                <asp:ControlParameter ControlID="ddlDevice29" PropertyName="SelectedValue" Name="ddlDevice29" Type="String" />
                <asp:ControlParameter ControlID="ddlDevice30" PropertyName="SelectedValue" Name="ddlDevice30" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>

ASPX 代码隐藏 Page_Load:

namespace Mvc
{
    public partial class AmpLoadDaily : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlDevice1.SelectedValue = Request.QueryString["device"];

                Chart1.DataSource = SqlDataSource_Chart;
                Chart1.DataBind();
                GridView1.DataSource = SqlDataSource_grdTotal_IA;
                GridView1.DataBind();

Timer 方法只是 re-DataBind'ing,与 !IsPostBack 相同。

预期结果是让用户的结果自动显示在 "Page_Load" 上,而不是让用户引起 PostBack 或必须等待屏幕的计时器刷新。

添加到 ASPX 页面的以下 __doPostBack 功能似乎可以在 "PageLoad" 期间显示图表和网格数据。 (它实际上可能不会在页面加载期间导致回发,但所选数据在页面首次显示之前已被处理。)

<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>

        <script type="text/javascript">  
            if ('False' === '<%= Page.IsPostBack.ToString()%>') 
            {
                __doPostBack();
            }
        </script>  

        <asp:Timer ID="Timer1" runat="server" Interval="90000" OnTick="Timer1_Tick">
        </asp:Timer>

        <asp:DropDownList runat="server" ID="ddlDevice1"  DataSourceID ="SqlDataSource_ddlDevice1" DataTextField="CIRCUIT" DataValueField="CIRCUIT" 
                          AppendDataBoundItems="true" AutoPostBack="true" Width="95" OnSelectedIndexChanged="ddlDevice1_SelectedIndexChanged">
            <asp:ListItem Text="-Device1-" Value=" " Selected="True">
            </asp:ListItem>
        </asp:DropDownList>