如何从SQL表中为aspxcombobox上的选定项目提取ID? [英] How to pull an ID from SQL table for a selected item on an aspxcombobox?

查看:45
本文介绍了如何从SQL表中为aspxcombobox上的选定项目提取ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MS SQL表,其中包含以下列:ProductItemCategoryID,ParentProductItemCategoryID和ProductCategoryText。我还有一个ASPxComboBox,我将其命名为cmbCategory。我正在使用ProductCategoryText列中的数据填充ASPxComboBox。下面是我用来填充comboBox的代码。我用来拉取ProductItemCategoryID和ParentProductItemCategoryID的代码:



I have a MS SQL table that has the following columns, ProductItemCategoryID, ParentProductItemCategoryID and ProductCategoryText. I also have a ASPxComboBox that I have named cmbCategory. I am populating ASPxComboBox with that data found in the ProductCategoryText column. Below is the code that I am using to populate the comboBox. Also the code that I use to pull the ProductItemCategoryID and the ParentProductItemCategoryID :

private void LoadComboBox()
    {
        var productCategoryText = new ProductItemCategoryList().Cast<productitemcategory>().Select(a => a.ProductCategoryText).ToList();

        cmbCategory.Items.AddRange(productCategoryText);

    }

    //Pull the parentProductItemCategoryID AND productItemCategoryID
    protected void cmbCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        var parentCategoryID = new ProductItemCategoryList().Cast<productitemcategory>().Select(b => b.ParentProductItemCategoryID).ToList();
        var productItemID = new ProductItemCategoryList().Cast<productitemcategory>().Select(c => c.ID).ToList();
    }

<pre>I want to know the ProductItemCategoryID and ParentProductItemCategoryID of the selected item on the comboBox. How do I do that? How do I match the items inside these 3 lists? Please help.





我的尝试:



我试过以下代码:





What I have tried:

I have tried the following code:

var parentProductItemCategoryID = cmbCategory.SelectedItem.ID

推荐答案

您应该从包含ID和要显示的文本的数据源填充ComboBox。



如何使用ComboBox控件? (C#)| Microsoft Docs [ ^ ]一个有效的例子。



注意区别之间 DataTextField DataIdField

然后,您可以使用DataIdField查询数据库以获取您需要从该行获取的任何其他数据或者您可以将该信息保存在另一个隐藏的ComboBox中(与它们之间的链接具有相同的ID)或者您可以开发(获取)多列Combobox并使其他列不可见 - < a href =https://www.thomasclaudiushuber.com/2008/07/31/developing-multicolumn-dropdowndropdownlist-with-aspnet-the-gridview-and-the-ajax-control-toolkit/>开发Multicolumn-DropDown / DropDownList with ASP.NET,GridView和AJAX Control Toolkit - Thomas Claudius Huber [ ^ ]
You should populate the ComboBox from a datasource that includes the ID and the and the text to display.

See How do I use the ComboBox Control? (C#) | Microsoft Docs[^] for a worked example.

Note the difference between DataTextField and DataIdField
You can then use the DataIdField to query the database to get any other data you require from that row OR you could hold that information in another hidden ComboBox (with the same ID as the link between them) OR you could develop (acquire) a multi-column Combobox and just make the other columns invisible - Developing Multicolumn-DropDown/DropDownList with ASP.NET, the GridView and the AJAX Control Toolkit – Thomas Claudius Huber[^]


这篇关于如何从SQL表中为aspxcombobox上的选定项目提取ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆