Solved

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

Posted on 2014-12-17
1
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 29

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

Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

627 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