简体   繁体   English

如果没有从下拉列表中选择任何值,则允许文本框添加到数据库VB.net

[英]If no value selected from dropdown, allow textbox to add to database VB.net

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

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): 您的JavaScript函数(不是肯定的,但应该很接近):

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). (使用ASP验证程序或您最喜欢的本地方法来实现)。 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). 在按钮事件处理程序中,评估ddlDescription是否具有选定的值:如果是,则将其用作参数(就像您的代码已在执行一样)。 If not, use the value of your text box. 如果不是,请使用文本框的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM