Asp:由dropdownlistbox确定的标签工作不正常

本文关键字:标签 工作 不正常 dropdownlistbox Asp | 更新日期: 2023-09-27 17:58:30

前几天还在工作,不确定发生了什么,但现在不是,我想不通。标签只会在大约20个不同的项目中给我一个项目。无论我在下拉列表中选择什么,它总是在标签上给我相同的值。

HTML:

                    <td>
                    <asp:DropDownList ID="ddlCompanyCode" runat="server" CssClass="Dropdown" AutoPostBack="True" OnSelectedIndexChanged="ddlCompanyCode_SelectedIndexChanged" Width="139px" DataSourceID="CompanyCodeDS" DataTextField="CompanyCode" DataValueField="CompanyCode"></asp:DropDownList>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="ddlCompanyCode" Display="Dynamic" ErrorMessage="*Please select a drop down list item." ForeColor="#CC0000" ValidationGroup="Submit"></asp:RequiredFieldValidator>
                </td>
                <td>
                    <asp:Label ID="lblSourceSyst" runat="server" CssClass="txtLabel" Text="Select Company Code" Width="137px" ></asp:Label>
                </td>
    <asp:SqlDataSource ID="CompanyCodeDS" runat="server" SelectCommandType="StoredProcedure" ConnectionString="<%$ ConnectionStrings:RptDatamartConnectionString %>" SelectCommand="[AXMap].[SelectCompanyCode_GLSourceCOA]">
    </asp:SqlDataSource>

C#:

        protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlCompanyCode.Items.Add(new ListItem("--Please Select--", ""));
            ddlCompanyCode.AppendDataBoundItems = true;
        }
    }
    protected void ddlCompanyCode_SelectedIndexChanged(object sender, EventArgs e)
    {
        String connectionString = ConfigurationManager.ConnectionStrings["RptDatamartConnectionString"].ConnectionString;
        String sqlStoredProc = "RptDatamart.AXMap.SelectCompanyCode_GLSourceCOA";
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand();
        cmd.Parameters.AddWithValue("@CompanyCode", ddlCompanyCode.SelectedItem.Value);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = sqlStoredProc;
        cmd.Connection = con;
        try
        {
            con.Open();
            SqlDataReader dataReader = cmd.ExecuteReader();
            while (dataReader.Read())
            {
                lblSourceSyst.Text = dataReader["SourceSystem"].ToString();
            }
        }
        catch (Exception ex)
        {
            lblStatus.Text = ex.Message;
        }
    }

然后我试图使用的存储过程。。。

ALTER PROCEDURE [AXMap].[SelectCompanyCode_GLSourceCOA] 
@CompanyCode varchar(10) = Null,
@SourceSystem nvarchar(255) = Null
AS
BEGIN
SET NOCOUNT OFF;
Select distinct CompanyCode, SourceSystem from [RptDatamart].[AXMap].[GLSourceCOA]
END

我的sql可能非常糟糕,但我认为它会起作用。

Asp:由dropdownlistbox确定的标签工作不正常

lblSourceSyst.Text = dataReader["SourceSystem"].ToString();

应为:

lblSourceSyst.Text += dataReader["SourceSystem"].ToString();