I have a dropdown list that will add new titles to my database, however I would like to add a textbox that will allow the user to type in a new title if there is nothing in the dropdown that makes any sense for what they are trying to add.
I have the dropdown dynamically getting titles from the database. Unfortunately, this means that the first value in the dropdown list is not a default that says "Select an option." I don't know how to get the dropdown to have the first option listed as "Select" when the values are being pulled from the database. I can't add Select an option to the database so how would this even work?
What can I add to my codebehind that will allow the textbox to insert into the database if the dropdown list is not inserting anything? Right now I don't have any codebehind for the textbox but I do have the dropdown list inserting the correct information.
<li class="alternatingItem">
<asp:LinkButton ID="DescButton" runat="server">Description</asp:LinkButton>
<asp:Panel ID="DescPanel" runat="server" CssClass="modalPopup" Style="display:none">
<div class="PopupHeader">Add a Description</div>
Title:<asp:DropDownList ID="ddlDescription" runat="server" DataSourceID="dsNewDescription" DataTextField="Title" DataValueField="Title">
</asp:DropDownList><br />
New Title:<asp:TextBox ID="NewDescTitle" runat="server"></asp:TextBox><br />
Description:<asp:TextBox ID="Description" runat="server" TextMode="MultiLine">
</asp:TextBox><br />
<asp:Button ID="submitDescription" runat="server" Text="Submit" />
<asp:Button ID="CancelSubmitDesc" runat="server" Text="Cancel" />
</asp:Panel>
<asp:ModalPopupExtender ID="DescModal" runat="server" DropShadow="True"
DynamicServicePath="" Enabled="True" PopupControlID="DescPanel"
TargetControlID="DescButton">
</asp:ModalPopupExtender>
</li>
Protected Sub submitDescription_Click(ByVal sender As Object, ByVal e
As System.EventArgs) Handles submitDescription.Click
DescModal.Hide()
'SQL INSERT: Marketing Table
Dim strSQL As String = "INSERT INTO Picklist (Title, Data)
VALUES (@Title, @Data);
INSERT INTO Marketing
(ProductID, MarketingTypeID, MarketingTitle, MarketingData)
VALUES (@ProductID ,2, 'Description', scope_identity())"
Using cn As New SqlConnection
(System.Configuration.ConfigurationManager.ConnectionStrings
("LocalSqlServer").ConnectionString)
Using cmd As New SqlCommand(strSQL, cn)
cmd.Parameters.Add(New SqlParameter("@Title",
ddlDescription.SelectedValue))
cmd.Parameters.Add(New SqlParameter("@Data",
Description.Text))
cmd.Parameters.Add(New SqlParameter("@ProductID",
ProductID.Value))
cn.Open()
cmd.ExecuteNonQuery()
End Using
End Using
Response.Redirect(Request.RawUrl)
End Sub
WORKING CODE
Title:<asp:DropDownList ID="ddlDescription" runat="server"
DataSourceID="dsNewDescription" DataTextField="Title" DataValueField="Title"
enableViewstate="False" AutoPostBack="True" AppendDataBoundItems="True">
<asp:ListItem Text="Select below or enter new" Selected="True"></asp:ListItem>
If ddlDescription.SelectedValue <> "Select below or enter new" Then
cmd.Parameters.Add(New SqlParameter("@Title", ddlDescription.SelectedValue))
Else
cmd.Parameters.Add(New SqlParameter("@Title", NewDescTitle.Text))
End If