Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Professionals
|
ASP.NET 3.5 Professionals If you are an experienced ASP.NET programmer, this is the forum for your 3.5 questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Professionals section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 11th, 2010, 05:59 PM
Authorized User
 
Join Date: Mar 2009
Posts: 74
Thanks: 5
Thanked 0 Times in 0 Posts
Default Your help needed, been on this issue for too long!

I have a web form, by the left side of the webform is a menu control, when the menu control is clicked, a usercontrol is dynamically loaded. This usercontrol has two ListBoxes, when an item in the first ListBox is slected, the selected index changed event is fired, which executes code to load values into the second ListBox.That is to say, the values that should be in the second ListBox depend on the selected index of the first ListBox.That workd fine, but the problem comes in when I submit the form, I noticed that the record was not inserted into the database. I traced it to the second ListBox, this is what happens, whatever index I select in the second ListBox, when I click the submit button, the selectedIndex is set to -1 I have been trying to solve this for days, I had to abandon for a while. someone told me that this is happening becos I did not load the second ListBox at page Load so the selectedvalu is lost at postback. Yes I do not Load the second ListBox at page Load becus its content should depend on the selected index of the first Listbox, both controls are on a dynamically loaded usercontrol. Will appreciate any help, thanks...

Last edited by ysfkay; July 11th, 2010 at 07:11 PM..
 
Old July 11th, 2010, 06:09 PM
Registered User
 
Join Date: Aug 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to avrail
Default

hay,
please be a ware that, every time your listbox will be regenerated from the beginning that because you've created it at the page load event,
you need to add your list box definition iniside
Code:
if(!IsPostBack)
{
     //then add your listbox definition here
}
 
Old July 11th, 2010, 07:00 PM
Authorized User
 
Join Date: Mar 2009
Posts: 74
Thanks: 5
Thanked 0 Times in 0 Posts
Default

Please can you come clear, I need a better explanation. And yes I noticed before now that if I load the List box inside the "if(!IsPostBack)" it doesnt get loaded, maybe its cuz am working on a dynamically loaded usercontrol and not directly on a web form, am not sure, but thats what happens, so what I do is that I load it in the pageLoad and then use the Clear() method to clear its values before loading whenever a post back occurs, however, that is for the first list box, I load the second ListBox when the selectedIndex changed of the second ListBox is fired. Please see code below

//This is the page_Load of a dynamically loaded user control
protected void Page_Load(object sender, EventArgs e)
{

LoadBlocks();//Loads First ListBox

if (!IsPostBack)
{
//If I put the LoadBlocks() here,the ListBox doesnt get loaded
}


}

//LoadBlocks method
protected void LoadBlocks()
{
ListBoxBlocks.Items.Clear();

foreach (var i in ControlLoader.GetBlocks())
{


ListBoxBlocks.Items.Add(i.BlockNumber.ToString());


}

}


//Selected Index of LoadBlocks which Loads the Second ListBox called //ListBoxShops

protected void ListBoxBlocks_SelectedIndexChanged(object sender, EventArgs e)
{
ListBoxShops.Items.Clear();
GetAvailableShops(lstBlocksKeys.ElementAt(ListBoxB locks.SelectedIndex));

foreach (var i in ControlLoader.GetAvailableShops(lstBlocks.ElementA t(ListBoxBlocks.SelectedIndex)))
{

ListBoxShops.Items.Add(i.ShopNumber.ToString());
}

}

Last edited by ysfkay; July 11th, 2010 at 08:05 PM..
 
Old July 11th, 2010, 07:55 PM
Registered User
 
Join Date: Aug 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to avrail
Default

hay,
actually your LoadBlocks should be loaded, there is something still missing,
at least the first time you visit your page load this is (!IsPostBack)
so it should be loaded
 
Old July 11th, 2010, 08:34 PM
Authorized User
 
Join Date: Mar 2009
Posts: 74
Thanks: 5
Thanked 0 Times in 0 Posts
Default Thanks

I appreciate the help, but the problem isnt solved
 
Old July 11th, 2010, 08:58 PM
Registered User
 
Join Date: Aug 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to avrail
Default

hay,
I understand that this is not solving your issue,
so i made a little sample, if it doesn't help then i will ask for more codes,

this is my first markup file, it contains 2 list boxes one of them are advisable

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="ListBoxBlocks" runat="server" AutoPostBack="True"
onselectedindexchanged="ListBoxBlocks_SelectedInde xChanged" Width="225px"></asp:ListBox>

<asp:ListBox ID="ListBoxShops" runat="server" Visible="False" Width="373px"></asp:ListBox>
</div>
</form>
</body>
</html>


and the second one is the code file .cs


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadBlocks();
}
}

protected void LoadBlocks()
{
ListBoxBlocks.Items.Clear();
string str = "Just a Demo";
foreach (var i in str.ToCharArray())
{
ListBoxBlocks.Items.Add(i.ToString());
}
}

protected void ListBoxBlocks_SelectedIndexChanged(object sender, EventArgs e)
{
ListBoxShops.Items.Clear();
//GetAvailableShops(lstBlocksKeys.ElementAt(ListBoxB locks.SelectedIndex));
string str = "Just a Demo";
int indexSelected = ListBoxBlocks.SelectedIndex;
char[] myList = str.ToCharArray();
//foreach (var i in ControlLoader.GetAvailableShops(lstBlocks.ElementA t(ListBoxBlocks.SelectedIndex)))
//{
ListBoxShops.Items.Add(myList[indexSelected].ToString());
//}
ListBoxShops.Visible = true;
}
}
}

try to check them
 
Old July 12th, 2010, 01:28 AM
Authorized User
 
Join Date: Mar 2009
Posts: 74
Thanks: 5
Thanked 0 Times in 0 Posts
Default Thanks

I tried it that way,but when I put the LoadBlocks inside (!IsPostBack) then
it does not get populated, please also be reminded that my ListBoxes are not directly on a webform, they are childs of a dynamically loaded usercontrol as I explained above, my main problem is why I get the selectedIndex of ListBoxShops set to -1 when I click on the submit button...Thanks alot.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Long Long int to bin walid C# 0 January 23rd, 2007 12:47 PM
Use of long long int jvanp C# 2005 2 September 20th, 2006 06:07 PM
Use of long long int jvanp C++ Programming 7 October 3rd, 2004 01:59 PM
string to "long long" without using atoll sarraju C++ Programming 2 August 4th, 2004 07:19 AM
Modifying long text in a long field sajsal Classic ASP Databases 1 February 20th, 2004 12:36 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.