将值传递给aspx代码页中的类

时间:2016-09-19 10:19:51

标签: c# asp.net class parameter-passing

我正在编写一个asp.net应用程序,我根据输入的搜索字词填写下拉列表。

我需要一种方法将搜索字词发送到我用作数据源的列表中。

这是我在aspx.cs页面上的LoadFilteredDropdown函数:

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

这是CLI.cs页面中的Active_Filtered_List代码:

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

正如您在评论中看到的那样,我希望从中发送值以及将目标发送到的目的地,我将如何实现?

修改 不确定这是否有帮助,但这里是DefaultDesc_List类:

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

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

2 个答案:

答案 0 :(得分:1)

将术语作为参数传递:

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

答案 1 :(得分:0)

Active_Filtered_List转为某个功能,并为term

取一个参数
public static List<DefaultDesc_List> Active_Filtered_List(string term)
{
    ...    
}

并将其称为LoadFilteredDropdown函数

中的函数
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;
}