Search code examples
c#asp.netajaxcontroltoolkit

ASP.NET Gridview inside UpdatePanel not visible after declarative data bind


I've created a page that has a dropdownlist used to select a study. The StudyID is then used to return results from a stored procedure and I have tried to data bind the results to a gridview. However, the gridview does not render on page load. All controls are wrapped inside an AJAX Control Toolkit UpdatePanel.

Here's what I've written to obtain the results from the stored procedure:

public void RunResponses(int iStudyID)
{
    DataTable dt = new DataTable();
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQL"].ConnectionString);

    iStudyID = Convert.ToInt32(
        string.IsNullOrWhiteSpace(ddlStudies.SelectedValue) ? null : ddlStudies.SelectedValue);

    try
    {
        using (conn)
        {
            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            command.CommandText = "spGetAnalysisReport";
            command.CommandType = CommandType.StoredProcedure;

            SqlParameter param = new SqlParameter();
            param.ParameterName = "@StudyID";
            param.SqlDbType = SqlDbType.Int;
            param.Direction = ParameterDirection.Input;
            param.Value = iStudyID;

            command.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(command.CommandText, conn);
            DataSet ds = new DataSet();

            conn.Open();
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    adapter.Fill(ds);
                    dt = ds.Tables["Responses"];
                    return;
                }

                gvStudyResponseData.DataSource = dt;
                gvStudyResponseData.DataBind();
            }
            else
            {
                gvStudyResponseData = null;
                gvStudyResponseData.DataBind();
            }
        }
    }
    catch (Exception e)
    {
        Debug.WriteLine(e.ToString());

    }
    finally
    {
        conn.Close();
        dt.Dispose();
    }
}

My Page_Load is as follows:

protected void Page_Load(object sender, EventArgs e)
{
    int iUserID = 0;

    if (Session["UserID"] == null)
    {
        Response.Redirect("Default.aspx");
    }
    iUserID = Convert.ToInt32(Session["UserID"]);
    int iRole = 0;
    iRole = Convert.ToInt32(Session["RoleID"]);

    int iStudyID = Convert.ToInt32(string.IsNullOrWhiteSpace(ddlStudies.SelectedValue) ? null : ddlStudies.SelectedValue);

    if (!Page.IsPostBack)
    {
        DataTable dt = new DataTable();
        using (PROTOTYPING db = new PROTOTYPING(ConfigurationManager.ConnectionStrings["SQL"].ConnectionString))
        {
            var query =
            (from d in db.Studies
                where d.StudyStatus == 0 //Closed...
                orderby d.StudyName
                select new
                {
                    d.StudyName,
                    d.StudyID,
                });
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    ddlStudies.Items.Add(new ListItem(row["StudyID"].ToString(), row["StudyName"].ToString()));
                }
            }

            ddlStudies.DataSource = query;
            ddlStudies.DataBind();
        }

        GetResponses(iStudyID, ddlStudies);
        RunResponses(iStudyID);
    }
}

My page layout is as follows:

<asp:UpdatePanel ID="UpdatePanelPromo" runat="server">
    <ContentTemplate>
        <div style="position: fixed; left: 50%; height: 40px; width: 150px; z-index: 10000;">
            <asp:UpdateProgress ID="UpdateProgressPromo" runat="server" DisplayAfter="1" AssociatedUpdatePanelID="UpdatePanelPromo">
                <ProgressTemplate>
                    <table style="border: medium solid #000080; width: 100%; background-color: white;">
                        <tr>
                            <td align="right" style="width: 12px;">
                                <img src="Images/indicator_big.gif" alt="" />
                            </td>
                            <td><span style="color: #fff"><span style="font-size: 8pt; color: #0000cc; font-family: Verdana"><strong style="font-family: arial, Helvetica, sans-serif; font-size: 10pt; color: navy; font-weight: bold">processing  <span class="">....</span> </strong></span></span></td>
                        </tr>
                    </table>
                </ProgressTemplate>
            </asp:UpdateProgress>
        </div>
        <div id="Studies" class="col-md-3" style="padding-top: 15px;">
            <div class="row">
                <asp:Label runat="server" ID="lblSelectStudy" Text="Please Select A Closed Study to Analyze:"/>
                <asp:DropDownList id="ddlStudies" runat="server" DataValueField="StudyID" DataTextField="StudyName">

                </asp:DropDownList>

            </div>
            <div class="row">
                <asp:Label runat="server" ID="lblSelectedStat" Text="Please Select An Analysis to Run on the Study Selected Above:"/><br/>
                <asp:ListBox id="lbStatsToRun" runat="server" AutoPostBack="True">

                    <asp:ListItem>Regression Peak</asp:ListItem>

                </asp:ListBox>

            </div>
            <div class ="row">
                <asp:Button runat="server" id="btnRunStats" Text="Run Selected Analysis"/>
            </div>
            <div class="row">
                <asp:Label runat="server" id="lblError"/>
                <asp:Label runat="server" id="lblResult" Text="Result:"/>

            </div>
            <div class="row">
                <asp:GridView runat="server" id="gvStudyResponseData" AutoGenerateData="True" EnableViewState="True">
                    <Columns>

                    </Columns>
                </asp:GridView>
            </div>
        </div>
    </ContentTemplate>     
</asp:UpdatePanel>

How do I ensure that the gridview is rendered on page load?


Solution

  • You've got this before doing anything with the data/sp:

    if (dt != null && dt.Rows.Count > 0)
    

    the row count at this point will always be 0 and the block will not run.