Click here to Skip to main content
15,117,830 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
The user enters a link in textBox1 and hits the button and the code below prints it in a listbox but only once. If the user enters another URL or the same URL it does not get saved into the listbox

how can I take the user input multiple times on this button click and save it in the listbox?

What I have tried:

This is my code :

private void button1_Click(object sender, EventArgs e)
        {
            
            hyperlinks link1 = new hyperlinks();
            link1.hyperlink_id = 1;
            link1.hyperlink_name = label2.Text;
            link1.hyperlink_link = textBox1.Text;
            hll.Add(link1);
            listBox1.DataSource = hll;
            listBox1.DisplayMember = "hyperlink_name";
            listBox1.ValueMember = "hyperlink_id";
            
        }



I want to be able to take the inputs
link1.hyperlink_name = label2.Text;
link1.hyperlink_link = textBox1.Text;


multiple times from the user and display it in my listbox.

Rest of my code :

List<hyperlinks> hll = new List<hyperlinks>();

public class hyperlinks
       {
           public int hyperlink_id { get; set; }
           public string hyperlink_name { get; set; }
           public string hyperlink_link { get; set; }
       }


private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (hyperlinks link in hll)
            {
                if (listBox1.SelectedValue.ToString() == link.hyperlink_id.ToString())
                {
                    label6.Text = link.hyperlink_link;
                }
            }
        }
Posted
Updated 11-Oct-21 23:52pm
Comments
Richard MacCutchan 11-Oct-21 12:25pm
   
Every link you add has the same id. Maybe that is not correct.
candijen 11-Oct-21 12:53pm
   
ohh so I'll just remove the id bit and try again??
candijen 11-Oct-21 12:56pm
   
no it doesn't work
Richard MacCutchan 11-Oct-21 14:53pm
   
What doesn't work? I pointed out that you are using the same id value for every entry so you would check to see if that is what you intended. Just removing it in a "hope for the best", is rather pointless unless you know what the real problem is.

Quote:
...WebForms...
C#
List<hyperlinks> hll = new List<hyperlinks>();
It looks like you're storing your list as a field within your page. That won't work in WebForms - every time the user causes a post-back, a new instance of your page class is created, so the list will be empty.

If you want to persist the value between requests, you either need to store it in the session:
C#
private List<hyperlinks> hll
{
    get
    {
        var value = (List<hyperlinks>)Session["hll"];
        if (value == null)
        {
            value = new List<hyperlinks>();
            Session["hll"] = value;
        }
        
        return value;
    }
}
or store it in the form somehow - the usual approach is to use ViewState:
C#
private List<hyperlinks> hll
{
    get
    {
        var value = (List<hyperlinks>)ViewState["hll"];
        if (value == null)
        {
            value = new List<hyperlinks>();
            ViewState["hll"] = value;
        }
        
        return value;
    }
}
   
Comments
BillWoodruff 12-Oct-21 5:12am
   
+5
You just discovered the no-datasource-refresh when new Item added to DataSource behavior that most of us (ouch) discover.

The best way to fix this is just to use a 'BindingList [^]
// create and initialize at class level scope: 
private BindingList<hyperlinks > hll = new BindingList<hyperlinks>();
Move your code that assigns the DataSource out of the button click handler into the Form load event.

Using a BindingList, adding a new item will not affect the currently selected value.
   
Comments
Richard Deeming 12-Oct-21 4:54am
   
I don't think the BindingList<T> class works in ASP.NET WebForms. :)
BillWoodruff 12-Oct-21 5:11am
   
Hi, a quick look suggests it might work ? https://stackoverflow.com/a/2419512/133321

If you think the code i posted is unusable by the OP, i'll remove this; mea culpa, I have never worked with WebForms. Should have paid attention to the tags.

The 'ugly" way i used in WinForms before BindingList came along ... for adding Items at run-time ... involved setting the DataSource to null, then re-binding, and resetting the DisplayMember property.
Richard Deeming 12-Oct-21 5:16am
   
Interesting - I've never tried using the BindingList<T> in ASP.NET; I though it was only for desktop applications.

That thread suggests that your code might work, but I suspect it might still have the issue of the list being emptied out on each post-back. :)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



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