0

列表框填充但找不到所选值。它是 null 或默认值(第一项)。每当我选择另一个项目时,它都会变为 null。我做了 !ispostback 但仍然没有。在 asp.net/c#/sql 中使用向导。任何帮助表示赞赏。

     protected void Page_Load(object sender, EventArgs e)
    {
        ListBox lstService = (ListBox)Wizard1.FindControl("lstService");
        string s = lstService.SelectedValue.ToString();
        int s1 = lstService.SelectedIndex;

        if (s == "MarketTrack Toys")
        {
            Wizard2.Visible = true;
        }

        if (!Page.IsPostBack)
        {
            BindGrid();
        }

        if ((Wizard1.ActiveStepIndex <= 5) && (Wizard1.ActiveStepIndex != 0))
        {
            Wizard1.DisplaySideBar = true;
            Wizard2.DisplaySideBar = false;
        }
        else
        {
            Wizard1.DisplaySideBar = false;
            Wizard2.DisplaySideBar = true;
        }

    }

    private void BindGrid()
    {

        dAS = new DataAccessClass();
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();


        ds = dAS.func_FillDataset("select servicename from dbo.Services", "Services");
        ListBox lstService = (ListBox)Wizard1.FindControl("lstService");
        lstService.DataSource = ds;
        lstService.DataTextField = "ServiceName";
        lstService.DataValueField = "ServiceName";
        lstService.DataBind();

        if (lstService.Items.Count > 0)
        {
            lstService.SelectedIndex = 0;
        }
    }
4

1 回答 1

1

在调试器中遍历它。

放置在 ListBox 中的任何对象都不知道将自己转换为您要查找的字符串ToString()

ListBox 正确填充,因为您指定了如何获取名称lstService.DataTextField = "ServiceName";

您可能需要重新处理您放入 ListBox 中的对象,或者只是覆盖该对象的 ToString。

于 2012-09-07T20:47:21.663 回答