我的页面当前如下所示:

<!-- Datasource -->

<asp:SqlDataSource ID="SqlDataSource_GetURL" runat="server" ConnectionString="<%$ ConnectionStrings:TEST_DEV1 %>" SelectCommand="TEST_GetReports" SelectCommandType="StoredProcedure">
    <SelectParameters>

        <asp:Parameter DefaultValue="0" Name="CustomerID" Type="String" />
        <asp:Parameter DefaultValue="0" Name="UserId" Type="String" />

    </SelectParameters>
</asp:SqlDataSource>

<!-- Dropdown List -->

Company: <asp:DropDownList ID="ddlCompanyList"
runat="server" OnSelectedIndexChanged="ddlCompanyList_SelectedIndexChanged"
AutoPostBack="true" DataSourceID="SqlDataSource_GetURL"
DataValueField="RSReportLinkID" DataTextField="ReportName" CssClass="select">

</asp:DropDownList>

<!-- Report Viewer -->

 <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana" Width="930px" Height="1000" Font-Size="8pt" ProcessingMode="Remote" WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt">
        <ServerReport ReportPath="/Systems/Test/Report 1" ReportServerUrl="http://test/ReportServer" />
            </rsweb:ReportViewer>


其背后的代码是:

protected void ddlCompanyList_SelectedIndexChanged(object sender, EventArgs e)
{

    //TODO: change to SP
    string strConnectionString = ConfigurationManager.ConnectionStrings[Constants.ConnectionStringName].ConnectionString;
    string strCustomerID = CommonCode.GetCurrentCustomerID();
    string strUserID = CommonCode.GetCurrentUserID().ToString();
    using (SqlConnection connection = new SqlConnection(strConnectionString))
    {
        connection.Open();
        SqlCommand command = new SqlCommand("TEST_GetReportSettingsForReport", connection);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.AddWithValue("@CustomerID", strCustomerID);
        command.Parameters.AddWithValue("@UserId", strUserID);
        command.Parameters.AddWithValue("@RSReportLinkID", "5CCD639D-E930-4896-AE36-323EC3495D24");
;


        SqlDataReader reader = command.ExecuteReader();

        if (reader.HasRows)
        {
            while (reader.Read())
            {
                Response.Write(reader.GetValue(3));

            }
        }

        else
        {
           /* Console.WriteLine("No rows found.");*/
        }

        reader.Close();
        connection.Close();
    }


 }


当前@RSReportLinkID的硬编码为5CCD639D-E930-4896-AE36-323EC3495D24,因此,通过下拉列表选择报表时,ddlCompanyList_SelectedIndexChanged会很神奇,并由于以下这一行而从表的第四列返回ReportPath:

Response.Write(reader.GetValue(3));


但是,我的问题是,如何将ReportLinkID传递给@RSReportLinkID而不是对其进行硬编码?我的下拉列表中的选项是从存储过程中获取的,该存储过程从表中获取报告列表。每个报告的第一列中都有一个ReportLinkID,所以我希望做一些事情,例如...当从下拉列表中选择一个报告(即SelectedIndexChange)时,将从我的表中获取第一列(是ReportLinkID),然后将其传递到我的代码中并存储在@RSReportLinkID中。

感谢您的任何帮助或信息!我感觉这很简单,将涉及对象发送器和EventArgs e的使用,但是我很难将其包裹起来。

最佳答案

我认为您需要将RSReportLinkID存储为下拉列表的值。这样,在ddlCompanyList_SelectedIndexChanged方法中,您将可以像这样访问它:

protected void ddlCompanyList_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList ddl = sender as DropDownList;
    string RSReportLinkID = ddl.SelectedValue;
}

09-25 22:00