简体   繁体   中英

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; }
}

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;
     }

Turn Active_Filtered_List to a function, and take a parameter for term

public static List<DefaultDesc_List> Active_Filtered_List(string term)
{
    ...    
}

And call it like a function from your LoadFilteredDropdown function

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); // <-- here
    ddlClientFiltered.DataBind();
    ddlClientFiltered.SelectedIndex = 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM