AllExperts > C# 
Search      
C#
Volunteer
Answers to thousands of questions
 Home · More C# Questions · Answer Library  · Encyclopedia ·
More C# Answers
Question Library

Ask a question about C#
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Srini Nagarajan
Expertise
can answer any kind of questions in ASP.NET, C#, VB.NET, ASP, SharePoint 2007, Coldfusion, Powerbuilder 7.00 / 8.00, JAVA servlets, MS SQL 2000 / MSSQL7, Sybase

Experience
Contact me if you need any custom development on ASP.NET, ASP, Coldfusion, Powerbuilder
 
   

You are here:  Experts > Computing/Technology > Microsoft .NET > C# > how to trigger a dropdownlist refresh

C# - how to trigger a dropdownlist refresh


Expert: Srini Nagarajan - 6/4/2008

Question
Hello,

I'm kind of new to C#.NET and I've been breaking my head on this one for 2 days now.

What I am trying to do:

I have two DropDownlists: Addl and Bddl. Both DropDownlists are attached to a datasource that I specified using the "configure data source tool" of web developer 2008. Bddl is dependent on the selected item of Addl, i.e. once an item from Addl is selected with the mouse, this fires an event that tells Bddl that the data had been selected, so then Bddl adds its own items in the DropDownList (which is specified in the datasource where one of the parameters refers to the selected item of Addl, hence Bddl gets triggered by Addl and generates its own list of items).

The Problem is:

I created a function that updates DropDownlist Addl and Bddl directly from the info in my Access database. So programmatically (all in 1 function), I select an item from Addl (so far no problems since items in Addl are always generated “no dependency”). After selecting the item from Addl I expected Bddl to recognize the selected item from Addl so that it would generate its own list of items (as it usually does when I use the GUI interface with the mouse)! But Bddl at that point does not fill its list automatically with items from the query I specified in its datasource! So I am unable to select the item I want from the list!

Is this happening because no postback occurs between the time I selected an item in Addl and the time I select an item from Bddl? Is there a way I can trigger the datasource of Bddl to generate its list of items before I select my item from the list?

Should I force a postback between both dropdownlist item selections, if so how?

Should I just send a refresh event in between both selections, if so how?

I need a clean solution that does not require redefining the datasource, all the tools are there I just need to know how to trigger them at the right time, wait, and then make my selection!

Here is my code:

protected void  UpdateDDlLine(ref DropDownList A,ref DropDownList B)
{
SelectDDLItem(A, itemA);

//Here I need DropDownList B to be updated before I make my
//selection???? How to trigger the query?

SelectDDLItem(B, itemB);
}

protected void SelectDDLItem(DropDownList DDLItem, string sItem)
{
  foreach (ListItem item in DDLItem.Items)
      {
          if (item.Value == sItem)
      DDLItem.SelectedIndex = DDLItem.Items.IndexOf(item);
      }

}


Thank you in advance for your help
Nassim


Answer
Hi

Hope this helps

I've created example web form with two dropdown lists to it and name it as ddlMain and ddlSub. ddlMain is main dropdown that provides filter value to ddlSub. The code of form follows.

<%@ Page

   Language="C#" AutoEventWireup="true"
   CodeBehind="DropDownFiltering.aspx.cs"
   Inherits="MyApplication1.DropDownFiltering" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
   <title>My Page</title>
</head>
<body>
   <form id="form1" runat="server">
   <div>
       Main selection:
       <asp:DropDownList runat="server" ID="ddlMain" DataTextField="Title"
           DataValueField="Id" AutoPostBack="true"
           onselectedindexchanged="ddlMain_SelectedIndexChanged">
       </asp:DropDownList>

       Sub selection
       <asp:DropDownList runat="server" ID="ddlSub" DataTextField="Title"
           DataValueField="Id">
       </asp:DropDownList>
   </div>
   </form>
</body>
</html>

As you can see I defined OnSelectedIndexChanged event for ddlMain, also it has  AutoPostBacks turned on. If user selects some value from ddlMain then web page will be sent to server and OnSelectedIndexChanged event will be fired.


This is my Page_Load

       protected void Page_Load(object sender, EventArgs e)
       {
           if (IsPostBack)
               return;

           BindMain();
           BindSub();
       }


      protected void BindMain()
       {
           ddlMain.DataSource = GetMainData();
           ddlMain.DataBind();
       }


       protected void BindSub()
       {
           DataTable table = GetSubData();
           table.DefaultView.RowFilter = "ForeignID=" + ddlMain.SelectedValue;
           ddlSub.DataSource = table;
           ddlSub.DataBind();
       }


//created a simple datatable for MAINDDL
       protected DataTable GetMainData()
       {
           DataTable table = new DataTable();
           table.Columns.Add("ID");
           table.Columns.Add("Title");
           DataRow dr;

           dr = table.NewRow();
           dr["ID"] = 1;
           dr["Title"] = "First";
           table.Rows.Add(dr);

           dr = table.NewRow();
           dr["ID"] = 2;
           dr["Title"] = "Second";
           table.Rows.Add(dr);

           dr = table.NewRow();
           dr["ID"] = 3;
           dr["Title"] = "Third";
           table.Rows.Add(dr);
           return table;
       }

       protected DataTable GetSubData()
       {
           DataTable table = new DataTable();
           table.Columns.Add("ID");
           table.Columns.Add("ForeignID"); //Main table ID
           table.Columns.Add("Title");

           DataRow dr;
           dr = table.NewRow();
           dr["ID"] = 1;
           dr["ForeignID"] = 1;
           dr["Title"] = "1:First";
           table.Rows.Add(dr);

           dr = table.NewRow();
           dr["ID"] = 2;
           dr["ForeignID"] = 1;
           dr["Title"] = "1:Second";
           table.Rows.Add(dr);

           dr = table.NewRow();
           dr["ID"] = 3;
           dr["ForeignID"] = 2;
           dr["Title"] = "2:First";
           table.Rows.Add(dr);

           dr = table.NewRow();
           dr["ID"] = 4;
           dr["ForeignID"] = 2;
           dr["Title"] = "2:Second";
           table.Rows.Add(dr);

           return table;
       }



       protected void ddlMain_SelectedIndexChanged(object sender, EventArgs e)
       {
           BindSub();
       }
   }
}


Thanks

-Srini

Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.