开发者

Accesing Server Control from other webform

I have 2 webforms with 1 ListBox Control on each of them.

How do I access the Listbox that's located on webformA from webformB?

For example I wish to declare something like this string name = ListBoxWebformA.SelectedValue.ToString(); on WebFormB, so that I can work with that value in the code of WebFormB.

The ListBox on WebFormA lists several names. When I select a name from the listbox and press the OK button I call Response.Redirect("~/WebFormB.aspx");

So from WebFormB I w开发者_StackOverflow社区ish to access this "name" by putting the selected value into a string.


Based on your edit, the easiest (possibly best) way to go about doing this will not be to try to maintain a stateful instance of webformA during the request to webformB. Once the user is redirected, assume that webformA is gone.

Instead, when you're about to perform the Response.Redirect() to webformB, include in some way the value from webformA that you wish to pass along. The easiest way to do this will be on the query string. Something like:

Response.Redirect(string.Format("~/WebFormB.aspx?name={0}", HttpUtility.UrlEncode(ListBoxWebformA.SelectedValue.ToString())));

Then, in webformB you can access the value:

string name = Request.QueryString["name"];

Note that you'll want to do some error checking, etc. Make sure the value is actually selected before appending it to the redirect URL on webformA, make sure Request.QueryString["name"] contains a value before using it in webformB, etc.

But the idea in general is to pass that value along, by query string or POST value or Session value or some other more stateful means, when redirecting from one form to the other.


I guess you have to resort to either passing the value from A to B in the query string or storing the value in Session and reading afterwards.

So would be a

Response.Redirect(string.Format("~/WebFormB.aspx?YourVariable={0}",HttpUtility.UrlEncode(ListBoxWebformA.SelectedValue));  

and you can read it in Form B like

Request.QueryString["YourVariable"]

If the values are not sensitive this approach above would be the best.

If they are... To store in Session:

Session["YourVariable"] = ListBoxWebformA.SelectedValue

And to read...

if (Session["YourVariable"] != null) {
    var listAValue =  Session["YourVariable"].ToString()
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜