为什么listbox.selectIndex返回的值总是-1???

csdn_bob 2002-11-10 06:44:36
另外rows属性有什么用?我真的不知道!
...全文
144 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
csdn_bob 2002-11-11
  • 打赏
  • 举报
回复
protected System.Web.UI.WebControls.ListBox lboData;

foreach (DirectoryInfo dri in diArr) //显示目录
{
strTmp=dri.Name.ToLower();
strTmp=strTmp.PadRight(20,' ');
strTmp+="<目录>";
lboData.Items.Add(new ListItem(strTmp,dri.FullName));
}

private void btnOpenDir_Click(object sender, System.EventArgs e)
{
if(lboData.SelectedIndex>-1)
{
Label1.Text=lboData.SelectedItem.Value;
}
else
{
Label1.Text="没有选择!";
}
}


csdn_bob 2002-11-11
  • 打赏
  • 举报
回复
不行呀,怎么会呢?高手呀,救命呀!
newfans 2002-11-11
  • 打赏
  • 举报
回复
Page_Load中一定要加上
if (!IsPostBack)
{
DataBind();//你自己的向ListBox中加入数据的方法
}
rbjojo 2002-11-10
  • 打赏
  • 举报
回复
rows:
ListBox 控件中显示的行数。默认值为 4。
letian 2002-11-10
  • 打赏
  • 举报
回复
是否每次提交后都重新绑定了呢?

jb2008 2002-11-10
  • 打赏
  • 举报
回复
在你的web.config中加入
<system.web>
<!--此处设置页面编码,防止页面输出或数据传输出现的乱码现象,culture中文排序用-->
<globalization fileEncoding="gb2312" requestEncoding="gb2312"
responseEncoding="gb2312" culture="zh-CN" />
</system.web>

rows就是显示listBox的行数,如果ListBox的实际行数超过rows,则会出现上下滚动条
QQ好友例表控件 带实例和源码 //1. 属性列表: // SelectionMode 组件中条目的选择类型,即多选(Multiple)、单选(Single) // Rows 列表框中显示总共多少行 // Selected 检测条目是否被选中 // SelectedItem 返回的类型是ListItem,获得列表框中被选择的条目 // Count 列表框中条目的总数 // SelectedIndex 列表框中被选择项的索引 // Items 泛指列表框中的所有项,每一项的类型都是ListItem //2. 取列表框中被选中的 // ListBox.SelectedValue //3. 动态的添加列表框中的项: // ListBox.Items.Add("所要添加的项"); //4. 移出指定项: // //首先判断列表框中的项是否大于0 // If(ListBox.Items.Count > 0 ) // { ////移出选择的项 //ListBox.Items.Remove(ListBox.SelectedItem); // } //5. 清空所有项: // //首先判断列表框中的项是否大于0 // If(ListBox.Items.Count > 0 ) // { ////清空所有项 //ListBox.Items.Clear(); // } //6. 列表框可以一次选择多项: // 只需设置列表框的属性 SelectionMode="Multiple",按Ctrl可以多选 //7. 两个列表框联动,即两级联动菜单 // //判断第一个列表框中被选中的 // switch(ListBox1.SelectValue) // { ////如果是"A",第二个列表框中就添加这些: //case "A" // ListBox2.Items.Clear(); // ListBox2.Items.Add("A1"); // ListBox2.Items.Add("A2"); // ListBox2.Items.Add("A3"); ////如果是"B",第二个列表框中就添加这些: //case "B" // ListBox2.Items.Clear(); // ListBox2.Items.Add("B1"); // ListBox2.Items.Add("B2"); // ListBox2.Items.Add("B3"); // } //8. 实现列表框中项的移位 // 即:向上移位、向下移位 // 具体的思路为:创建一个ListBox对象,并把要移位的项先暂放在这个对象中。 // 如果是向上移位,就是把当前选定项的的上一项的赋给当前选定的项,然后 // 把刚才新加入的对象的,再附给当前选定项的前一项。 // 具体代码为: // //定义一个变量,作移位用 // index = -1; // //将当前条目的文本以及都保存到一个临时变量里面 // ListItem lt=new ListItem (ListBox.SelectedItem.Text,ListBox.SelectedValue); // //被选中的项的等于上一条或下一条的 // ListBox.Items[ListBox.SelectedIndex].Text=ListBox.Items[ListBox.SelectedIndex + index].Text; // //被选中的项的等于上一条或下一条的 // ListBox.Items[ListBox.SelectedIndex].Value=ListBox.Items[ListBox.SelectedIndex + index].Value; // //把被选中项的前一条或下一条的用临时变量中的取代 // ListBox.Items[ListBox.SelectedIndex].Test=lt.Test; // //把被选中项的前一条或下一条的用临时变量中的取代 // ListBox.Items[ListBox.SelectedIndex].Value=lt.Value; // //把鼠标指针放到移动后的那项上 // ListBox.Items[ListBox.SelectedIndex].Value=lt.Value; //9. 移动指针到指定位置: // (1).移至首条 // //将被选中项的索引设置为0就OK了 // ListBox.SelectIndex=0; // (2).移至尾条 // //将被选中项的索引设置为ListBox.Items.Count-1就OK了 // ListBox.SelectIndex=ListBox.Items.Count-1; // (3).上一条 // //用当前被选中的索引去减 1 // ListBox.SelectIndex=ListBox.SelectIndex - 1; // (4).下一条 // //用当前被选中的索引去加 1 // ListBox.SelectIndex=ListBox.SelectIndex + 1; //this.ListBox1.Items.Insertat(3,new ListItem("插入在第3行之后项","")); //this.ListBox1.Items.Insertat(index,ListItem) //ListBox1.Items.Insert(0,new ListItem("text","value"));

62,266

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧