ListBox SelectedValue为null,selectedindex = -1

时间:2012-09-07 19:15:04

标签: c# webforms wizard

列表框已填充但无法找到所选值。它是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;
        }
    }

1 个答案:

答案 0 :(得分:1)

在调试器中完成。

无论在ListBox中放置什么对象都不知道将自己转换为您正在寻找的字符串ToString()

ListBox正确填充,因为您指定了如何使用lstService.DataTextField = "ServiceName";

获取名称

您可能必须重新编写放在ListBox中的对象,或者只是覆盖该对象的ToString。