这是我定义的ListBox:
//数据绑定到ListBox
this.month=new string[12];
for(int i=1;i<=12;i++)
{
this.month[i-1]="第"+i.ToString()+"月";
}
this.mingzuList.DataBind();
//单击按钮将ListBox选定的值赋值给Lbl_result
public void delbtn_Click(object sender,System.EventArgs e)
{
foreach(ListItem item in this.mingzuList.Items)
{
this.Lbl_result.Text="dd";
if(item.Selected==true)
{
this.Lbl_result.Text+=item.Text;
}
}
}
但取得的ListBox的值为空,这是怎么回事呀,请高手帮帮忙呀
foreach(ListItem item in this.mingzuList.Items)
{
this.Lbl_result.Text="dd";
if(item.Selected==true)
{
this.Lbl_result.Text+=item.Text;
}
}
改成this.Lbl_result.Text=this.mingzuList.SelectedValue;
你的response.write(listbox1.selectedvalue.tostring())这句话必须写到listbox1的selectedindexchanged事件中才能得到响应,双击selectedindexchanged事件,然后:
protected void listbox1_selectedindexchanged(object sender, eventargs e)
{
response.write(listbox1.selectedvalue.tostring());
}
2个错误首先加载时这么绑定数据
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.month = new string[12];
for (int i = 1; i <= 12; i++)
{
this.month[i - 1] = "第" + i.ToString() + "月";
}
this.mingzuList.DataBind();
}
}
第2读数据时这么写
protected void Button1_Click(object sender, EventArgs e)
{
foreach (ListItem item in this.mingzuList.Items)
{
if (item.Selected.Equals(true))
{
this.Lbl_result.Text += item.Text;
}
}
}
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息