一个很郁闷的问题?
我做的是文章的后台管理.我在一个页面的IFRAME窗口中显示一类文章的列表并在后面加上编辑,删除的文字链接.链接的页面也是在这个IFRAME中显示.在我没有用后台账号登录时,我按那个删除或编辑.会弹出<请先登录>窗口,但是在我按到5,6次时就会出现错误(String Uid = Session["Uid"].ToString();).有时要10几次,不知道这到底哪里出现在问题...会的大虾给指点下我.

解决方案 »

  1.   

    Session["Uid"].tostring() 
    出错是因为Session["Uid"]为空了。不能tostring()转换前先判断不为空。
      

  2.   

    session 过期 或者 你iis应用池 回收太快
      

  3.   

    还有个问题就是IFRAME容易丢失Session```
      

  4.   


    刚也碰到了这样一个问题...
    再比如:dropdownlist控件的内容在赋值前也要先检查下是否为空.特别是级联菜单.
    id.SelectedIndex>-1
      

  5.   

    我把代码贴出来:
    <aspx页面就不贴了>
    //HouTai.aspx.cn
    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;
    using System.Data.Sql;
    using System.Data.SqlClient;public partial class HouTai_HouTai : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String uid = this.Lab_loginName.Text;
            if (!IsPostBack)
            {
                            if (uid == "")
                {
                    this.Lab_loginName.Text = "您尚未<a href='#' onclick='popup_show()'>登录</a>!";
                    Session["Uid"] = "";
                }
                else
                {
                    this.Lab_loginName.Text = "欢迎您:" + uid;            }
            }
           
        }    //登录验证
        protected void Btn_Login_Click(object sender, EventArgs e)
        {
            String MyConStr = ConfigurationManager.ConnectionStrings["MyConStr"].ConnectionString;
            String login_uid =Server.HtmlEncode(this.Txb_Uid.Text);
            
            String login_pwd =Server.HtmlEncode(this.Txb_Pwd.Text);
            SqlConnection con = new SqlConnection();
            con.ConnectionString = MyConStr;
            SqlCommand cmd = new SqlCommand();
            String txtSql = "select Pwd from admin where (Uid='" + login_uid + "');";
            cmd.CommandText = txtSql;
            cmd.Connection = con;        try
            {
                con.Open();
                SqlDataReader sdr = cmd.ExecuteReader();
                if (sdr.Read())
                {
                    String pwd_temp = sdr["Pwd"].ToString();
                    pwd_temp = pwd_temp.Trim();
                    if (login_pwd == pwd_temp)
                    {
                        Response.Write("<script>alert('登录成功!');</script>");
                        Session["Uid"] = login_uid;
                        this.Lab_loginName.Text = " 欢迎您:" + login_uid;
                        con.Close();
                    }                else
                    {
                        Response.Write("<script>alert('密码错误!');</script>");
                        Response.Write("<script language=javascript>window.location.href='HouTai.aspx'</script>");
                    }
                }
                else
                {
                    con.Close();
                    Response.Write("<script>alert('用户名不存在!');</script>");
                    Response.Write("<script language=javascript>window.location.href='HouTai.aspx'</script>");
                }
                 
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }    }
    }
    //addNews.aspx.cn这个页面在上个页面的IFRAME中显示.
    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;
    using System.Data.Sql;
    using System.Data.SqlClient;
    using System.Runtime;public partial class HouTai_addNews : System.Web.UI.Page
    {    String MyConStr = ConfigurationManager.ConnectionStrings["MyConStr"].ConnectionString;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               
            }
        }
        //添加新闻    protected void Btn_AddNews_Click(object sender, EventArgs e)
        {
            String Uid = Session["Uid"].ToString();
            String newstitle = this.Txb_NewsTitle.Text;
            String newscontent = this.Ftb_NewsContent.Text;
            kknet kk = new kknet();        if (Uid == "" || Uid == null)
            {
                Response.Write("<script lauguage=javascript>alert('请先登录!');history.back();</script>");
                Response.Write("<script lauguage=javascript>window.location.href='addNews.aspx'</script>");
            }        else
            {            if (newstitle == "")
                {
                    Response.Write("<script lauguage=javascript>alert('标题不能为空!');history.back();</script>");
                    Response.Write("<script>window.parent.Management.location.href('HNews.aspx')</script>");
                }
                SqlConnection con = new SqlConnection();
                con.ConnectionString = MyConStr;            SqlCommand cmd = new SqlCommand("insert into news (Title,Auther,News_Content,News_Time) values ('" + newstitle + "','" + Uid + "','" + newscontent + "','" + kk.getNowDate() + "');", con);
                try
                {
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                catch (Exception ex)
                { Response.Write(ex.ToString()); }            Response.Write("<script>alert('增加成功!');history.back();</script>");
                Response.Write("<script language=javascript>window.parent.Management.location.href('HNews.aspx')</script>");
            }
        }
      
        
    }
    还有就是有id登录,多按几次也会出现这种状况.感觉5楼的说法可能性比较大,但要怎么解决....还有一点忘记说了就是:我在本地测试没事,只有上传到空间时才会出现的.
      

  6.   

    String Uid = Session["Uid"]==null?"":Session["Uid"].ToString();
    通过div或JQUERY弹出窗口
      

  7.   

    是我用的判断方法出错了..但是为什么会点多次后才会出错呢(我思维卡在这里.).,....谢谢wuyq11同楼上朋友们.