Solved

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

Posted on 2014-12-17
1
294 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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

9 Experts available now in Live!

Get 1:1 Help Now