데이터베이스에 새 제목을 추가하는 드롭 다운 목록이 있습니다. 그러나 사용자가 입력 할 수있는 텍스트 상자를 추가하고 싶습니다. 드롭 다운에 아무 것도 추가하지 않으면 새 제목이 추가됩니다.드롭 다운에서 값을 선택하지 않은 경우 텍스트 상자를 데이터베이스에 추가 할 수 있음 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
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
원래 게시물을 편집해야했습니다. 내 드롭 다운 목록에는 데이터베이스에서 채워지므로 항상 옵션이 선택된다는 것을 알지 못했습니다. 드롭 다운이 어떻게 이렇게 작동 할 수 있는지 알고 있습니까? – jlg
'AppendDataBoundItems = "true"'를 설정하고 빈 항목을 첫 번째 위치에 추가하십시오. –
제안 해 주셔서 감사합니다. 오, 그리고 appenddatabounditems 중복을 생성했다지만 드롭 다운에 조금 더 추가하여 그 문제를 극복 할 수 있었다. – jlg