1

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

我知道我可以在我的 secondwebpage.aspx 中使用PreviousPage.FindControl("myControlId")方法从“firstwebpage.aspx”中获取我的控制权,从而获取我的数据并且它起作用了。

但是,似乎此方法不适用于我在运行时以编程方式生成的控件,同时将它们填充到我的 firstwebpage.aspx 的表中。

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

文本框 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);
        }    

抱歉不能给你看完整的代码,因为它很长,我相信它应该是保密的,即使我把它们都写了

4

2 回答 2

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"];
  }
于 2012-08-18T09:57:28.910 回答
0

我也尝试使用这个函数 Response.Write("--" + Request["TextBox1"].ToString() + "--"); 尽管此语句确实打印了 TextBox1 上文本字段中的文本,但它只返回了 textbox1 的字符串值。我也无法将其转换为以下格式的文本框控件 TextBox temptextBox = (TextBox)Request["TextBox1"];

嗨 lw,我认为您可以尝试将控件类型(例如“tb”)与内容一起传递并创建一个新对象(例如 TextBox)并将其分配给 templtexBox 对象。

我的 20 美分。安迪

于 2012-08-20T08:50:40.360 回答