使用PostBackUrl,获取PreviousPage.Control()

时间:2012-08-18 09:36:52

标签: c# asp.net

我正在使用PostBackUrl将我的控件从“firstwebpage.aspx”发布到“secondwebpage.aspx”,以便我能够生成一些配置文件。

我知道我可以在我的secondwebpage.aspx中使用 PreviousPage.FindControl(“myControlId”)方法从“firstwebpage.aspx”获取我的控件,从而获取我的数据工作

但是,似乎这个方法对我在运行时编程生成的控件不起作用,同时在我的firstwebpage.aspx中的表中填充它们。

我也尝试过使用这个函数 Response.Write(“ - ”+ Request [“TextBox1”]。ToString()+“ - ”); 虽然这个语句打印出TextBox1上文本字段中的文本,但它只返回textbox1的字符串值。我也无法将其转换为以下格式的文本框控件

TextBox temptextBox =(TextBox)Request [“TextBox1”];

我的问题是,如何在“secondwebpage.aspx”的“firstwebpage.aspx”中实际访问我生成编程的控件?

请指教! 非常感谢!

//我的面板和aspx中的按钮

<asp:Panel ID="Panel2" runat="server"></asp:Panel>

<asp:Button ID="Button1" runat="server" Text="Generate Xml" PostBackUrl="~/WebForm2.aspx" onclick="Button1_Click" />

//这是我在面板中插入一行

的功能
 public void createfilerow(string b, string path, bool x86check, bool x86enable, bool x64check, bool x64enable)
        {
            Label blank4 = new Label();
            blank4.ID = "blank4";
            blank4.Text = "";
            Panel2.Controls.Add(blank4);

            CheckBox c = new CheckBox();
            c.Text = b.Replace(path, "");
            c.Checked = true;
            c.ID = "1a";
            Panel2.Controls.Add(c);

            CheckBox d = new CheckBox();
            d.Checked = x86check;
            d.Enabled = x86enable;
            d.ID = "1b";
            Panel2.Controls.Add(d);

            CheckBox e = new CheckBox();            
            e.Checked = x64check;
            e.Enabled = x64enable;
            e.ID = "1c";
            Panel2.Controls.Add(e);
    }

//my virtual path in WebForm2.aspx

<%@ PreviousPageType VirtualPath="~/WebForm1.aspx"  %>

//我的页面加载处理程序

protected void Page_Load(object sender, EventArgs e)
{  
    if (PreviousPage != null)
    {
        CheckBox tempCheckbox = (CheckBox)Page.PreviousPage.FindControl("1a");
        Button1.Text = tempCheckbox.Text;
    }         
}

//将在点击

时填充面板的处理程序
protected void Button7_Click(object sender, EventArgs e)
        {
            //get foldername 

            if (!Directory.Exists(@"myfilepath" + TextBox2.Text))
            {
                //folder does not exist
                //do required actions
                return;
            }
            string[] x86files = null;
            string[] x64files = null;
            string[] x86filespath = null;
            string[] x64filespath = null;
            ArrayList common = new ArrayList();
            if (Directory.Exists(@"myfilepath" + TextBox2.Text + "\\x86"))
                x86files = Directory.GetFileSystemEntries("myfilepath" + TextBox2.Text + "\\x86");
            if (Directory.Exists(@"myfilepath" + TextBox2.Text + "\\x64"))
                x64files = Directory.GetFileSystemEntries("myfilepath" + TextBox2.Text + "\\x64");

            //some codes to convert x64files and x86files to string[]

            //The header for Panel, 4 column
            Label FL = new Label();
            FL.ID = "flavourid";
            FL.Text = "Flavour";
            Panel2.Controls.Add(FL);

            Label filetext = new Label();
            filetext.ID = "filenamelabel";
            filetext.Text = "File(s)";
            Panel2.Controls.Add(filetext);

            Label label86 = new Label();
            label86.ID = "label86";
            label86.Text = "x86";
            Panel2.Controls.Add(label86);

            Label label64 = new Label();
            label64.ID = "label64";
            label64.Text = "x64";
            Panel2.Controls.Add(label64);


            //a for loop determine number of times codes have to be run
            for (int a = 0; a < num; a++)
            {
                ArrayList location = new ArrayList();
                if (//this iteration had to be run)
                {
                    string path = null;
                    switch (//id of this iteration)
                    {
                     case id:
                     path = some network address
                    }

                    //check the current version of iternation
                    string version = //version type;
                    //get the platform of the version
                    string platform = //platform

                    if (curent version = certain type)
                    {   
                        //do what is required.
                        //build a list
                    }
                    else
                    {
                        //normal routine
                        //do what is required
                        //build a list
                    }

                    //populating the panel with data from list

                        createflavourheader(a);
                        //create dynamic checkboxes according to the list

                     foreach(string s in list)
                     //createrow parameter is by version type and platform
                             createfilerow(readin, path, true, true, false, false);

                    }
                }
            }
            form1.Controls.Add(Panel2);
        }    

很抱歉无法向您展示完整的代码,因为它很长,我相信它应该是保密的,即使我写的都是

2 个答案:

答案 0 :(得分:2)

是的,您可以访问,下面是一个示例

 // On Page1.aspx I have a button for postback
  <asp:Button ID="btnSubmit" runat="server" Text="Submit" 
     PostBackUrl="~/Page2.aspx" />

 // Page1.aspx.cs 
 protected void Page_Load(object sender, EventArgs e)
 {
    TextBox t = new TextBox(); // created a TextBox
    t.ID = "myTextBox";        // assigned an ID
    form1.Controls.Add(t);     // Add to form

}

现在在第二页上,我将获得TextBox的值为

 // Page2.aspx.cs
 protected void Page_Load(object sender, EventArgs e)
 {       
    if (PreviousPage != null)
    {
        TextBox t = (TextBox) PreviousPage.FindControl("myTextBox");
        string mytboxvalue = t.Text;
    }
                        // OR
    string myTextBoxValue = Request.Form["myTextBox"];
 }

更新答案:

  Panel myPanel = new Panel();
    myPanel.ID = "myPanel";

    TextBox t = new TextBox();
    t.ID = "myTextBox";
    myPanel.Controls.Add(t);

    TextBox t1 = new TextBox();
    t1.ID = "myTextBox1";
    myPanel.Controls.Add(t1);

    // Add all your child controls to your panel and at the end add your panel to your form
    form1.Controls.Add(myPanel);

   // on the processing page you can get the values as
  protected void Page_Load(object sender, EventArgs e)
  {

    if (PreviousPage != null)
    {
        TextBox t = (TextBox) PreviousPage.FindControl("myTextBox");
        string mytboxvalue = t.Text;
    }

    string myTextBoxValue = Request.Form["myTextBox1"];
  }

答案 1 :(得分:0)

  

我也尝试过使用这个函数Response.Write(“ - ”+   请求[“TextBox1”]。ToString()+“ - ”);虽然这句话确实如此   打印出TextBox1上文本字段中的文本,它只返回我   textbox1的字符串值。我无法将其强制转换为文本框控件   也采用以下格式   TextBox temptextBox =(TextBox)Request [“TextBox1”];

嗨,呃, 我想你可以尝试将控件类型(例如'tb')与内容一起传递并创建一个新对象(例如TextBox)并将其分配给templtexBox对象。

我的20美分。 安迪