Question

So I'm not really familiar with C# and I am trying to learn the basics, but it's still all vague to me. What I'm trying to do this: when I select one item in a Listbox it will get that selected items value and put it into a textbox (this is just for testing when i want to use the value for MSSSQL).

So here is my markup (.aspx code): <asp:ListBox ID="Listbox1" runat="server" AutoPostBack="True" onselectedindexchanged="lstArtiesten_SelectedIndexChanged"></asp:ListBox>

And here is my code-behind (.aspx.cs code):

protected void Listbox1_SelectedIndexChanged(object sender, EventArgs e)
{
    txtArtName.Text = Listbox1.SelectedItem.Text;
} 

As you can see AutoPostBack is on. I know whenever I click on one of the items it is redirected to the "protected void Page_Load(object sender, EventArgs e) {}", however when I try to do it without AutoPostBack, then it won't send my selected information. I'd like to use AutoPostBack, but I am not sure when or when not to use it).

My problem is when I select my item the page will be refreshed and my selected value will be lost, thus a null as result...

I've tryed working with the if(isPostBack){ }, but failed to understand the structure of C#.

I hope somebody could help me with this. Thanks in advance! Kind regards, Nkmol

Was it helpful?

Solution

Apologies if I have misunderstoood the question, but from the sounds of it, what's happening is that when you change the value in the Listbox, this causes a postback which in turn is running the code you use to populate the listbox initially. (I assume that you are doing this in code-behind since I don't see anything in your markup that is specifying the Items in the list)

So given this markup:

 <asp:ListBox ID="Listbox1" runat="server" AutoPostBack="True" 
     onselectedindexchanged="lstArtiesten_SelectedIndexChanged"></asp:ListBox>
     <asp:TextBox runat="server" id="txtArtName"></asp:TextBox>

your code-behind should look something like this:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //only call initialise during initial page load.
            initialise();
        }
    }

    protected void initialise()
    {
        //add some items to the list
        Listbox1.Items.Add("Something1");
        Listbox1.Items.Add("Something2");
        Listbox1.Items.Add("Something3");
        Listbox1.Items.Add("Something4");
        Listbox1.SelectedIndex = 0; //select the first item in the list, or whatever
    }

    protected void lstArtiesten_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtArtName.Text = Listbox1.SelectedItem.Text;
    }

Note how the initialise method is only called if we're not in Postback -i.e the initial page load. By checking for that state and not re-loading the Listbox, we allow ViewState to work it's [terrible, dark, evil] magic and restore the selection you made on the Listbox control.

Hope that helps.

OTHER TIPS

Are you familiar with the concept of ViewState? This is what allows ASP.NET controls to "remember" their values. I would recommend that you visit this tutorial and then, if you are still having issues, then ask/re-ask your question.

ASP.NET ViewState Tutorial

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top