Question

I got stuck here,

i have dynamic table that i want to print. So i make session to pass it into web control. Unfortunately, it doesn't run smooth.

Here are my code :

protected void bt_print_click(object sender, EventArgs e)
{
    StringWriter sw = new StringWriter();
    HtmlTextWriter w = new HtmlTextWriter(sw);
    panelBilling.RenderControl(w);
    string s = sw.GetStringBuilder().ToString();
    Session["ctrl"] = s;

    ClientScript.RegisterStartupScript(this.GetType(), "onclick", "<script language=javascript>window.open('Print.aspx?rep=1','PrintMe','height=680px,width=1024px,scrollbars=1');</script>");

}

And the Print.aspx.cs code:

protected void Page_Load(object sender, EventArgs e)
{
    Control ctrl = (Control)Session["ctrl"];
    PrintHelper.PrintWebControl(ctrl);
}

I always got error message :

"Unable to cast object of type 'System.String' to type 'System.Web.UI.Control'."

on

(Control)Session["ctrl"]

part. I have use this method many time and no problems before. Anyone has any idea? Thanks.

Was it helpful?

Solution

This just doesn't make sense, casting a string value to a Control.

As far as I can see, you are not inserting a Control into the Sessiosn, not even the Control.ToString() value, which also wouldn't be working.

Before continuing, it might be usefull to check which type is retrieved from the Session without casting anything (remark: this is only possible with .NET 3.5 or higher):

var sessionValue = Session["ctrl"];

When doing so, you will find out the value is of type object, containing the value:

"<div>\r\n\r\n</div>"

The above output is not a Control, but a string/html value.

There are two solution paths which u can follow:

  • Change the "SET" call on the Session
  • Change the "GET" call on the Session

In the second example, you keep to the fact that you are inserting a string and therefor you would like to read out that string value:

string sessionValueAsString = string.Empty;
Object sessionValue = Session["ctrl"];

if (sessionValue != null)
    sessionValueAsString = sessionValue.ToString();

The above example could suit ur needs, never the less I'm guessing you are in the need to save an object of type "Control" in the session. This can be achieved like so:

MyControl myControl = new MyControl {Title = "My custom control"};
Session["myControl"] = myControl;
// Read the Session value cross Postback
MyControl mySessionControl = (MyControl)Session["myControl"];

Apart from the above example, I'm not 100% sure what you are trying to do. My guess is you made a mistake inserting the wrong value in the Session.

One remark: I'm not a fan of storing the WebControls in the session, it would be cleaner to store your data objects in the Session (e.g. store a Customer class instead of the CustomerUserControl, this way you can read out the Session and populate the required controls using the data found in the Session).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top