如何根据下拉选择从sql server数据库填充文本框 [英] How to Populate a textbox from sql server database on basis of drop down selection

查看:112
本文介绍了如何根据下拉选择从sql server数据库填充文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用asp.net 3层架构来创建Web应用程序。在表示层,我想在选择下拉列表中的某个选项时,根据该选择从sql server数据库填充文本框。请帮我提供示例代码..

I am using asp.net 3-tier architecture for creating a web application. In presentation layer, I want when some option from drop down list is selected, the textbox is populated from sql server database on basis of that selection.Please help me with Sample code..

推荐答案

参考此代码,



protected void yourDropDwonId_SelectedIndexChanged(object sender,EventArgs e)

{

SqlConnection sqlConnection = new SqlConnection(你的ConnString);

DataTable dt = new DataTable();

string sql = @选择查询;

SqlCommand command = new SqlCommand(sql,sqlConnection);

command.Parameters.AddWithValue(@ empCode,empcode);

command.Parameters.AddWithValue( @id,id);

SqlDataAdapter da = new SqlDataAdapter(command);

da.Fill(dt);

return dt ;

if(yourDropDwonId.SelectedValue ==0)//全部

{

//代码

Textbox1.Text = dt.Rows [0] [yourcolumnname]。toString();

}

else if(yourDropDwonId.SelectedValue ==1)//已标记

{

//代码

Textbox1.Text = dt.Rows [0] [yourcolumnname]。toString();

}

else if(yourDropDwonId.SelectedValue == 2)//新

{

//代码

Textbox1.Text = dt.Rows [0] [yourcolumnname]。 toString();

}

}
Ref this code,

protected void yourDropDwonId_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection sqlConnection = new SqlConnection(Your ConnString);
DataTable dt=new DataTable();
string sql = @"Select Query";
SqlCommand command = new SqlCommand(sql, sqlConnection);
command.Parameters.AddWithValue("@empCode", empcode);
command.Parameters.AddWithValue("@id", id);
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(dt);
return dt;
if (yourDropDwonId.SelectedValue == "0")//All
{
//code
Textbox1.Text=dt.Rows[0]["yourcolumnname"].toString();
}
else if (yourDropDwonId.SelectedValue == "1")//Flagged
{
//code
Textbox1.Text=dt.Rows[0]["yourcolumnname"].toString();
}
else if (yourDropDwonId.SelectedValue == "2")//New
{
//code
Textbox1.Text=dt.Rows[0]["yourcolumnname"].toString();
}
}


这篇关于如何根据下拉选择从sql server数据库填充文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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