Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取列表框的选定索引和选定值_C#_Asp.net - Fatal编程技术网

C# 获取列表框的选定索引和选定值

C# 获取列表框的选定索引和选定值,c#,asp.net,C#,Asp.net,获取列表框的选定索引和选定值时遇到问题。我试图用数据库中的值填充列表框,在此之后,在selected index change事件中,我无法提取selected index和selected value。相反,我得到的选定索引为-1,而选定的值不包含值 以下是单击列表框中任何项目之前的屏幕截图: 这是单击项目后拍摄的屏幕截图: 以下是c#代码: 如果您在页面加载中放置一个断点,您会注意到它会在每次回发中命中。由于发生了这种情况,您的代码设计为每次回发时列表框都会恢复数据,包括在列表框中选择新

获取列表框的选定索引和选定值时遇到问题。我试图用数据库中的值填充列表框,在此之后,在selected index change事件中,我无法提取selected index和selected value。相反,我得到的选定索引为-1,而选定的值不包含值

以下是单击列表框中任何项目之前的屏幕截图:

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

以下是c#代码:


如果您在页面加载中放置一个断点,您会注意到它会在每次回发中命中。由于发生了这种情况,您的代码设计为每次回发时列表框都会恢复数据,包括在列表框中选择新索引时。这就是导致您的SelectedIndex被重置的原因

你需要做的只是绑定你的列表框一次。您可以通过检查条件来实现这一点。如果回发不是由客户端引起的,请绑定列表框

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"));
    }
}

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

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

if(!IsPostBack)
{
         Label1.Text = Session["uid"].ToString();
          Label2.Text = Session["crs_id"].ToString();
          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"));
}
if(!IsPostBack)
{
         Label1.Text = Session["uid"].ToString();
          Label2.Text = Session["crs_id"].ToString();
          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"));
}