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 wish 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
webformAduring the request towebformB. Once the user is redirected, assume thatwebformAis gone.Instead, when you’re about to perform the
Response.Redirect()towebformB, include in some way the value fromwebformAthat you wish to pass along. The easiest way to do this will be on the query string. Something like:Then, in
webformByou can access the value: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 sureRequest.QueryString["name"]contains a value before using it inwebformB, 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.