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 ...

6 Years
Discussion Span
Last Post by Freon22

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.

Edited by Freon22: n/a


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">
        <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;
        return false;

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;            

Now lets do the button click event.

protected void Button1_Click(object sender, EventArgs e)

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...

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.