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 t开发者_C百科hat 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
You can try something like this:
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true" onblur="validateSelection(this)" ...>
<asp:ListItem Text="" Value="" />
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server" style="display:none;" ... />
Your JavaScript function (not positive, but it should be close):
validateSelection = function(selectList){
var input = document.getElementById("<%=TextBox1.ClientID%>");
if (input){
input.style.display = selectList.selectedIndex > -1 ? "block" : "none";
if (selectList.selectedIndex > -1){
input.focus();
}
}
}
And in your code-behind:
string description = TextBox1.Text;
if (!String.IsNullOrEmpty(DropDownList1.SelectedValue))
description = DropDownList1.SelectedValue;
Enforce that the user has either an item selected in the dropdown, or valid value typed into the textbox, but not both. (Implement this using ASP validators, or your favorite home-grown approach). With that protection in place, it's easy:
Within your button event handler, evaluate whether ddlDescription has a selected value: if so, use it as the parameter (just like your code is already doing). If not, use the value of your text box.
精彩评论