[英]Pass literal control from one page to another
I have this literal control in Page1: 我在第1页中有此文字控件:
string s1 = "<table>"
+ "<tr>"
+ "<td>AAA</td>"
+ "<td>AAA</td>"
+ "</tr>"
+ "<tr>"
+ "<td>BBB</td>"
+ "<td>BBB</td>"
+ "</tr>"
+ "</table>"
+ "<br />";
string s2 = "<input type=\"button\" onclick=\"window.open('Page2.aspx','','Height=500,width=500')\" value=\"pop up\" />";
LiteralControl lc = new LiteralControl(s1 + s2);
Div1.Controls.Add(lc);
As you can see, it is a simple HTML table and button. 如您所见,它是一个简单的HTML表格和按钮。
What I want to do is to click the button, pop up Page2.aspx, and add the literal control (lc) without the button to a Div in Page2.aspx (Div1). 我要做的是单击按钮,弹出Page2.aspx,然后将不带按钮的文字控件(lc)添加到Page2.aspx(Div1)中的Div。
I can't get this to work. 我无法使它正常工作。
Page1 must not post back to the server to accomplish this. Page1不得回发到服务器来完成此操作。
There are only three ways to pass data between pages: 在页面之间传递数据只有三种方法:
GET values 获取值
POST values POST值
Server variables (sessions ... etc) 服务器变量(会话等)
Server variables are the most flexible solution and shouldn't result in a severe performance hit if used correctly. 服务器变量是最灵活的解决方案,如果使用正确,则不会对性能造成严重影响。 But you've ruled that option out.
但是您已经排除了该选项。
POST values are another option. POST值是另一种选择。 They're stored in HiddenFields inside your form, but you need to do a form submit in order to use them.
它们存储在表单内的HiddenFields中,但是您需要进行表单提交才能使用它们。 And since you're using a link that opens up a pop up, this isn't a good solution.
而且由于您使用的链接会弹出一个弹出窗口,所以这不是一个好的解决方案。
This leaves GET values. 剩下GET值。 Which is storing the value inside the query string of the URL.
它将值存储在URL的查询字符串中。 This would work, but it has a size limitation of around 8 KB.
这可以工作,但是大小限制约为8 KB。 If you can keep the size of your HTML to within that, then what you would need to do is Base64 encode the HTML (this isn't encrypting it, it's just packaging it into one neat string), use the encoded string as part of the URL (ie "
Page2.aspx?data=SomeString
"), then in Page2 get that values using Request.Params["data"]
then Base64 decode it, and place that value in the local Literal control. 如果您可以将HTML的大小保持在此范围内,那么您需要做的是对 HTML 进行Base64编码 (这不是对其进行加密,它只是将其包装为一个整洁的字符串),请使用编码后的字符串作为URL(即“
Page2.aspx?data=SomeString
“),然后在Page2中使用Request.Params["data"]
获取该值,然后Base64对其进行解码,并将该值放置在本地Literal控件中。
But, it seems your value is much much larger than that. 但是,看来您的价值要比这大得多。 So that leaves you to use a server variable, or store your HTML somewhere and pass a reference to it.
这样就可以使用服务器变量,或将HTML存储在某个地方并传递对其的引用。 For instance, you can save the generated HTML inside a database with a unique identifier key and just pass the key to the Page2 which would query the DB for the actual HTML.
例如,您可以使用唯一的标识符密钥将生成的HTML保存在数据库中,然后将密钥传递给Page2,后者将向数据库查询实际的HTML。
This is a terrible way of programming , you should the .aspx
page handle the HTML and the Code behind part handle the data. 这是一种糟糕的编程方式 ,您应该让
.aspx
页处理HTML,而后面的代码处理数据。
for example, add the data (your DataTable
for example) to a session object, or make use of the GridView
control where you simply do: 例如,将数据(例如,您的
DataTable
)添加到会话对象,或者在简单执行以下操作的地方使用GridView
控件:
myGridView.DataSource = dt;
myGridView.DataBind();
and because you are reusing the same thing, you can use a User Control that has the myGridView
on it. 并且由于您正在重复使用同一事物,因此可以使用上面带有
myGridView
的用户控件 。
This way, you're not handling HTML format on your Code Handling page and you are reusing a control where, tomorrow, if anything happens, you can easily change just one file, and all places that has that control will be modified automatically. 这样一来,你就不是你的代码处理页面上处理的HTML格式和你重用控制,其中,明天,如果有什么事情发生,你可以很容易地改变只有一个文件,而且具有控制所有的地方将被自动修改。
If you still don't care much about decent programming rules, you can add your Literal control to, for example, a session object and call it on the second page: 如果您仍然不太关心体面的编程规则,则可以将Literal控件添加到例如会话对象中,然后在第二页上调用它:
LiteralControl lc = new LiteralControl(s1 + s2);
// Add control to a Session instance
Session["myLiteralControl"] = lc;
Div1.Controls.Add(lc);
in your other page: 在另一个页面中:
LiteralControl lc = (Literal)Session["myLiteralControl"];
Div1.Controls.Add(lc);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.