2011-09-26 111 views
0

我有一個下拉列表,將添加新的標題到我的數據庫,但我想添加一個文本框,將允許用戶鍵入一個如果下拉列表中沒有任何內容對於他們正在添加的內容有任何意義,則可以使用新標題。如果沒有從下拉列表中選擇的值,允許文本框添加到數據庫VB.net

我有下拉動態從數據庫中獲取標題。不幸的是,這意味着下拉列表中的第一個值不是默認值,表示「選擇一個選項」。當數據從數據庫中拉出時,我不知道如何讓下拉列表中的第一個選項列爲「選擇」。我無法添加選擇一個選項到數據庫,這將如何工作?

什麼可以添加到我的代碼隱藏,這將允許文本框插入到數據庫中,如果下拉列表不插入任何東西?現在我沒有任何文本框的代碼隱藏,但我確實有下拉列表插入正確的信息。

<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 

工作代碼

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 

回答

1

你可以嘗試這樣的事情:

<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;" ... /> 

你的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(); 
     }    
    } 
} 

而在你的後臺代碼:

string description = TextBox1.Text; 
if (!String.IsNullOrEmpty(DropDownList1.SelectedValue)) 
    description = DropDownList1.SelectedValue; 
+0

我不得不編輯原始帖子。我沒有意識到,我的下拉列表將始終選擇一個選項,因爲它正從數據庫填充。你有什麼想法如何下拉這樣工作? – jlg

+1

只需設置AppendDataBoundItems =「true」'並在第一個位置添加一個空白項目。 –

+0

感謝您的建議!我編輯原始帖子的代碼,現在的作品:)哦,appenddatabounditems產生了重複,但我能夠通過添加更多的下拉菜單來解決這個問題。 – jlg

0

強制用戶具有無論是在下拉選擇了一個項目,或鍵入到文本框有效值,而不是兩者。 (使用ASP驗證器或您最喜歡的自制方法來實現此目的)。有了這種保護,很容易:

在您的按鈕事件處理程序中,評估ddlDescription是否具有選定的值:如果是,請將其用作參數(就像您的代碼已經在做)。如果沒有,請使用您的文本框的值。

相關問題