Barry Michael Doyle Barry Michael Doyle - 2 months ago 9
ASP.NET (C#) Question

Pass value to a class from an aspx code page

I'm writing an asp.net application where I'm trying to fill a drop-down list according to what search term is entered.

I need a way to send the search term across to the list that I'm using as a data source.

This is my LoadFilteredDropdown function on the aspx.cs page:

protected void LoadFilteredDropdown(string term)
{
// send term to Services.StaticLists.Master.CLI.Active_Filtered_List here
ddlClientFiltered.Items.Clear();
ddlClientFiltered.Items.Add(new ListItem("- Select", "0"));
ddlClientFiltered.AppendDataBoundItems = true;
ddlClientFiltered.DataSource = Services.StaticLists.Master.CLI.Active_Filtered_List;
ddlClientFiltered.DataBind();
ddlClientFiltered.SelectedIndex = 0;
}


And here is the Active_Filtered_List code from the CLI.cs page:

private static List<DefaultDesc_List> cli_Active_Filtered_List = null;
public static List<DefaultDesc_List> Active_Filtered_List
{
get
{
var db = new Entities();
string term = ""; // receive the term here
var results = (from cli in db.CDC_M_Client_CLI
where cli.CLI_Active == true &&
cli.CLI_Name.StartsWith(term)
orderby cli.CLI_Name
select new
{
cli.CLI_Id,
cli.CLI_Name,
cli.CLI_Active
}).ToList();
cli_Active_Filtered_List = new List<DefaultDesc_List>();
foreach (var result in results)
{
if(result.CLI_Active)
{
cli_Active_Filtered_List.Add(new DefaultDesc_List()
{
Id = result.CLI_Id,
Desc = result.CLI_Name
});
}
}

return cli_Active_Filtered_List;
}
}


As you can see in the comments, that is where I want to send the value from and the destination to send it to, how would I accomplish that?

Edit:
Not sure if this helps but here's the DefaultDesc_List class:

public class DefaultDesc_List
{
public int Id { get; set; }
public string Desc { get; set; }

public virtual ICollection<DefaultDesc_List> DefaultDesc_Lists { get; set; }
}

Answer

Just pass term as an argument:

 protected void LoadFilteredDropdown(string term)
 {
// send term to Services.StaticLists.Master.CLI.Active_Filtered_List here
ddlClientFiltered.Items.Clear();
ddlClientFiltered.Items.Add(new ListItem("- Select", "0"));
ddlClientFiltered.AppendDataBoundItems = true;
ddlClientFiltered.DataSource = Services.StaticLists.Master.CLI.Active_Filtered_List(term);
ddlClientFiltered.DataBind();
ddlClientFiltered.SelectedIndex = 0;
}


  private static List<DefaultDesc_List> cli_Active_Filtered_List = null;
 public static List<DefaultDesc_List> Active_Filtered_List(string term)
 {
        var db = new Entities();
    string term = ""; // receive the term here
    var results = (from cli in db.CDC_M_Client_CLI
                   where cli.CLI_Active == true &&
                   cli.CLI_Name.StartsWith(term)
                   orderby cli.CLI_Name
                   select new
                   {
                       cli.CLI_Id,
                       cli.CLI_Name,
                       cli.CLI_Active
                   }).ToList();
    cli_Active_Filtered_List = new List<DefaultDesc_List>();
    foreach (var result in results)
    {
        if(result.CLI_Active)
        {
            cli_Active_Filtered_List.Add(new DefaultDesc_List()
            {
                Id = result.CLI_Id,
                Desc = result.CLI_Name
            });
        }
    }

    return cli_Active_Filtered_List;
     }