简体   繁体   English

.NET C#DropDownList将参数传递给函数

[英].NET C# DropDownList pass parameter to function

I don't really know how to word this...I have a drop down list that queries a database and displays a list of descriptions based on ids. 我真的不知道该怎么写...我有一个下拉列表,用于查询数据库并显示基于ID的描述列表。 I want the user to be able to select a description, push a button and call my getQueryResults() function with the id being passed and not the description. 我希望用户能够选择一个描述,按一个按钮并使用传递的ID而不是描述来调用我的getQueryResults()函数。 getQueryResults(DropDownList1.SelectedValue) doesn't work because of conflicting types. 由于类型冲突,getQueryResults(DropDownList1.SelectedValue)不起作用。 What else can I use? 我还能使用什么?

    protected void Page_Load(object sender, EventArgs e)
    {
        DataConnector dc = new DataConnector();
        DropDownList1.DataSource = dc.getCodeTypes();
        DropDownList1.DataValueField = "id";
        DropDownList1.DataTextField = "description";
        DropDownList1.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        DataConnector dc = new DataConnector();
        GridView2.DataSource = dc.getQueryResults(DropDownList1.SelectedValue); //error: cannot convert from string to int
        GridView2.DataBind();

    }

    public List<CodeDesc> getQueryResults(int searchTerm)
    {
        try
        {
            string query = "select id, code, descr from code_desc where code_type_id = :searchTerm";
           //more stuff

My manager does not want me to change the query string. 我的经理不希望我更改查询字符串。 (previously I had used "select id, code, descr from code_desc where code_type_id = (select id from code_desc where descr = :searchTerm);" where searchTerm was the string description thus eliminating the need for the id) (以前,我使用过“从code_desc中选择id,代码,descr,其中code_type_id =(从code_desc中选择id,descr =:searchTerm);”其中searchTerm是字符串描述,因此不需要id)

Start by wrapping your DropDownList DataBinding inside an IsPostBack check. 首先将DropDownList DataBinding包装在IsPostBack检查中。

if (!Page.IsPostBack)
{
    DropDownList1.DataSource = dc.getCodeTypes();
    DropDownList1.DataValueField = "id";
    DropDownList1.DataTextField = "description";
    DropDownList1.DataBind();
}

I you do not, every time you load the page the data is bound again and the DropDownList goes back to it's first value. 我不是,每次加载页面时,数据都会再次绑定,并且DropDownList返回其第一个值。

Then you can call the getQueryResults with the SelectedValue by converting the value to an int . 然后,可以通过将值转换为int来调用带有SelectedValuegetQueryResults

GridView2.DataSource = dc.getQueryResults(Convert.ToInt32(DropDownList1.SelectedValue));

Finally, google parameratized queries . 最后,谷歌用参数化查询

The SelectedValue property from the DropDownList class is a string DropDownList类的SelectedValue属性是一个字符串

https://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.listcontrol.selectedvalue(v=vs.110).aspx https://msdn.microsoft.com/fr-fr/library/system.web.ui.webcontrols.listcontrol.selectedvalue(v=vs.110).aspx

[BindableAttribute(true, BindingDirection.TwoWay)]
[BrowsableAttribute(false)]
[ThemeableAttribute(false)]
public virtual string SelectedValue { get; set; }

An that is why you get this convertion error, you need to parse the string into an int or even better just pass the string value. 这就是为什么会出现此转换错误的原因,您需要将字符串解析为int甚至更好地只是传递字符串值。

您可以使用它来传递一个int值。

int parameter = Convert.ToInt32(DropDownList1.SelectedValue);
   public class ComboboxItem
    {
        public string Text { get; set; }
        public object Value { get; set; }

        public override string ToString()
        {
            return Text;
        }
    }

 protected void Page_Load(object sender, EventArgs e)
{
    DataConnector dc = new DataConnector();
    DropDownList1.DataSource = dc.getCodeTypes();
    ComboboxItem item = new ComboboxItem();
            item.Text = "id"
            item.Value = "description";

            DropDownList1.Items.Add(item);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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