本文介绍了列表框中的选定项为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!
问题描述
列表框中的项目很少.当我选择一个项目时,我将它存储在一个字符串中以供进一步使用,但是当我显示该字符串值时,它显示为 null.
下面是我的 Invitee.aspx 文件的代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Invitee.aspx.cs" Inherits="FinalProj2.Invitee" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 过渡//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="服务器"><标题></标题></头><身体><form id="form1" runat="server"><asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList><asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList><asp:DropDownList ID="DropDownList3" runat="server"></asp:DropDownList> <asp:DropDownList ID="DropDownList4" runat="server"></asp:DropDownList><asp:DropDownList ID="DropDownList5" runat="server"></asp:DropDownList><asp:DropDownList ID="DropDownList6" runat="server"></asp:DropDownList></div><asp:ListBox ID="ListBox1" runat="server" AutoPostBack="true" Height="310px"onselectedindexchanged="ListBox1_SelectedIndexChanged" 宽度="271px"></asp:ListBox><asp:TextBox ID="TextBox1" runat="server" Height="217px" Width="544px"></asp:TextBox></表格></身体></html>Invitee.aspx.cs 的代码:
使用系统;使用 System.Collections.Generic;使用 System.Linq;使用 System.Web;使用 System.Web.UI;使用 System.Web.UI.WebControls;命名空间 FinalProj2{公共部分类受邀者:System.Web.UI.Page{FinalProj2.Models.DataClasses1DataContext db = new FinalProj2.Models.DataClasses1DataContext();protected void Page_Load(object sender, EventArgs e){for (int i = 1; i <13; i++){DropDownList1.Items.Add(new ListItem(i.ToString()));DropDownList4.Items.Add(new ListItem(i.ToString()));}for (int i = 1; i <32; i++){DropDownList2.Items.Add(new ListItem(i.ToString()));DropDownList5.Items.Add(new ListItem(i.ToString()));}for (int i = 2010; i <2021; i++){DropDownList3.Items.Add(new ListItem(i.ToString()));DropDownList6.Items.Add(new ListItem(i.ToString()));}var query = 来自 db.Employees 中的 emp选择emp.Employee_Name;ListBox1.DataSource = 查询;ListBox1.DataBind();}protected void ListBox1_SelectedIndexChanged(对象发送者,EventArgs e){Response.Write("你好");字符串选择 = ListBox1.SelectedValue.ToString();Response.Write("
选择的值是" + selected);var query = 来自 db.Employees 中的 emp其中 emp.Employee_Name == 已选择选择emp.Employee_ID;Response.Write(查询);//int empid = query.First();//var query1 = from meet_emp in db.Meet_Emps//其中 meet_emp.Employee_ID == empid//选择 meet_emp.Meeting_ID;//int meetid = query1.First();//Response.Write(meetid);}}}
在列表框中选择一个项目后,字符串selected"的值为空.
解决方案 这是因为你在每个周期都运行 ListBox1.DataBind().改为:
if (!IsPostBack){var query = 来自 db.Employees 中的 emp选择emp.Employee_Name;ListBox1.DataSource = 查询;ListBox1.DataBind();}
I have few items in the listbox. When I select an item, I store it in a string for further use, but when I display that string value it is shown as null.
Below is the code for my Invitee.aspx file:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Invitee.aspx.cs" Inherits="FinalProj2.Invitee" %>
<!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:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList4" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList5" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList6" runat="server">
</asp:DropDownList>
</div>
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="true" Height="310px"
onselectedindexchanged="ListBox1_SelectedIndexChanged" Width="271px">
</asp:ListBox>
<asp:TextBox ID="TextBox1" runat="server" Height="217px" Width="544px"></asp:TextBox>
</form>
</body>
</html>
And code for Invitee.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FinalProj2
{
public partial class Invitee : System.Web.UI.Page
{
FinalProj2.Models.DataClasses1DataContext db = new FinalProj2.Models.DataClasses1DataContext();
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 1; i < 13; i++)
{
DropDownList1.Items.Add(new ListItem(i.ToString()));
DropDownList4.Items.Add(new ListItem(i.ToString()));
}
for (int i = 1; i < 32; i++)
{
DropDownList2.Items.Add(new ListItem(i.ToString()));
DropDownList5.Items.Add(new ListItem(i.ToString()));
}
for (int i = 2010; i < 2021; i++)
{
DropDownList3.Items.Add(new ListItem(i.ToString()));
DropDownList6.Items.Add(new ListItem(i.ToString()));
}
var query = from emp in db.Employees
select emp.Employee_Name;
ListBox1.DataSource = query;
ListBox1.DataBind();
}
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write("Hello");
string selected = ListBox1.SelectedValue.ToString();
Response.Write("
Selected Value is" + selected);
var query = from emp in db.Employees
where emp.Employee_Name == selected
select emp.Employee_ID;
Response.Write(query);
//int empid = query.First();
//var query1 = from meet_emp in db.Meet_Emps
// where meet_emp.Employee_ID == empid
// select meet_emp.Meeting_ID;
//int meetid = query1.First();
//Response.Write(meetid);
}
}
}
Value of string "selected" is null after I select an item in list box.
解决方案 It's because you are running ListBox1.DataBind() on every cycle. Change to:
if (!IsPostBack)
{
var query = from emp in db.Employees
select emp.Employee_Name;
ListBox1.DataSource = query;
ListBox1.DataBind();
}
这篇关于列表框中的选定项为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
The End
相关推荐
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11
C#/.NET开发问题
2
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11
C#/.NET开发问题
14
热门文章
1阅读完 JSON 内容后遇到的附加文本:
2Excel 错误 HRESULT: 0x800A03EC 尝试使用单元格名称获取范围
3承载错误 - invalid_token - 未找到签名密钥
4反序列化 Newtonsoft.Json 中的自定义异常
5RabbitMQ 连接错误没有一个指定的端点是可达的"
6“由于系统缺乏足够的缓冲区空间或队列已满,无法对套接字执行操作"
7使用 System.IdentityModel.Tokens.Jwt 解码和验证 JWT 令牌
8Linq - 在多个 (OR) 条件下进行左连接
热门精品源码
最新VIP资源
1多功能实用站长工具箱html功能模板
2多风格简历在线生成程序网页模板
3论文相似度查询系统源码
4响应式旅游景点宣传推广页面模板
5在线起名宣传推广网站源码
6酷黑微信小程序网站开发宣传页模板
7房产销售交易中介网站模板
8小学作业自动生成程序

大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)