DropDownList1.SelectedItem.Text 为什么总是选到第一项?

cangjingshenying 2010-12-15 11:58:51
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Top.aspx.cs" Inherits="Top" %>

<!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>
<style type="text/css">
.style1
{
text-align: right;
height: 150px;
}
.style2
{
font-size: xx-large;
}
.style3
{
width: 100%;
}
.style4
{
height: 84px;
text-align: center;
font-size: xx-large;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="style1">
<span class="style2"></span>
<table class="style3">
<tr>
<td class="style4">
文件管理</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="上传"
Width="80px" />
<asp:FileUpload ID="FileUpload1" runat="server" Width="176px" />
<asp:DropDownList ID="DropDownList1" runat="server" Height="16px"
Width="84px">
<asp:ListItem Value="ppt/">初一级ppt</asp:ListItem>
<asp:ListItem Value="word/">初一级word</asp:ListItem>
<asp:ListItem Value="ppt/">初二级ppt</asp:ListItem>
<asp:ListItem Value="word/">初二级word</asp:ListItem>
<asp:ListItem Value="ppt/">初三级ppt</asp:ListItem>
<asp:ListItem Value="word/">初三级word</asp:ListItem>
<asp:ListItem Value="ppt/">高一级ppt</asp:ListItem>
<asp:ListItem Value="word/">高一级word</asp:ListItem>
<asp:ListItem Value="ppt/">高二级ppt</asp:ListItem>
<asp:ListItem Value="word/">高二级word</asp:ListItem>
<asp:ListItem Value="ppt/">高三级ppt</asp:ListItem>
<asp:ListItem Value="word/">高三级word</asp:ListItem>
<asp:ListItem Value="ppt/">通知ppt</asp:ListItem>
<asp:ListItem Value="word/">通知word</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="下载"
Width="80px" />
<asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="删除" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class Top : System.Web.UI.Page
{
string downpath = "";
string mpath="";
protected void Page_Load(object sender, EventArgs e)
{
if (Session["dlm"].ToString() == "admin")
{
Button3.Enabled = true;
}
else
Button3.Enabled = false;
if (!IsPostBack)
{
select();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//if (Session["dlm"].ToString() != "")
//{
string filePath = "", fileExtName = "", mFileName;
if ("" != FileUpload1.PostedFile.FileName)
{
select();
filePath = FileUpload1.PostedFile.FileName;
fileExtName = filePath.Substring(filePath.LastIndexOf(".") + 1);
if (fileExtName == "doc" || fileExtName == "ppt")
{
try
{
mFileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
FileUpload1.PostedFile.SaveAs(mpath+"\\"+DropDownList1.SelectedValue+mFileName);
Response.Write("<script language =javascript>alert('上传成功');history.back();</script>");
}
catch (Exception error)
{
Response.Write("<script language =javascript>alert('" + error.ToString() + "')</script>");
}
}
else
{
Response.Write("<script language =javascript>alert('文件格式不对');history.back();</script>");
return;
}
}
else
{
Response.Write("<script language =javascript>alert('请选择文件');history.back();</script>");
}
//}
//else
//{
// Response.Write("<script language =javascript>alert('请先登陆');history.back();</script>");
// return;
//}
}
public void select()
{
switch (DropDownList1.SelectedItem.Text)
{
case "初一级ppt":
mpath = Server.MapPath("初一级");
break;
case "初一级word":
mpath = Server.MapPath("初一级");
break;
case "初二级ppt":
mpath = Server.MapPath("初二级");
break;
case "初二级word":
mpath = Server.MapPath("初二级");
break;
case "初三级ppt":
mpath = Server.MapPath("初三级");
break;
case "初三级word":
mpath = Server.MapPath("初三级");
break;
case "高一级ppt":
mpath = Server.MapPath("高一级");
break;
case "高一级word":
mpath = Server.MapPath("高一级");
break;
case "高二级ppt":
mpath = Server.MapPath("高二级");
break;
case "高二级word":
mpath = Server.MapPath("高二级");
break;
case "高三级ppt":
mpath = Server.MapPath("高三级");
break;
case "高三级word":
mpath = Server.MapPath("高三级");
break;
case "通知ppt":
mpath = Server.MapPath("通知");
break;
case "通知word":
mpath = Server.MapPath("通知");
break;
}
}
...全文
403 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2010-12-15
  • 打赏
  • 举报
回复
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
select();
}
Button3.Enabled =Session["dlm"].ToString() == "admin"? true:false;

}
v风雪山神庙v 2010-12-15
  • 打赏
  • 举报
回复
DropDownList的AutoPostBack属性没有设置
flyerwing 2010-12-15
  • 打赏
  • 举报
回复
if(!ispostback)
{
bindddlst();
}
kevin_520_kevin 2010-12-15
  • 打赏
  • 举报
回复
你开启DropDownList1的autopostback属性才行,在它的选择事件里加代码就好了
kevin_520_kevin 2010-12-15
  • 打赏
  • 举报
回复
估计就是楼上说的那样的,你要考虑两个,一个是页面回传的问题就是加!ispostBack,还有就是在加载事件里调用了,但是你重新选中了DropDownList1的项后没有确定它的值,当然每次就是第一项的值
q107770540 2010-12-15
  • 打赏
  • 举报
回复
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
select();
}
if (Session["dlm"].ToString() == "admin")
{
Button3.Enabled = true;
}
else
Button3.Enabled = false;

}
还想懒够 2010-12-15
  • 打赏
  • 举报
回复
protected void Page_Load(object sender, EventArgs e)
{


这里面包含的代码加上if(!IsPostBack)
cangjingshenying 2010-12-15
  • 打赏
  • 举报
回复
非常感谢bearben2010 真是这个问题
bearben2010 2010-12-15
  • 打赏
  • 举报
回复

<asp:ListItem Value="ppt/">初一级ppt</asp:ListItem>
<asp:ListItem Value="word/">初一级word</asp:ListItem>
<asp:ListItem Value="ppt/">初二级ppt</asp:ListItem>
<asp:ListItem Value="word/">初二级word</asp:ListItem>
<asp:ListItem Value="ppt/">初三级ppt</asp:ListItem>
<asp:ListItem Value="word/">初三级word</asp:ListItem>
<asp:ListItem Value="ppt/">高一级ppt</asp:ListItem>
<asp:ListItem Value="word/">高一级word</asp:ListItem>
<asp:ListItem Value="ppt/">高二级ppt</asp:ListItem>
<asp:ListItem Value="word/">高二级word</asp:ListItem>
<asp:ListItem Value="ppt/">高三级ppt</asp:ListItem>
<asp:ListItem Value="word/">高三级word</asp:ListItem>
<asp:ListItem Value="ppt/">通知ppt</asp:ListItem>
<asp:ListItem Value="word/">通知word</asp:ListItem>


必须保证每个value都唯一,否则就会出现你这种莫名其妙的问题
cangjingshenying 2010-12-15
  • 打赏
  • 举报
回复
pageload 里面加个POSTBACK 判断啊 我已经判断过了 但是判断的内容我觉得是不正确的,如if (!IsPostBack)
{
select();
}
不知道怎么该啊 麻烦高手们解决一下!!
西域Bluchen 2010-12-15
  • 打赏
  • 举报
回复
pageload 里面加个POSTBACK 判断啊
cangjingshenying 2010-12-15
  • 打赏
  • 举报
回复
if (!IsPostBack)
{
select();// 这里是要放置初始化DropDownList1的数据的,但数据我已经事先写在里面了,<asp:ListItem Value="ppt/">初一级ppt</asp:ListItem>
我设AutoPostBack=true,时所选的值立即刷新 变成初始化了,所以变成总是第一项,但是不知道怎么解决这的问题
}
cangjingshenying 2010-12-15
  • 打赏
  • 举报
回复
都不行啊 如果设置AutoPostBack属性 所选的值会消失 一选它就还原成默认值了
ycproc 2010-12-15
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wuyq11 的回复:]

protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
select();
}
Button3.Enabled =Session["dlm"].ToString() == "admin"? true:false;

}
[/Quote]

62,074

社区成员

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

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

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

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