Cascading dropdown in ASP.Net is not working fine - asp.net

Hi - I know this sounds simple, but I am having trouble with some functionality to be achieved. Please refer the attached screen shot of the situation. This is what I am doing:
On page load I am loading the first drop down of Product / Service
BAsed on the selection of the Product dropdown I am populating the second dropdown "Category". I am making the database call to make this happen.
Based on the selection of the Category I am populating the third drop down of the price. I am making database call to get the information for this drop down.
Based on the selection of third (Price) dropdown, I am hiding / showing the panels.
Each dropdown is doing a post back to make the call to the datbase to get the details for the subsequent dropdown.
Everything seems to be working during first run, but the problem comes when I try a combination of the drop down. Following are the problems I am having:
Panel once rendered doesn't go away if I select different option from Dropdown1 and 2.
After subsequent run, the values are added up after each post back. For example if I have a product "Toy Car" in the product / service dropdown, it is now appearing twice and so on.
If the user selects the default option "Select Product", "Select Category" etc. The entire hierarchy should be reset. For example if the previous selection in the Product dropdown was "Toy Car", and now I select the default option "Select Product". It should reset "Category", Price and Panel visibility.
Please let me know how this can be done.
Thanks for the help.

On three DropDownLists
1 You set AutoPostBack="true"
2 You define OnSelectIndexChanged
<asp:dropdownlist id="ddl1" runa="server" Autopostback="true" onselectindexChanged="ddl1_selectindexChanged"/>
<asp:dropdownlist id="ddl2" runa="server" Autopostback="true" onselectindexChanged="ddl2_selectindexChanged"/>
<asp:dropdownlist id="ddl3" runa="server" Autopostback="true" onselectindexChanged="ddl3_selectindexChanged"/>
You must define page_load with this strategy
If(! IsPostBack)
{
//Bind dropdownlist
}
On each selectindexChanged, get value and execute selection in database, and bind your dropdownlist.
void ddl1_selectindexChanged(Object sender, EventArgs e)
{
//Get Selected Values
//For examlpe you can use SelectedValue property
var value1 = ddl1.SelectedValue;
//Request your database
//And bind your dropdownlist of sub categories
...
}
void ddl2_selectindexChanged(Object sender, EventArgs e) 
{
...
}
void ddl3_selectindexChanged(Object sender, EventArgs e)
{
..;
}

Do following things:
1) Second and Third dropdown should be disabled until user select the first drop down then enable second dropdown when user selects second dropdown then enable the third drop down.
2) On post back, Clear the Items in the DropDownList and them bind them with the datasource again. Simply create a function for each dropdown and call it on PostBack after clearing DropDownList by calling Clear method of dropdownlist.
3) To reset the entire hierarchy, Set the SeletecedIndex of all the dropdowns to Zero or -1;

Related

FormView insert hidden parameters based on dropdownlist selection

I have a FormView which I populate using SqlDataSource1. I do databinding like:
Text='<%# Bind("EffectiveDate") %>'
FormView also contain a dropdownlist with a custom SqldataSource2. Dropdown list contains list of cities. User is able to change a value in dropdown.
Once I submit a form I have to send to PowerShell script some parameters which I supposed to hide like street, ZipCode, etc... On first form load I can bind this parameters to hidden fields from SqlDataSource1. But if user change a value in dropdownlist I have to rebind parameters. Have no idea yet how.
Thanks!
You can configure the drop down list to do a post back.
AutoPostBack="true"
and then on Page Load or Selection changed event you can set up the hidden field.
Example:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
hfSomething.Value = ddlSomething.SelectedValue;
}
else
{
//data binding code
}
}

Dynamic ASP Form Post - Without Using an Iframe

I need to modify a 3-level dropdown <select> menu to function directly on the page and not inside an iframe (that's how we currently do it).
Here's how this menu works:
User makes a selection in 1st dropdown
Choices in 2nd dropdown are filtered based on what's chosen in 1st one; user selects from one of these choices
Choices in 3rd dropdown are filtered based on what's chosen in 2nd one; user selects from one of these choices; this last dropdown submits the form and redirects the user to another page, also passing the values from all three dropdowns.
Right now, this is accomplished using an <iframe> that queries an ASP.net database and reloads itself after each selection.
I really need it to work without using an iframe. Not sure what the most elegant way to approach this is ...
If you're open to using the ASP.NET Ajax Toolkit, they have a cascading dropdown control which does what you ask. One additional benefit, because it uses Ajax it does not have to reload the page after each selection in the dropdowns.
i think you'll probably need to use the 'onSelectIndexChanged` even on the DropDownList control. something like...
<asp:DropDownList id="ddl1" runat="server" OnSelectedIndexChanged="ddl1_OnSelectedIndexChanged"></asp:DropDownList>
<asp:DropDownList id="ddl2" runat="server" OnSelectedIndexChanged="ddl2_OnSelectedIndexChanged"></asp:DropDownList>
<asp:DropDownList id="ddl3" runat="server" OnSelectedIndexChanged="ddl3_OnSelectedIndexChanged"></asp:DropDownList>
Page_Load()
{
if(!IsPostBack)
{
ddl1.DataSource = getdata();
ddl1.DataBind();
}
protected void ddl1_onSelectedIndexChanged(object sender, EventArgs e)
{
ddl2.DataSource = getData(ddl1.SelectedValue);
ddl2.DataBind()
}
protected void ddl1_onSelectedIndexChanged(object sender, EventArgs e)
{
ddl3.DataSource = getData(ddl2.SelectedValue);
ddl3.DataBind()
}
protected void ddl3_onSelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("SomePage.aspx?ddl1="+ddl1.SelectedValue+"&ddl2="+ddl2.SelectedValue+"&ddl3="+ddl3.SelectedValue, true);
}
You could put all 3 of them in an UpdatePanel.
I am sure your changes are already making the page refresh.
Putting them in an update panel will make only the panel refresh, causing a partial page update.
This uses ajax to do the job internally.
Here is a update panel example, if you want one.
P.s.: Asp.net's method to create a <select> is a DropDownList control. I hope you are already using those, if not convert your selects to a dropdownlists.

ASP.NET Boundfield changes when I update another gridview

I have a boundfield in a gridview that programatically receives a hyperlink for the content in it. It gets a new dataset after a drop down list index change. One of the columns of the dataset will apply links to fields with data and skip those without, you can see my logic for applying links below:
if (e.Row.DataItem != null && int.TryParse(e.Row.Cells[4].Text, out incidents))
{
HyperLink incidentsLink = new HyperLink();
incidentsLink.ForeColor = System.Drawing.Color.Blue;
incidentsLink.NavigateUrl = "~/somesite.aspx?no=" + stnNum + "&dt=" + date;
incidentsLink.Text = e.Row.Cells[4].Text;
e.Row.Cells[4].Controls.Add(incidentsLink);
}
This is applied OnRowDataBound for the gridview. Then I have another gridview that is wired to another drop down list. When either drop down list changes index it grabs a new dataset for the related gridview and fires a ajax update using an update panel.
What happens is when the second gridview updates it erases the links in the first gridview. It doesn't erase the text, that stays, but the text is no longer anchored to a link. All links made this way and put into gridviews lose their link properties, however fields created using asp:HyperLinkField are left unchanged. The problem is that stnNum and date are not a part of the dataset that is returned for the first gridview, so I have to add them as a link after the gridview is already built.
The only solution I can think of is to refire the function that adds links to the gridview each time the second gridview updates. Any other solutions would be helpful, or an explanation as to why my link is being erased would be great.
Try binding the GridView/DropDowns only if there is not any POSTBACK. I believe POST Back event is letting your gridview & Controls to loose their data somehow.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Bind your Grids & Dropdowns here on page load
}
}

Populate gridview based on databound dropdown list selectedvalue change

I have an asp.net application for membership management. One page needs to have a gridview which is populated based on a dropdown list of statuses. I initially thought about hard-coding with a Select Case, but then remembered that the dropdown list is databound and needs to be dynamic (because the admin-level users have another page to change the statuses). I'm still new at this, and my searches are not turning up anything.
Any links or examples would be helpful. Thanks.
I would suggest to use OnSelectedIndexChanged event of dropdownlist for your purpose with AutoPostBack property set to true, something like this
<asp:DropDownList runat="server" ID="ddlStatus" OnSelectedIndexChanged="ddlStatus_SelectedIndexChanged" AutoPostBack="True"></asp:DropDownList>
And on your code behind page you can bind your grid differently for different selected values in your event handler, something like this
protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlStatus.SelectedItem.Value == "RequiredValue")
{
// bind grid in some way
}
else
{
// bind grid in some other way
}
}
This will work irrespective of your binding the dropdownlist options dynamically or having them hard coded.

listbox selection not retaining on postback (yes i check for postback)

I have a listbox in an asp.net webpage.
I select item 2. if i have autopostback on, this reloads the page and selects item1.
I only databind the page on first load (!IsPostback), have stepped through and verified this. It's not rebinding or repopulating the list, it just selects the first item again, removing the second item selection.
Furthermore, the onselectedindexchanged event only fires AFTER postback so i cannot retain the selected index in a session variable to set on postback...
page is set to viewstateenabled, controls set to viewstateenabled, viewstatemode is enabled.
How do I set it up so that the 2nd item is still selected on postback? My code is very straightforward but I'll post it...
<asp:ListBox ID="AvailableRepairSelection" runat="server" AutoPostBack="True"
onselectedindexchanged="AvailableRepairSelection_SelectedIndexChanged"
ViewStateMode="Enabled"></asp:ListBox>
Again, I select item 2, page posts back, item1 is highlighted.
relevant parts of codebehind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int selectedmodel = Convert.ToInt32(Session["selectedModel"]);
List<AvailableRepair> Repairs = (from r in Database.instance().AvailableRepairs
where r.modelid == selectedmodel
select r).ToList<AvailableRepair>();
foreach (AvailableRepair repair in Repairs)
{
AvailableRepairSelection.Items.Add(new ListItem(repair.repair + ": " + repair.description + "- " + repair.cost, repair.cost));
}
}
}
PLEASE NOTE that there is more to this page but I have isolated the only issues to be with these snippets. So dont advise me how to change my page or why i am posting back a selection when theres nothing but a listbox in the page.

Resources