Solved

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

Posted on 2014-12-17
1
296 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:mwmiller78
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now