我必须单击两次链接按钮才能更改页面

时间:2014-02-19 07:05:26

标签: asp.net

我必须单击两次链接按钮才能更改页面并加载当前文件夹的子文件夹。我想我的会话和活动有些问题。

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            TableRow r = new TableRow();
            TableCell c1 = new TableCell();
            LinkButton l = new LinkButton();
            var strarray = Server.MapPath(Session["url"].ToString()).Split(Path.DirectorySeparatorChar);
            Table table = new Table();
            l.ID = "lable" + table.Rows.Count;
            l.Text = strarray.Last();
            l.EnableViewState = true;
            r.ID = "newRow" + table.Rows.Count;
            c1.ID = "newC1" + table.Rows.Count;
            c1.Controls.Add(l);
            r.Cells.Add(c1);
            table.Rows.Add(r);
            this.form1.Controls.Add(table);
        }
        else
        {
            if(Directory.Exists(Server.MapPath(Session["url"].ToString())))
            {
                string[] allFolders = Directory.GetDirectories(Server.MapPath(Session["url"].ToString()));
                Table table = new Table();
                foreach (string str in allFolders)
                {
                    TableRow r = new TableRow();
                    TableCell c1 = new TableCell();
                    LinkButton l = new LinkButton();
                    var strarray = str.Split(Path.DirectorySeparatorChar);
                    l.ID = strarray.Last();
                    l.CommandName = "createLink";
                    l.Click+= new EventHandler(LinkButton_Command);
                    l.Text = strarray.Last();
                    l.EnableViewState = true;                  
                    r.ID = "newRow" + table.Rows.Count;
                    c1.ID = "newC1" + table.Rows.Count;
                    c1.Controls.Add(l);
                    r.Cells.Add(c1);
                    table.Rows.Add(r);
                    this.form1.Controls.Add(table);
                }
            }
            else
            {
                Work work=new Work();
                work.SetFolder(Session["url"].ToString());
                work.CreateFolder();
            }
        }
    }

    public void LinkButton_Command(object sender, EventArgs e)
    {
        Session["url"] = Session["url"] + "\\" + ((LinkButton)sender).ID;
    }

2 个答案:

答案 0 :(得分:0)

我认为这是因为在Page_Load()之前调用LinkButton_Command()方法。当Session["url"]中的代码执行时,会话变量Page_Load()仍包含先前的值。

尝试从Page_Load()末尾的LinkButton_Command()的else分支移动代码。

答案 1 :(得分:0)

在混淆了你的答案和我的程序之后,找到一个可行的答案。

protected void Page_Load(object sender,EventArgs e)         {             if(Directory.Exists(Server.MapPath(Session [“url”]。ToString())))             {                 string [] allFolders = Directory.GetDirectories(Server.MapPath(Session [“url”]。ToString()));                 表table = new Table();                 foreach(allFolders中的字符串str)                 {                     TableRow r = new TableRow();                     TableCell c1 = new TableCell();                     LinkBut​​ton l = new LinkBut​​ton();                     var strarray = str.Split(Path.DirectorySeparatorChar);                     l.ID = strarray.Last();                     l.CommandName =“createLink”;                     l。点击+ = new EventHandler(LinkBut​​ton_Command);                     l.Text = strarray.Last();                     l.EnableViewState = true;                     r.ID =“newRow”+ table.Rows.Count;                     c1.ID =“newC1”+ table.Rows.Count;                     c1.Controls.Add(升);                     r.Cells.Add(C1);                     table.Rows.Add(R);                     this.form1.Controls.Clear();                     this.form1.Controls.Add(表);                 }             }

    }
    public void LinkButton_Command(object sender, EventArgs e)
    {
        Session["url"] = Session["url"] + "\\" + ((LinkButton)sender).ID;

        if (Directory.Exists(Server.MapPath(Session["url"].ToString())))
        {
            string[] allFolders = Directory.GetDirectories(Server.MapPath(Session["url"].ToString()));
            Table table = new Table();
            foreach (string str in allFolders)
            {
                TableRow r = new TableRow();
                TableCell c1 = new TableCell();
                LinkButton l = new LinkButton();
                var strarray = str.Split(Path.DirectorySeparatorChar);
                l.ID = strarray.Last();
                l.CommandName = "createLink";
                l.Click += new EventHandler(LinkButton_Command);
                l.Text = strarray.Last();
                l.EnableViewState = true;
                r.ID = "newRow" + table.Rows.Count;
                c1.ID = "newC1" + table.Rows.Count;
                c1.Controls.Add(l);
                r.Cells.Add(c1);
                table.Rows.Add(r);
                this.form1.Controls.Clear();
                this.form1.Controls.Add(table);
            }
        }

    }