asp.netparameter-passingsqldatasourceaspxgridviewpage-load-time

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


The user is wanting the data for a device selected on an MVC page to automatically be displayed when the ASPX page is first displayed. The problem that I'm running into is that I'm getting the DropDownList value and column heading displaying on Page_Load, however, the Chart and GridView data are not displaying the requested data.

I am passing the device parameter through an ActionLink click on the MVC page to the ASPX page and setting the value into the DropDownList.SelectedValue (1) in !IsPostBack. The DropDownList value is used as the column heading, but it is also used in multiple SqlDataSource SelectCommands as an input ControlParameter. The results of these queries are used for the creation of the line Chart and GridView table, but they are not displaying the data.

The ASPX page is also a standalone page that works fine when there isn't an initial parameter. With a parameter, it will eventually display the data when a PostBack event occurs; such as when the data refresh Timer kicks off. Since the SqlDataSource commands return the data requested on a PostBack, I believe that I need to somehow cause all of the SqlDataSource's to "runat" the server during the initial Page_Load. (Just DataBind'ing the current SqlDataSource doesn't cut it.)

I haven't been able to find how to do this as of yet. Could someone either point me to a link explaining how to do so, explain how to themselves, or tell me what/where the "correct" solution is, (if I'm totally off base)?

Here is what I have so far:

MVC ActionLink to the ASPX page with the device parameter:

<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 and DropDownList one:

<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 Chart

        <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 Column heading:

                    <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 one:

                    <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 example (Chart):

       <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 Code Behind 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();

The Timer method is just re-DataBind'ing, same as !IsPostBack.

Expected results are to have the user's results displayed automatically on the "Page_Load", not by having the user cause a PostBack or have to wait for the Timer refresh of the screen.


Solution

  • The following __doPostBack function added to the ASPX page seems to be working for displaying the Chart and Grid data during "PageLoad". (It may not actually be causing a PostBack during the PageLoad, but the selected data is processed before the page is first shown.)

    <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>