Problem with viewstate for listitems
-
Ive got an Onclick event which has the following code in: ViewState["items"] = lb1.SelectedItem.Value; Response.Redirect("Page.aspx?sql=items"); on the pageload it should then display the values chosen from listbox if the page has posted back. However when I click the Button it shows an Object Reference Error for the following line: ViewState["items"] = lb1.SelectedItem.Value; why is this? thanks
Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"
-
Ive got an Onclick event which has the following code in: ViewState["items"] = lb1.SelectedItem.Value; Response.Redirect("Page.aspx?sql=items"); on the pageload it should then display the values chosen from listbox if the page has posted back. However when I click the Button it shows an Object Reference Error for the following line: ViewState["items"] = lb1.SelectedItem.Value; why is this? thanks
Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"
Seems to me that there there are no selected items when the button is click'ed.
if(lb1.SelectedItem != null)
{
ViewState["items"] = lb1.SelectedItem.Value;
}If the listbox allso has multiselect = true, you will allso get a problem. if so you should use
string strValues = "";
foreach(ListItem li in lb1.Items)
{
if(li.Selected)
{
strValuse+=li.Value;
}
}Hope it helps Thomas
modified on Monday, October 6, 2008 10:31 AM
-
Seems to me that there there are no selected items when the button is click'ed.
if(lb1.SelectedItem != null)
{
ViewState["items"] = lb1.SelectedItem.Value;
}If the listbox allso has multiselect = true, you will allso get a problem. if so you should use
string strValues = "";
foreach(ListItem li in lb1.Items)
{
if(li.Selected)
{
strValuse+=li.Value;
}
}Hope it helps Thomas
modified on Monday, October 6, 2008 10:31 AM
Thanks for the post but it doesnt work Ive got string strValues = ""; foreach (ListItem li in listbox1.Items) { if (li.Selected) { strValues += li.Value; } } Session["items"] = strValues; in the onclick event and then on pageload I have: lbitems.Text = "Items selected: " + Session["items"].ToString(); The label is shown as blank. I really dont understand why this is happening, very weird! Any more help would be great thanks Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"
-
Thanks for the post but it doesnt work Ive got string strValues = ""; foreach (ListItem li in listbox1.Items) { if (li.Selected) { strValues += li.Value; } } Session["items"] = strValues; in the onclick event and then on pageload I have: lbitems.Text = "Items selected: " + Session["items"].ToString(); The label is shown as blank. I really dont understand why this is happening, very weird! Any more help would be great thanks Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"
Session is not updated at the time you go to the next page so you should us ViewState or HttpContext.Current.Items["somevalues"] If you have multiselect, you should splitt the valuse by somthing, so you can retrive them Something lik:
if(listbox1.SelectedItem != null){
string strValues = "";
for (int i = 0; i < listbox1.Items.Count;i++)
{
if(listbox1.Items[i].Selected){
strValues += listbox1.Items[i].Value + ",";
}
strValues = strValues .Remove(strValues.Lenght - 1, 1); // To remove the last seperator
}HttpContext.Current.Items["theItemsValues"] = strValues;
}on the pageload of the new page:
if(HttpContext.Current.Items.Contains("theItemsValues")) {
lbitems.Text= "Items selected: " +HttpContext.Current.Items["theItemsValues"].ToString();
}If you then actually needs the items values:
string[] strListOfValues = HttpContext.Current.Items["theItemsValues"].ToString().Splitt(',');
modified on Tuesday, October 7, 2008 5:45 AM
-
Seems to me that there there are no selected items when the button is click'ed.
if(lb1.SelectedItem != null)
{
ViewState["items"] = lb1.SelectedItem.Value;
}If the listbox allso has multiselect = true, you will allso get a problem. if so you should use
string strValues = "";
foreach(ListItem li in lb1.Items)
{
if(li.Selected)
{
strValuse+=li.Value;
}
}Hope it helps Thomas
modified on Monday, October 6, 2008 10:31 AM
Still the problem persists. This is one of the strangest I have ever seen. Cant understand it.
Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"
-
Session is not updated at the time you go to the next page so you should us ViewState or HttpContext.Current.Items["somevalues"] If you have multiselect, you should splitt the valuse by somthing, so you can retrive them Something lik:
if(listbox1.SelectedItem != null){
string strValues = "";
for (int i = 0; i < listbox1.Items.Count;i++)
{
if(listbox1.Items[i].Selected){
strValues += listbox1.Items[i].Value + ",";
}
strValues = strValues .Remove(strValues.Lenght - 1, 1); // To remove the last seperator
}HttpContext.Current.Items["theItemsValues"] = strValues;
}on the pageload of the new page:
if(HttpContext.Current.Items.Contains("theItemsValues")) {
lbitems.Text= "Items selected: " +HttpContext.Current.Items["theItemsValues"].ToString();
}If you then actually needs the items values:
string[] strListOfValues = HttpContext.Current.Items["theItemsValues"].ToString().Splitt(',');
modified on Tuesday, October 7, 2008 5:45 AM
Thanks, I will try that tomorrow and will get back to you, thnaks for the help!
Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"
-
Still the problem persists. This is one of the strangest I have ever seen. Cant understand it.
Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"
Are you showing List item with its id and description ? Check what is value and what is text for the list item Try to debug the application whats happening there Can you tell me what you have to do ?
Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... " Check My Blog
-
Ive got an Onclick event which has the following code in: ViewState["items"] = lb1.SelectedItem.Value; Response.Redirect("Page.aspx?sql=items"); on the pageload it should then display the values chosen from listbox if the page has posted back. However when I click the Button it shows an Object Reference Error for the following line: ViewState["items"] = lb1.SelectedItem.Value; why is this? thanks
Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"
Session["items"] = lb1.SelectedItem.Value; Response.Redirect("Page.aspx?sql=session["items"].Tostring()); use Session state instread of Viewstate
-
Session["items"] = lb1.SelectedItem.Value; Response.Redirect("Page.aspx?sql=session["items"].Tostring()); use Session state instread of Viewstate
Right, below is my code. It is still not working, I thought a new day would bring new answers but no. PageLoad: lbitems.Text = "Items selected: " + Session["items"]; Button(This posts back to the same page): if (listbox1.SelectedItem != null) { string strValues = ""; for (int i = 0; i < listbox1.Items.Count - 1; i++) { if (listbox1.Items[i].Selected) { strValues += listbox1.Items[i].Value + ","; } } strValues += listbox1.Items[listbox1.Items.Count - 1].Value; Session["items"] = strValues; } Response.Redirect("~/main/Page.aspx?sql=items"); Why is this not working? It doesnt display any selected values. thanks Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"
-
Right, below is my code. It is still not working, I thought a new day would bring new answers but no. PageLoad: lbitems.Text = "Items selected: " + Session["items"]; Button(This posts back to the same page): if (listbox1.SelectedItem != null) { string strValues = ""; for (int i = 0; i < listbox1.Items.Count - 1; i++) { if (listbox1.Items[i].Selected) { strValues += listbox1.Items[i].Value + ","; } } strValues += listbox1.Items[listbox1.Items.Count - 1].Value; Session["items"] = strValues; } Response.Redirect("~/main/Page.aspx?sql=items"); Why is this not working? It doesnt display any selected values. thanks Deliver yesterday, code today, think tomorrow. "http://www.heuse.com/cphumor.htm"