我使用PostBackUrl将我的控件从"firstwebpage.aspx“发布到"secondwebpage.aspx”,以便能够生成一些配置文件。
我知道我可以在我的secondwebpage.aspx中使用secondwebpage.aspx方法从“firstwebpage.aspx”获取我的控制,从而获取我的数据,并且它工作了。
但是,该方法似乎不适用于在运行时通过编程方式生成的控件,而这些控件是在firstwebpage.aspx中的表中填充控件时生成的。
我还尝试使用这个函数Response.Write(“-”+请求“TextBox1”.ToString()+“-”);,尽管这个语句打印了textbox1上textfield中的文本,但它只返回textbox1的字符串值。我也无法以下列格式将其转换为textbox控件
TextBox temptextBox =(TextBox)请求“TextBox1”;
我的问题是,如何才能真正访问我在"firstwebpage.aspx“中在"secondwebpage.aspx”中生成的控件?
请指点!非常感谢!
// aspx中的my面板和按钮
<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);
} 抱歉,不能给你看完整的代码,因为它很长,我相信它应该是保密的,即使是我写的全部。
发布于 2012-08-18 09:57:28
是的,您可以访问,下面是一个示例
// 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-20 08:50:40
我还尝试使用这个函数Response.Write(“-”+请求“TextBox1”.ToString()+“-”);尽管这个语句打印了textbox1上textfield中的文本,但它只返回了textbox1的字符串值。我无法将其转换为以下格式的textbox控件: TextBox temptextBox =(TextBox)请求“TextBox1”;
嗨,lw,我认为您可以尝试将控制类型(例如'tb')与内容一起传递,并创建一个新的对象(例如TextBox),并将其分配给templtexBox对象。
我的20美分。安迪
https://stackoverflow.com/questions/12017507
复制相似问题