Solved

Asp.Net Drop Down List Datasource

Posted on 2015-01-12
7
486 Views
Last Modified: 2015-01-28
HI

I have a details view that contains a dropdown list

<asp:DropDownList ID="ddlReasons1" runat="server" AppendDataBoundItems="True" DataSourceID="dsSusReasonEdit" DataTextField="REASONFORSUSPENSION"  ForeColor="Black" DataValueField="REASONFORSUSPENSIONID"  SelectedValue='<%# Bind("REASONFORSUSPENSIONID", "{0}") %>' Enabled="False"> 
                                                 <asp:ListItem>Choose Reason</asp:ListItem> 
                                             </asp:DropDownList>

Open in new window


The record set that runs the detailsview is set depending on an id that is set when clicking on an item in a formview.


Because my dropdown list  is databound  (   SelectedValue='<%# Bind("REASONFORSUSPENSIONID", "{0}") %>' )  I get an error if there is no results to bind to  and the REASONFORSUSPENSIONID is NULL

What's the easiest way to dynamically set the dropdown list binding?  

Basically I want to say that if the ID is 25 then databind the dropdown list.
0
Comment
Question by:edjones1
  • 2
  • 2
7 Comments
 
LVL 79

Expert Comment

by:David Johnson, CD, MVP
ID: 40545884
you have to check for a null return and if null then do not display the drop down list
0
 
LVL 30

Accepted Solution

by:
MlandaT earned 250 total points
ID: 40546019
In cases like this, I think it's better to move your code into the code-behind. That way, you can seperate some of this logic (i.e. figuring out what to do if the value is null) from your actual HTML markup. Something along the lines of:
If IsDBNull(myObject("REASONFORSUSPENSIONID")) Then
     ddlReasons1.SelectedIndex = 0 'your default value
Else
     ddlReasons1.SelectedValue = myObject("REASONFORSUSPENSIONID")
End If

Open in new window

If you really need to do this in the ASPX page, then try something like (using an inliine Iif):
SelectedValue='<%# Iif(IsDBNull(Eval("REASONFORSUSPENSIONID")), "Choose Reason", Eval("REASONFORSUSPENSIONID")) %>'

Open in new window

PS: I havent tested these snippets, so they might need some tweaking, but I hope you get the idea.
0
 

Author Comment

by:edjones1
ID: 40548516
Thanks, I've tried this but still getting the same error

My code


<EditItemTemplate>
                                            <asp:DropDownList ID="ddlReasons1" runat="server" AppendDataBoundItems="True" DataSourceID="dsSusReasonEdit" DataTextField="REASONFORSUSPENSION"  ForeColor="Black" DataValueField="REASONFORSUSPENSIONID"  SelectedValue='<%# Bind("REASONFORSUSPENSIONID", "{0}") %>' Enabled="False">
                                                <asp:ListItem Value="0">Choose Reason</asp:ListItem>
                                            </asp:DropDownList>
                                            <asp:SqlDataSource ID="dsSusReasonEdit" runat="server" ConnectionString="<%$ ConnectionStrings:IRISConnectionString %>" SelectCommand="SELECT [REASONFORSUSPENSIONID], [REASONFORSUSPENSION], [TYPE] FROM [ID_REASONFORSUSPENSIONID]"></asp:SqlDataSource>
                                        </EditItemTemplate>



 
Dim ddl As DropDownList = CType(dvEvent.FindControl("ddlReasons1"), DropDownList)



        If IsDBNull(dvEvent.FindControl("REASONFORSUSPENSIONID")) Then
            ddl.SelectedIndex = 0
        Else
            ddl.SelectedValue = dvEvent.FindControl("REASONFORSUSPENSIONID").ToString
        End If

Open in new window

0
 

Author Comment

by:edjones1
ID: 40548764
have tried this on dvEvent_databound

     Dim ddl As DropDownList

        ddl = DirectCast(dvEvent.FindControl("ddlsusReason"), DropDownList)

        If DirectCast(dvEvent.FindControl("susReason"), Label) Is Nothing Then

            ddl.SelectedIndex = -1

        End If
0
 
LVL 79

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 250 total points
ID: 40569298
what I'd do is test if user is suspended if not then don't display the drop down.. if user is suspended then display the drop down.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jQuery - following an example but can I save data server side? 20 50
Two wordpress questions 3 46
List<PaisEntity> - show some 1 24
Wordpress plugins not working in html 3 25
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question