Saroj13
asked on
how to handle active and inactive items in dropdownlist c#?
My DB design:
resolve Table: id, resolve
Resolve_system: id, resolve_id, system_id, is_active
Results table: id, feedback_id, system_id, resolve_id
On page load, it will display all the feedbacks in the gridview. Clicking on the feedbackid in gridview row will open the details in the formview.
Some of the items in resolve table are inactive now. But there are many old records which have inactive resolve. I want old records have the old inactive value selected in the dropdownlist, but for new items, it will display only active values in the resolve dropdown.
resolve Table: id, resolve
Resolve_system: id, resolve_id, system_id, is_active
Results table: id, feedback_id, system_id, resolve_id
On page load, it will display all the feedbacks in the gridview. Clicking on the feedbackid in gridview row will open the details in the formview.
Some of the items in resolve table are inactive now. But there are many old records which have inactive resolve. I want old records have the old inactive value selected in the dropdownlist, but for new items, it will display only active values in the resolve dropdown.
<asp:FormView ID="fvRecordDetail" runat="server" DefaultMode="Edit" Width="100%">
<EditItemTemplate>
<asp:DropDownList ID="ddlResolveRD" runat="server" AppendDataBoundItems="true" DataSourceID="sdsResolve" DataTextField="resolve" DataValueField="id" SelectedValue='<%# Eval("resolve_id") %>' Width="100%" >
</asp:DropDownList>
<asp:HiddenField ID="hidOrgResolveSelectedValue" runat="server" Value='<%# Eval("resolve_id") %>' />
<asp:SqlDataSource ID="sdsResolve" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnString %>"
SelectCommand="[dbo].[fb_get_resolve]"
SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter Name="system_id" QueryStringField="sysid" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
SELECT [res].[id], [res].[resolve] as resolve
FROM [dbo].[resolve] [res]
INNER JOIN [dbo].[resolve_system] [res_sys] ON [res].[id] = [res_sys].[resolve_id]
INNER JOIN [dbo].[system] [codes] ON [res_sys].[system_id] = [codes].[id]
WHERE ([codes].[code] = @system_code)
One way to handle this is when you're searching the list of drop down values to mark the "selected" one from the record you're loading. If you don't find the record's value in the drop down list, you know that it has a value that's not active. In that case, add the inactive value to the drop down list.
ASKER
Would you please make the changes in my code.
I want that in the dropdown in the edit section, its displaying the previously selected value. That selected value can be inactive also. then user is having capability to change the value in the dropdownlist also. in case of changing, it will just display the active items in dropdownlist.
I am not sure how to achieve this.
I want that in the dropdown in the edit section, its displaying the previously selected value. That selected value can be inactive also. then user is having capability to change the value in the dropdownlist also. in case of changing, it will just display the active items in dropdownlist.
I am not sure how to achieve this.
You'd have to change your logic so that the drop down is bound on the server side, so you can implement the logic to inspect the list values and add the missing value.
Or, create an OnDataBinding event and look for ArgumentOutOfRangeExceptio n. When that exception occurs, add the missing value.
You'll need to add the event to the control, too:
protected void ddlResolveRD_DataBinding(object sender, EventArgs e)
{
DropDownList theDropDownList = (DropDownList)sender;
theDropDownList.DataBinding -= new EventHandler(DropDownList_DataBinding);
try
{
theDropDownList.DataBind();
}
catch(ArgumentOutOfRangeException)
{
UserInterface.test item = (UserInterface.test)((IDataItemContainer)theDropDownList.NamingContainer).DataItem;
theDropDownList.Items.Add(item.Value);
}
}
You'll need to add the event to the control, too:
<asp:DropDownList ID="ddlResolveRD" runat="server" OnDataBinding="ddlResolveRD_DataBinding" AppendDataBoundItems="true" DataSourceID="sdsResolve" DataTextField="resolve" DataValueField="id" SelectedValue='<%# Eval("resolve_id") %>' Width="100%" ></asp:DropDownList>
ASKER
My code is in VB.net. how to convert the same in vb.net?
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.