首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

内容页给自定义控件赋值后,自定义控件调用本身的值时属性值为空,这是什么情况

2012-12-25 
内容页给自定义控件赋值后,自定义控件调用自身的值时属性值为空,这是什么情况?我写了个用户控件ShowLoginB

内容页给自定义控件赋值后,自定义控件调用自身的值时属性值为空,这是什么情况?
我写了个用户控件ShowLoginBar,在内容页给用户控件的属性toURL赋了值,但是用户控件自身调用这个值的时候toURL的值却为空。我是菜鸟,大家多多帮忙啊!急急急急急急急!

自定义控件ShowLoginBar.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Web.Security;

namespace TraineeManagementSystem.Include.Ascx
{
    public partial class ShowLoginBar : System.Web.UI.UserControl
    {
        public string RootPath;
        private string toURL;
        /// <summary>
        /// toURL弹出登录框跳转的路径
        /// </summary>
        public string ToURL
        {
            get { return toURL; }
            set { toURL = value;  }
        }
        private string thisURL;
        /// <summary>
        /// toURL弹出登录框跳转的路径
        /// </summary>
        public string ThisURL
        {
            get { return toURL; }
            set { toURL = value; }
        }
        private static string thisToUrl;
        protected void Page_Load(object sender, EventArgs e)
        {
            RootPath = TraineeManagementSystem.Code.GetFilePath._ApplicationPath;
        }
        protected void lnkPWD_Click(object sender, EventArgs e)
        {
            Session["tabNumber"] = "3";
            Response.Redirect("aq.aspx");
        }
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            //创建链接字符串
            string conStr = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
            //创建数据库链接
            SqlConnection con = new SqlConnection(conStr);


            con.Open();
            //使用MD5加密,将用户输入的密码加密
            string passWord = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPWD1.Text.Trim().ToString(), "MD5");
            //创建SQL语句查询用户输入的帐号和密码是否正确
            string cmdStr = "select count(*) from T_UserInfo where UserName=@UserName and Password=@Password";
            //创建SqlCommand对象
            SqlCommand cmd = new SqlCommand(cmdStr, con);
            //添加并设置Parameter的值
            cmd.Parameters.Add(new SqlParameter("UserName", SqlDbType.NVarChar, 50));
            cmd.Parameters["UserName"].Value = txtUserName1.Text;
            cmd.Parameters.Add(new SqlParameter("Password", SqlDbType.NVarChar, 50));
            cmd.Parameters["Password"].Value = passWord;
            //判断Executescalar方法返回的参数是否大于0表示登录成功,并给出提示
            cmd.ExecuteScalar();
            if (Convert.ToInt32(cmd.ExecuteScalar()) > 0)
            {
                Session["UserName"] = txtUserName1.Text;
                if (chkPWD.Checked)
                {
                    Session["PassWord"] = txtPWD1.Text;
                }
                System.Web.UI.Page motherPage = this.Page;

                ShowMessage.ShowMessages.ShowConfirm("又登录成功了,是否跳转?",this.toURL, this.thisURL);
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "False", "<script>alert('用户名或密码错误!')</script>");
            }

            con.Close();

        }

    }
}



内容页.aspx

 <UserControl:ShowLoginBar runat="server"  ID="ShowLogin" />


内容页.cs


 protected void imgBtnWrite_Click(object sender, ImageClickEventArgs e)
        {
            if (Session["UserName"] == null)
            {
                ShowLogin.ToURL= "~/Admin/WriteContents.aspx";
                ShowLogin.ThisURL = this.Request.Path;
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Success", "<script>showFloat()</script>");
            }
            else
                Response.Redirect("~/Admin/WriteContents.aspx");
        }

[最优解释]
感觉是这样,点击按钮时,因为是服务端组件,会引起页面刷新,重新初始化组件,属性都为空,你可以使用viewstate等传值
或者你在cs代码中加上

           if(!Page.IsPostBack) 
            { 
                自定义控件.OnInit(10,20); 
            }

热点排行