Ed
asked on
Asp.Net Drop Down List Datasource
HI
I have a details view that contains a dropdown list
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("REASONFORSUSPENSIONI D", "{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.
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>
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("REASONFORSUSPENSIONI
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.
you have to check for a null return and if null then do not display the drop down list
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, I've tried this but still getting the same error
My code
<EditItemTemplate>
<asp:DropDownList ID="ddlReasons1" runat="server" AppendDataBoundItems="True " DataSourceID="dsSusReasonE dit" DataTextField="REASONFORSU SPENSION" ForeColor="Black" DataValueField="REASONFORS USPENSIONI D" SelectedValue='<%# Bind("REASONFORSUSPENSIONI D", "{0}") %>' Enabled="False">
<asp:ListItem Value="0">Choose Reason</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="dsSusReasonEdit" runat="server" ConnectionString="<%$ ConnectionStrings:IRISConn ectionStri ng %>" SelectCommand="SELECT [REASONFORSUSPENSIONID], [REASONFORSUSPENSION], [TYPE] FROM [ID_REASONFORSUSPENSIONID] "></asp:Sq lDataSourc e>
</EditItemTemplate>
My code
<EditItemTemplate>
<asp:DropDownList ID="ddlReasons1" runat="server" AppendDataBoundItems="True
<asp:ListItem Value="0">Choose Reason</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="dsSusReasonEdit" runat="server" ConnectionString="<%$ ConnectionStrings:IRISConn
</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
ASKER
have tried this on dvEvent_databound
Dim ddl As DropDownList
ddl = DirectCast(dvEvent.FindCon trol("ddls usReason") , DropDownList)
If DirectCast(dvEvent.FindCon trol("susR eason"), Label) Is Nothing Then
ddl.SelectedIndex = -1
End If
Dim ddl As DropDownList
ddl = DirectCast(dvEvent.FindCon
If DirectCast(dvEvent.FindCon
ddl.SelectedIndex = -1
End If
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.