积极答复者
可能非有意的引用比较;若要获取值比较,请将左边转换为类型"string"

问题
-
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 fileManage_inceptFile : System.Web.UI.Page
{
BaseClass bc = new BaseClass();
protected void Page_Load(object sender, EventArgs e)
{
if (Session["loginName"] == string.Empty) (一个警告:可能非有意的引用比较;若要获取值比较,请将左边转换为类型"string")
{
bc.MessageBox("该用户在线20分钟,没有操作任何动作,即将返回到系统主页!");
Response.Redirect("../Index.aspx");
}
if (!IsPostBack)
{
if (Session["loginName"].ToString() == "admin")
{
DataList1.DataSource = bc.GetDataSet("select * from [file] order by fileTime desc", "file");
DataList1.DataKeyField = "fileid";
DataList1.DataBind();
RadioButton2.Enabled = false;
RadioButton1.Enabled =false;
return;
}
else
{
DataList1.DataSource = bc.GetDataSet("select * from [file] where examine='已接收' and fileAccepter='" + Session["loginName"].ToString() + "' order by fileTime desc", "file");
DataList1.DataKeyField = "fileid";
DataList1.DataBind();
}
}
if (RadioButton1.Checked)
{
DataList1.DataSource = bc.GetDataSet("select * from [file] where examine='未接收' and fileAccepter='" + Session["loginName"].ToString() + "' order by fileTime desc", "file");
DataList1.DataKeyField = "fileid";
DataList1.DataBind();
}
if (RadioButton2.Checked)
{
DataList1.DataSource = bc.GetDataSet("select * from [file] where examine='已接收' and fileAccepter='" + Session["loginName"].ToString() + "' order by fileTime desc", "file");
DataList1.DataKeyField = "fileid";
DataList1.DataBind();
}
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
bc.ExecSQL("UPDATE [file] SET examine = '已接收' WHERE fileID ='" + (int)DataList1.DataKeys[e.Item.ItemIndex] + "'");
DataList1.DataSource = bc.GetDataSet("select * from [file] where fileAccepter='" + Session["loginName"].ToString() + "' order by fileTime desc", "file");
DataList1.DataKeyField = "fileid";
DataList1.DataBind();
}
}
答案
全部回复
-
Session["loginName"].ToString() == string.Empty
C#非常严谨,因为Session是存储object的,理论上不能直接转化成String,但是每个类先天从其父类中又继承了ToString方法(可重写)。所以建议使用以上方法,让两个String进行比较。
- 已建议为答案 ThankfulHeartModerator 2010年5月11日 9:01
-
你好!
Session["loginName"]返回的是Object类型,而Object类实现的==比较的是两个对象的引用是否一致,这里Object的内容即使是“”,也和string.Empty不是同一引用,所以永远不可能相等,所以编译器提醒你!
string类的==实现的是值比较,比较两个string值的值是否相等!
周雪峰- 已建议为答案 ThankfulHeartModerator 2010年5月11日 9:01