Need to delete the selected item from the dropdown from the database with the button click.Also when user clicks the delete button i need to show the alert or confirm dialog box for confirmatiuon that user realy want to delete that item....plz help me ...

Recommended Answers

All 3 Replies

i want to fetch data in a DropDownList from Sql server and delete the selected data from dropdownlist...

Well did you write any code yet? If I understand you right, you want to populate a dropdownlist from a database. Then you want to remove the selected item from the dropdownlist on a button click. You also want a dialog box to show before the removal of the selected item.

First look at databinding for your dropdownlist. Another thing if you are binding your dropdownlist in the page_load event make sure you put it in a if (!IsPostBack). This way each time you do a postback deleting a item from the dropdownlist it will not repopulate the list. So write some code and I or someone else will surely help.

I can't sleep so I will do some of this for you. First make your dropdownlist add a button and add a call to a javascript fuction.

<asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>
        <br /><br />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="javascript:return wait();" onclick="Button1_Click" />

Here is a quit javascript comfirm.

<script type="text/javascript">
    
      function wait()
      {
        if (confirm("Are you sure you want to delete this from the dropdownlist?") == true)
        return true;
        else
        return false;
      }
    
    </script>

Now lets make a generic list in the page load, you would want to load your droplistlist from your database.

using System.Collections.Generic;

public partial class GenericList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<string> myList = new List<string>();
            myList.Add("Item One");
            myList.Add("Item Two");
            myList.Add("Item Three");
            myList.Add("Item Four");
            DropDownList1.DataSource = myList;            
            DropDownList1.DataBind();
        }
    }

Now lets do the button click event.

protected void Button1_Click(object sender, EventArgs e)
    {
        DropDownList1.Items.Remove(DropDownList1.SelectedItem);
    }

There you go I don't think I forgot anything but like I said above you would fill your genericlist from your database then bind it to your dropdownlist. The idea is the same...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.