2011-06-23 48 views
7

Tôi có một số dropdownlist1 có 6 mục bộ sưu tập bao gồm Select One. Những gì tôi muốn là ddl này được thiết lập để Selectedvalue = null. Nhưng những gì tôi nhận được là ddl của tôi luôn luôn chọn Select One làm giá trị ban đầu của nó. Thuộc tính ddl của tôi cho Select One được chọn: sai. Làm cách nào để đặt giá trị ddl này thành giá trị được chọn ban đầu = null?C# cách đặt giá trị mặc định dropDownList cho selectedValue = 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; 
} 

Trả lời

3

Bạn có thể thêm một 'Empty' item giá trị:

<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> 

Sau đó, bạn sẽ kiểm tra cho

if (string.IsNullOrEmpty(DropDownList1.SelectedValue)) 
+0

Tôi đã làm như bạn nói nhưng khi tôi chèn một cái gì đó trong txtProductName, nó không được hiển thị trong trình duyệt. –

+0

Tuy nhiên nó là lạ, vấn đề của tôi liên quan đến câu hỏi này đang được giải quyết bằng cách chỉ thay thế txtProductName.Text = ""; với SqlProductmaster.InsertParameters ["ProductName"]. DefaultValue = txtProductName.Text; Làm việc như sự quyến rũ. –

2

Đã chọnGiá trị danh sách thả xuống sẽ không bao giờ rỗng. Nó có thể là chuỗi rỗng, nhưng nó không bao giờ có thể được null ...

+0

Đây là câu trả lời đúng. –

3
<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> 
+0

@Alex Vẫn không hiển thị.Bạn thấy, ddl của tôi bằng cách nào đó ảnh hưởng đến textbox txtProductName. nó nên hiển thị những gì tôi gõ vào txtProductName đầu tiên. Tôi có nên để nó được, và tôi chọn mục ddl, sau đó selectedValue sẽ điền vào. Hoặc, tôi có thể để trống cả hai, vẫn nhận được kết quả như tôi muốn. –

1

Sẽ tốt hơn nếu đặt các mục trong thời trang này

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

Ngoài ra SelectedValue của Dropdown là thuộc tính chuỗi nên kiểm tra tốt hơn bằng cách sử dụng string.IsNullOrEmpty vì nó không thể rỗng, để trống giá trị mà bạn muốn xem là rỗng và lặp lại cùng giá trị trong phần TextValue cho những người khác

0

Hera thêm một giá trị mặc định trong 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> 

nhưng thử một cái gì đó giống như mục seleted thay vì SelectedValue beacuse u have'nt giá trị quy định tại các mục danh sách ..

if (string.IsNullOrEmpty(DropDownList1.SeletedItem)) 

Đối textbox cũng

txtProductName = DropDownList1.SeletedItem).ToString(); 
+0

txtProductName khi điền vào không hoạt động. –

-1

Sử dụng CancelSelectOnNullParameter = False trên SqlDataSource

0

Tận dụng chọn value = "" đó là trống rỗng và chuyển thông tin này đến thủ tục lưu trữ của bạn như thế này

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); 
Các vấn đề liên quan