Atlas ASP.Net C# Listbox动态添加数据响应问题

soddyque 2007-07-03 05:32:47
.cs
======
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ListBox1.Items.Clear();
ListBox1.Items.Add("n1");
ListBox1.Items.Add("n2");
ListBox1.Items.Add("n3");
ListBox1.SelectedIndexChanged += new EventHandler(ListBox1_SelectedIndexChanged);
}

void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//throw new Exception("The method or operation is not implemented.");
ListBox ls = (ListBox)sender;
Label1.Text = ls.Items[ls.SelectedIndex].Value;
}
}
===========

.aspx
==========
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True" Height="155px" Width="71px">
</asp:ListBox>
<asp:Label ID="Label1" runat="server" Text="Label" Width="100px"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
===============

报错超出索引。
...全文
356 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
soddyque 2007-07-03
  • 打赏
  • 举报
回复
Label1.Text = ls.Items[ls.SelectedIndex].Value;
超出索引。跟的时候ls.SelectedIndex显示是-1.
lishijie910123 2007-07-03
  • 打赏
  • 举报
回复
给出报错信息
LixingTie 2007-07-03
  • 打赏
  • 举报
回复
哪行报错?

62,243

社区成员

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

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

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

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