获取ListBox的选定索引和选定值

时间:2016-05-09 17:44:25

标签: c# asp.net

我在获取所选索引和ListBox的选定值时遇到问题。我试图用数据库中的值填充列表框,然后,在选定的索引更改事件,我无法提取选定的索引和选定的值。相反,我得到一个选定的索引-1,所选的值不包含值。

以下是点击ListBox中任何项目之前的屏幕截图:

enter image description here

这是点击项目后拍摄的屏幕截图:

enter image description here

这是c#代码:

public partial class std_Course_dashboard : System.Web.UI.Page
{
    int index;
    string value;

    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Session["uid"].ToString();
        Label2.Text = Session["crs_id"].ToString();
        ListBox1.Items.Clear();
        SqlDataReader r;
        SqlCommand cmd = new SqlCommand("select lecture_text, lecture_Title,lecture_No   from Lecture where course_ID='" + Convert.ToInt32(Session["crs_id"]) + "'", con);
        con.Open();
        ListBox1.DataSource = cmd.ExecuteReader();
        ListBox1.DataTextField = "lecture_Title";
        ListBox1.DataValueField = "lecture_No";
        ListBox1.DataBind();
        con.Close();        
        ListBox1.Items.Insert(0, new ListItem("--Select Customer--", "0"));
    }

    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        value = ListBox1.SelectedValue;
        index = ListBox1.SelectedIndex;
        Label3.Text = value;
        Label4.Text = index.ToString();
    }
}

2 个答案:

答案 0 :(得分:3)

这是因为每次页面发出请求时清除项目

ip <- ggplot(data=daf,  aes(x=temp, y=diam, colour = iso)) +  
  geom_point() + facet_wrap(~iso)

ip + geom_smooth(method = "nls", 
                method.args = list(formula = y ~ thy * exp(thq * (x-thx)^2 + thc * (x - thx)^3), 
                                   start = list(thy=5.4, thq=-0.01, thx=25, thc=0.0008)),
                se = F, size = 0.5, data = subset(daf, iso=="Itiquira")) +

  geom_smooth(method = "nls", 
              method.args = list(formula = y ~ thy * exp(thq * (x-thx)^2 + thc * (x - thx)^3), 
                                 start = list(thy=5.4, thq=-0.01, thx=25, thc=0.0008)),
              se = F, size = 0.5, data = subset(daf, iso=="Londrina")) +

  geom_smooth(method = "nls", 
              method.args = list(formula = y ~ thy * exp(thq * (x-thx)^2 + thc * (x - thx)^3), 
                                 start = list(thy=5.4, thq=-0.01, thx=25, thc=0.0008)),
              se = F, size = 0.5, data = subset(daf, iso=="Sinop")) 

您可以删除此行代码并添加ListBox1.Items.Clear(); ,以便仅在页面加载时第一次加载数据

if(!IsPostBack)

答案 1 :(得分:2)

如果在Page_Load中放置一个断点,您会发现它在每一个PostBack上都会出现。由于这种情况发生,您的代码被设计为每次PostBack时ListBox都会反弹数据 - 包括在ListBox中选择新索引时。这就是导致SelectedIndex重置的原因。

您需要做的只是绑定ListBox一次。您可以通过检查Page.IsPostBack条件来实现此目的。如果PostBack不是由客户端引起的,请绑定ListBox。

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        Label1.Text = Session["uid"].ToString();
        Label2.Text = Session["crs_id"].ToString();
        ListBox1.Items.Clear();
        SqlDataReader r;
        SqlCommand cmd = new SqlCommand("select lecture_text, lecture_Title,lecture_No   from Lecture where course_ID='" + Convert.ToInt32(Session["crs_id"]) + "'", con);
        con.Open();
        ListBox1.DataSource = cmd.ExecuteReader();
        ListBox1.DataTextField = "lecture_Title";
        ListBox1.DataValueField = "lecture_No";
        ListBox1.DataBind();
        con.Close();        
        ListBox1.Items.Insert(0, new ListItem("--Select Customer--", "0"));
    }
}
相关问题