问题描述
我想在下拉列表中显示名称和代码.我的查询是从Temp_Shipper中选择Temp_Name,Temp_Code.
下拉列表是ddlshipper,其中发件人名称将来自查询,另一个是textbox8,其中代码将来自其中.
I want to display Name and code in drop down My query is Select Temp_Name,Temp_Code from Temp_Shipper
Drop Down list is ddlshipper in which Shipper name will come from query and other one is textbox8 in which code will come.
推荐答案
DataTable shippers = new DataTable();
using (SqlConnection con = new SqlConnection(connectionString))
{
try
{
SqlDataAdapter adapter = new SqlDataAdapter("Select Temp_Name,Temp_Code from Temp_Shipper", con);
adapter.Fill(shippers);
ddlshipper.DataSource = shippers;
ddlshipper.DataTextField = "Temp_Name";
ddlshipper.DataValueField = "Temp_Code";
ddlshipper.DataBind();
}
catch (Exception ex)
{
// Handle the Exception
}
}
// Add the initial item
ddlSubject.Items.Insert(0, new ListItem("<Select Shipper>", "0"));
现在处理DropDown的SelectedIndexChanged事件,并将其值分配给TextBox.
Now handle SelectedIndexChanged Event of DropDown and assign the value to TextBox.
void Shipper_Index_Changed(Object sender, EventArgs e)
{
textbox8.Text = ddlshipper.SelectedItem.Value;
}
DropDownList
的标记就像...
Markup of DropDownList
is like...
<asp:DropDownList id="ddlshipper" runat="server" AutoPostBack="True" OnSelectedIndexChanged="Shipper_Index_Changed">
</asp:DropDownList>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
在页面加载中,设置数据源后,
DropDownList1.SelectedItem.Text =名称;
DropDownList1.SelectedItem.Value =代码;
在DropDownList1_SelectedIndexChanged方法中,
您可以将文本框值设置为DropDownList1.SelectedItem.Value
In page load, after setting data source,
DropDownList1.SelectedItem.Text=name;
DropDownList1.SelectedItem.Value= code;
In DropDownList1_SelectedIndexChanged method,
You can set the textbox value to DropDownList1.SelectedItem.Value
ddlshipper.DataSource = shippers;
ddlshipper.DataTextField = "Temp_Name" + " " + "Temp_Code";
ddlshipper.DataValueField = "Temp_Code";
ddlshipper.DataBind();
这篇关于我想在下拉列表中显示名称和代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!