Click here to Skip to main content
15,868,080 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello all,

I have added listbox in my code
XML
<asp:ListBox ID="LststateList" runat="server" Width="180px" SelectionMode="Single"
 Height="350px"CssClass="textbox3"></asp:ListBox>


and onclick event of Linkbutton i am verifying that listbox have selected any value or not below is my linkbutton
C#
<asp:LinkButton ID="lnkgo" runat="server" OnClick="Go_Click" Text="Go to this page"></asp:LinkButton>


C#
<pre lang="cs">protected void Go_Click(object sender, EventArgs e)
        {
string sb = string.Empty;
           for (int i = 0; i < LststateList.Items.Count; i++)
            {
                if (LststateList.Items[i].Selected == true)
                {
                    sb = Convert.ToString(LststateList.Items[i].Value);
                }
            }
            if (string.IsNullOrEmpty(sb))
            {
               -----            }}



here problem is the,when i select any value from list box and click on linkbutton then Listbox always display selected value=false,
I dont know reason why this happend,and its occured in IE Only,

and one thing that I have added all above controls in
C#
<div></div> 
tag.

help me about that.
Posted
Updated 23-Aug-12 2:55am
v2
Comments
pramod.hegde 23-Aug-12 8:20am    
Use LststateList.SelectedItem
madhuri@mumbai 23-Aug-12 8:24am    
i have used it its always give result null

Your code seems perfectly okay.

You have a different problem. It is page postback.
What happens, when you click a server button, the page postback occurs and I am sure you have some code of loading list box in page_load event.

Try this
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
          //fill up your listbox here
    }
}


This will work.

cheers
 
Share this answer
 
v2
Comments
madhuri@mumbai 23-Aug-12 8:37am    
yes,i have the code in page_load,
but i have already added above condition,it doesn't execute coed in page_load becoze of above condition,
Sandip.Nascar 23-Aug-12 8:41am    
Please debug your code, specially the area where the list box populate code is written. In button click, is there any possibility of executing the code again?
Sandip.Nascar 23-Aug-12 8:44am    
one thing to mention though not related with the actual problem,
ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script language = "'javascript'">alert('Please select state from List')</script>");

Here I seeextra double quote "'javascript'".
madhuri@mumbai 23-Aug-12 8:48am    
I have debug my code there is no any possibility,
but one thing that in Firefox i got selected value but not in IE bowser ,so whats the prob i don't understand
Sandip.Nascar 23-Aug-12 9:12am    
What IE version you are using? This is suppose to run in IE 8 and above. Not sure on others.
Hi,
Please try

C#
if (LststateList.Items[i].Selected == true)
                {
                    sb = LststateList.Items[i].Value.ToString();
                }


It is working for me.

Thank you.
 
Share this answer
 
Comments
madhuri@mumbai 23-Aug-12 8:42am    
thanks for rply,
but LststateList.Items[i].Selected is always false,so it doesn't go inside codition,
that my exect problem that always selected value return false.
Sandip.Nascar 23-Aug-12 8:43am    
what is the difference with this code that Madhuri posted?
I agree to Sandip, your code is fine.

In ASP.NET Page Life Cycle, the Page_Load will be executed first and then the event button click.

So you need to validate adding the condition if(!Page.IsPostBack).

C#
if (!IsPostBack)
{
    var query = your query;

    ListBox1.DataSource = query;
    ListBox1.DataBind();
}
 
Share this answer
 
Comments
madhuri@mumbai 23-Aug-12 9:15am    
anuja,
I am saying that ,I have alredy this condition in my code,
Anuja Pawar Indore 23-Aug-12 9:23am    
After bind you are getting data in the listbox?
madhuri@mumbai 24-Aug-12 1:20am    
yes i got data in listbox after bind,
I got selected value from list in fireFox but not in IE.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900