将值分配给数据库驱动的组合框 [英] Assign Values To Database Driven Combobox

查看:68
本文介绍了将值分配给数据库驱动的组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的表中,我有两列name和id,我想在组合框中显示我已经做过的名称字段值,我的意思是如何为组合框分配值,以便用户选择"ABC"从组合框中,然后单击提交按钮,我要发送"ABC"的ID值而不是名称.例如我有

名称ID
ABC 23
ASD 28
ZXC 30

因此,当用户单击提交"按钮时,程序应发送23,而不是名称本身.我为此做了什么
我正在使用VB.NET

在此先感谢

Hi, in my table i have two columns name and id, i want to display name field values in combobox that i already did, my point here is how to assign values to the combobox so that if the user selects "ABC" from combobox and click on submit button i want to send the ID value of the "ABC" not the name. for example i have

Name id
ABC 23
ASD 28
ZXC 30

So when the user click on submit button the program should send 23, not the name itself. what i did for this
I am using VB.NET

Thanks in advance

推荐答案

这确实取决于您如何将数据绑定到组合框,但是您可能正在寻找
It does depend on how you''ve bound the data to the combobox, but you''re probably looking for the Selected Value[^] property,

There are a number of properties that will help you when trying to determine what the user has selected - SelectedIndex, SelectedItem, SelectedText, SelectedValue

Add an event handler to the combobox to trap when the user selects an item & experiement with these properties to view the data associated with them


我正在使用" cmbParent.SelectedItem.ToString ="ABC"",但ID无法保存并生成SqlException
将varchar值"ID"转换为数据类型int时转换失败."所以我想将ID值保存到数据库表中,而不是文本值中.
i am using '' cmbParent.SelectedItem.ToString = "ABC" '' but id not save and generate a SqlException
" Conversion failed when converting the varchar value ''ID '' to data type int. " so i want to save id value into database table, not text value.


使用此代码,但代码在C#

< asp:dropdownlist id ="DropDownList1" runat ="server" xmlns:asp =#unknown">
DataSourceID ="SqlDataSource1" DataTextField =名称" DataValueField ="ID">

< asp:sqldatasource id ="SqlDataSource1" runat ="server" xmlns:asp =#unknown">
ConnectionString =<%
use this, but code in c#

<asp:dropdownlist id="DropDownList1" runat="server" xmlns:asp="#unknown">
DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="ID">

<asp:sqldatasource id="SqlDataSource1" runat="server" xmlns:asp="#unknown">
ConnectionString="<%


这篇关于将值分配给数据库驱动的组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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