Solved

Best method to add new items to a databound dropdown menu in asp.net(VB)

Posted on 2014-12-17
1
298 Views
Last Modified: 2015-01-08
I'm looking for some opinions here really. I have an ASP.NET(VB) application with several dropdown menus populated from SQL database tables. Some of them are in datagrids and repeaters. What is the best method to add new items? Currently I have an option to add items on a separate page, but I'd like to have an "Add new" option. Is there a clean method that will add the value to the dropdown and the table without postback? Or if I have to postback, is there a nice looking way to do it in a popup window? Please no links to C# examples. AJAX/Java are ok if they are easy to implement into datagrids/repeaters.
0
Comment
Question by:Mike Miller
1 Comment
 
LVL 28

Accepted Solution

by:
sammySeltzer earned 500 total points
ID: 40507347
Hi, You can try this.

Tested and works like a dream for me:

'//Markup:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="addToDropdown.aspx.vb" Inherits="addToDropdown" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>asp.net DropDownList: how to add list item programmatically</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:Navy">DropDownList: Add List Item</h2>  
        <asp:Label   
            ID="Label1"  
            runat="server"  
            Font-Size="Medium"  
            ForeColor="Green"  
            >  
        </asp:Label>  
        <br />  
       <asp:DropDownList ID="ddlStates" runat="server" AutoPostBack="true" placeholder="Select a state..." Width="300px">
         <asp:ListItem Value=""></asp:ListItem>  
        </asp:DropDownList>  
        <br /><br />  
        <b style="color:Green">New State</b>  
        <asp:TextBox ID="TextBox1" runat="server" onclick="this.value='';" onblur="this.value='Select a state...';" >  
        </asp:TextBox>  
        <br />  
        <asp:Button ID="Button1" runat="server" Text="Add State" OnClick="Button1_Click" />  
    </div>  
    </form>  
</body>  
</html>  

Open in new window


'//Codebehind
Imports System.Collections
Imports System.Configuration
Imports System.Data
Imports System.Linq
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Xml.Linq
Imports System.Data.OleDb

Partial Class addToDropdown
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            PopulateStates()
            TextBox1.BackColor = System.Drawing.Color.OrangeRed
            TextBox1.ForeColor = System.Drawing.Color.Yellow
            Button1.Font.Bold = True
            Button1.ForeColor = System.Drawing.Color.Green
        End If
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim SQLstring As String = ""
        Dim li As New ListItem()
        li.Text = TextBox1.Text.ToString()
        ddlStates.Items.Add(li)
        SQLstring = "INSERT INTO States(StateName) VALUES(@statename) "
        'Response.Write(s)
        'Response.End()
        Dim connStr As String = ConfigurationManager.ConnectionStrings("allstringconstrng").ConnectionString
        Dim conn As New OleDbConnection(connStr)
        Dim cmd As New OleDbCommand(SQLstring, conn)
        cmd.Parameters.AddWithValue("@statename", TextBox1.Text)
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()

        Label1.Text = "State Added: " + TextBox1.Text.ToString()
    End Sub
    Public Sub PopulateStates()
        Dim cmd As New OleDbCommand("Select StateId, StateName from States", New OleDbConnection(ConfigurationManager.ConnectionStrings("allstringconstrng").ConnectionString))
        cmd.Connection.Open()

        Dim ddlValues As OleDbDataReader
        ddlValues = cmd.ExecuteReader()

        ddlStates.DataSource = ddlValues
        ddlStates.DataValueField = "StateId"
        ddlStates.DataTextField = "StateName"
        ddlStates.DataBind()

        cmd.Connection.Close()
        cmd.Connection.Dispose()
    End Sub

End Class

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question