Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-12-17
1
Medium Priority
?
316 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 29

Accepted Solution

by:
sammySeltzer earned 2000 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Loops Section Overview
Suggested Courses

782 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