• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

Dropdownlist values

how can I default the selected value of my dp down list to the current status in my db table...

I tried selected value, but problem is , that whenever there is a null value my web oage trows an error!!
<asp:DataList ID="DataList1" runat="server" BackColor="White" 
            BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" 
            DataKeyField="Id" DataSourceID="SqlDataSource1" GridLines="Both"
          oneditcommand="DataList1_EditCommand" 
            OnUpdateCommand="DataList1_UpdateCommand" 
            OnCancelCommand="DataList1_CancelCommand" style="font-size: x-small" 
            Width="96px" Height="16px" onitemdatabound="DataList_ItemDataBound"    >
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" >
                  <asp:ListItem Text="Assigned" Value="Assigned">Assigned</asp:ListItem> 
                    <asp:ListItem Text="Available" Value="Available">Available</asp:ListItem>
                    <asp:ListItem Text="BR" Value="BR">BR</asp:ListItem>
                  
                </asp:DropDownList>
            
                <tr>   
                <td>     
                <asp:LinkButton ID="lnkUpdate" runat="server" CommandName="update">Update</asp:LinkButton> 
               <asp:LinkButton ID="lnkCancel" runat="server" CommandName="cancel">Cancel</asp:LinkButton> 
                  </td></tr>
            </EditItemTemplate>
            <FooterStyle BackColor="White" ForeColor="#000066" />
            <FooterTemplate>
                Census
                  <asp:TextBox ID="txt_CensusNT1" runat="server" Width="35px" ></asp:TextBox>
                Comment
                <asp:TextBox ID="txt_CommentNT1" runat="server" Width="70px" 
                    TextMode="MultiLine"></asp:TextBox>
                <asp:Button ID="Button1" runat="server" OnClick="btnApply_Click" 
                    style="font-size: x-small" Text="Save" />
                  <br></br>
  </FooterTemplate>

Open in new window

0
TonyReba
Asked:
TonyReba
3 Solutions
 
Paul JacksonCommented:
Using SelectedValue is the correct way of doing it.
To cope with occurrances of NULL add another listitem that has value = ""

i.e.

<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("CurrentStatus")%>' > 
                  <asp:ListItem Text="None" Value=""></asp:ListItem>
                  <asp:ListItem Text="Assigned" Value="Assigned"></asp:ListItem>  
                    <asp:ListItem Text="Available" Value="Available"></asp:ListItem>
                    <asp:ListItem Text="BR" Value="BR"></asp:ListItem>
                   
                </asp:DropDownList>
0
 
Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
Hi,

The trick is that you can add items to the dropdown list by declaration. The second part of the trick is to add the DB entrys to the declarated entrys by AppenddataboundItems. Take care to set the value to "" cause the bind gives also back a "" if the field contains NULL

<asp:DropDownList ID="DropDownList1" runat="server"  SelectedValue='<%# Bind("FieldName") %>' AppendDataBoundItems=true>


or do selection dynamically on edit event

for more details follow the link
http://weblogs.asp.net/hpreishuber/archive/2005/08/07/421805.aspx
http://www.asp.net/data-access/tutorials/customizing-the-datalist-amp-rsquo-s-editing-interface-cs

Thanks
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Or in your SqlDataSource select command:
SelectCommand="SELECT ColA, ColB, ColC, ISNULL(CurrentStatus, 'Available') AS CurrentStatus FROM [MyTable]"

Open in new window


And your DropDownList:
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("CurrentStatus") %>'>
    <asp:ListItem Text="Assigned" Value="Assigned">Assigned</asp:ListItem>
    <asp:ListItem Text="Available" Value="Available">Available</asp:ListItem>
</asp:DropDownList>

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TonyRebaAuthor Commented:
Well my concern is this,

I am giving the end user the ability to update the status of a room # , so in the EditItemTemplate I have several updatable fields one is status , other a comment, So If they choose to edit a room to edit a comment (textbox) the dropdown list is not selecting the current status, which can cause lot of issues,,
So how would I handle this ???
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Tony did you try the example? it have that behavior. Assuming that nulls meant "Available"
0
 
TonyRebaAuthor Commented:
How exactly does this works, so I am supposing that if there is a NULL value it will show Available as the default selected value on the dropdown??
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Yes
0
 
TonyRebaAuthor Commented:
great, will try tomorrow
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Example:
SELECT
       ISNULL(MyTableColumn, ValueToReturnIfNull) AS MyTableColumn
FROM
       MyTable

Open in new window


The second param must be the same type of your column (or convertible).

More info:
ISNULL (Transact-SQL)
0
 
TonyRebaAuthor Commented:
yv989c Thanks, that worked Great!!!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now