1,105,386 Community Members

delete selective item from dropdown list from database using c# in asp.net

Member Avatar
rohitmanhas_12
Light Poster
25 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
rohitmanhas_12
Light Poster
25 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Freon22
Light Poster
48 posts since Feb 2010
Reputation Points: 2 [?]
Q&As Helped to Solve: 8 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
Freon22
Light Poster
48 posts since Feb 2010
Reputation Points: 2 [?]
Q&As Helped to Solve: 8 [?]
Skill Endorsements: 0 [?]
 
0
 

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article