簡體   English   中英

C#如何為selectedValue = null設置dropDownList的默認值

[英]C# how to set dropDownList default value for selectedValue = null

我有一個dropdownlist1 ,它有6個收集項,包括Select One 我想要的是這個ddl被設置為Selectedvalue = null。 但我得到的是我的ddl總是選擇Select One作為其初始值。 Select One ddl屬性:false。 如何將此ddl設置為初始選定值= null?

<asp:DropDownList ID="DropDownList1" runat="server" Visible="False" Width="146px">
     <asp:ListItem>Ceiling Speaker</asp:ListItem>
     <asp:ListItem>Remote Microphone</asp:ListItem>
     <asp:ListItem>Digital Source Player</asp:ListItem>
     <asp:ListItem>Remote paging Console</asp:ListItem>
     <asp:ListItem>Modular Mixer</asp:ListItem>
     <asp:ListItem>Select One</asp:ListItem>
</asp:DropDownList>


if (String.IsNullOrEmpty(txtSearchProductname.Text))
{
   if (DropDownList1.SelectedValue == null)
   {
       txtProductName.Text = "";
   }
   else
   {
       SqlProductmaster.InsertParameters["ProductName"].DefaultValue = DropDownList1.SelectedValue.ToString();
   }
}
else
{
    SqlProductmaster.InsertParameters["ProductName"].DefaultValue = txtProductName.Text;
}

您可以添加“空”值項:

<asp:DropDownList ID="DropDownList1" runat="server" Width="146px">
    <asp:ListItem Selected="True"></asp:ListItem>
    <asp:ListItem>Ceiling Speaker</asp:ListItem>
    <asp:ListItem>Remote Microphone</asp:ListItem>
    <asp:ListItem>Digital Source Player</asp:ListItem>
    <asp:ListItem>Remote paging Console</asp:ListItem>
    <asp:ListItem>Modular Mixer</asp:ListItem>
    <asp:ListItem>Select One</asp:ListItem>
</asp:DropDownList>

然后你會檢查

if (string.IsNullOrEmpty(DropDownList1.SelectedValue))
<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" runat="server"> 
    <asp:ListItem Text="(Select a State)" Value="" />   
    <asp:ListItem>Ceiling Speaker</asp:ListItem>
    <asp:ListItem>Remote Microphone</asp:ListItem>
    <asp:ListItem>Digital Source Player</asp:ListItem>
    <asp:ListItem>Remote paging Console</asp:ListItem>
    <asp:ListItem>Modular Mixer</asp:ListItem>
    <asp:ListItem>Select One</asp:ListItem>    
</asp:DropDownList>

下拉列表的SelectedValue永遠不會為null。 它可能是空字符串,但它永遠不能為空...

以這種方式設置項目會更好

<asp:ListItem Text="Select One" Value=""></asp:ListItem>

Dropdown的SelectedValue也是一個字符串屬性,因此最好使用string.IsNullOrEmpty檢查相同,因為它不能為null,將值留空,將其視為null,並在TextValue部分中為其他值重復相同的值

使選擇值=“”為空,並將其傳遞給您的存儲過程,如下所示

 dt = ta.GetData(
            String.IsNullOrEmpty(DropDownList1.SelectedValue)? null : DropDownList1.SelectedValue,
            String.IsNullOrEmpty(DropDownList2.SelectedValue) ? null : DropDownList2.SelectedValue,
            String.IsNullOrEmpty(DropDownList3.SelectedValue) ? null : DropDownList3.SelectedValue, null);

赫拉在lsit中添加一個默認值...

<asp:DropDownList ID="DropDownList1" runat="server" Width="146px">
<asp:ListItem Selected="True">Select One</asp:ListItem>
<asp:ListItem>Ceiling Speaker</asp:ListItem>
<asp:ListItem>Remote Microphone</asp:ListItem>
<asp:ListItem>Digital Source Player</asp:ListItem>
<asp:ListItem>Remote paging Console</asp:ListItem>
<asp:ListItem>Modular Mixer</asp:ListItem>
<asp:ListItem>Select One</asp:ListItem>

但嘗試像seleted項目而不是selectedvalue因為你沒有在列表項中定義的值..

    if (string.IsNullOrEmpty(DropDownList1.SeletedItem))  

對於文本框也

  txtProductName = DropDownList1.SeletedItem).ToString();

在SqlDataSource上使用CancelSelectOnNullParameter = False

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM