I have following Employee table,
我有以下员工表,
name | experience | location emp1 3 yrs aaa emp2 2 yrs bbb emp3 4 yrs ccc emp4 1 yr ddd
I have dropdown list which I have bound to name column of the table. I have 2 textboxes: txtExp
and txtLoc
. I want to populate textboxes to experience and location columns of the table on the basis of selected value from dropdown list, either by index changed of DDL or click event of a button. i want to do it form a code behind file in asp.net c#
我有下拉列表,我已经命名为表的列。我有2个文本框:txtExp和txtLoc。我想根据下拉列表中的选定值,通过DDL的索引更改或按钮的单击事件,填充文本框以体验和定位表的列。我想在asp.net c#中创建一个代码隐藏文件
2 个解决方案
#1
1
Step 1: HTML - Have a drop down list that performs a post-back:
第1步:HTML - 有一个执行回发的下拉列表:
<asp:DropDownList ID="ddlName" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ddlName_SelectedIndexChanged">
<asp:ListItem Value="1">Emp 1</asp:ListItem>
<asp:ListItem Value="2">Emp 2</asp:ListItem>
<asp:ListItem Value="3">Emp 3</asp:ListItem>
</asp:DropDownList>
Step 2: C# - Execute the code that updates the controls:
第2步:C# - 执行更新控件的代码:
protected void ddlName_SelectedIndexChanged(object sender, EventArgs e)
{
// Make a call to database/method to retrieve the Employee record based on
// Name/ID, which is bound to the drop down. Assumption that you will
// use an object called Employee with name & location properties)
Employee emp = GetEmployeeByID(ddlName.SelectedValue);
txtExp.Text = emp.Name;
txtLoc.Text = emp.Location;
}
#2
0
if you want to show both the things then you have to concatenate the values where you are fetching the records for binding the dropdownlist, i am pasting the code, which will tell you how you can get the experience by name(selected value in dropdown)
如果你想要显示这两个东西,那么你必须连接你正在获取用于绑定下拉列表的记录的值,我粘贴代码,它将告诉你如何通过名称获得体验(下拉列表中的选定值)
first step is :
第一步是:
save the datatable from which you are binding the dropdown into ViewState
将用于绑定下拉列表的数据表保存到ViewState中
then on dropdown's selectedindexchanged event use the following code:
然后在dropdown的selectedindexchanged事件中使用以下代码:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (ViewState["dt"] != null)
{
DataTable dt = (DataTable)ViewState["dt"];
if (dt.Rows.Count > 0)
{
string sqlFilter = "name = '" + DropDownList1.SelectedItem.Text + "'";
DataRow[] data = dt.Select(sqlFilter);
if (data.Length > 0)
{
TextBox1.Text = data["Exp"].ToString();
}
}
}
}
or if you want to pass it to db to get the experience and location then do it in the following way:
或者如果要将其传递给db以获取经验和位置,请按以下方式执行:
clsArtBook obj = new clsArtBook();
DataTable dt = new DataTable();
dt = obj.getDetailsbyName(DropDownList1.SelectedItem.Text);
if (dt.Rows.Count > 0) {
TextBox1.Text = dt.Rows[0]["expeince"].ToString();
TextBox1.Text = dt.Rows[0]["location"].ToString();
}
create a stored proc which will get the details by name like :
创建一个存储过程,它将按名称获取详细信息,如:
create proc proc_GetDetailsByName
(
@name varchar(100)
)
as
if exists(select 1 from tblemployee where name=@name)
begin
select location,experince from tblemployee where name=@name
end
it will resolve your problem
它会解决你的问题
#1
1
Step 1: HTML - Have a drop down list that performs a post-back:
第1步:HTML - 有一个执行回发的下拉列表:
<asp:DropDownList ID="ddlName" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="ddlName_SelectedIndexChanged">
<asp:ListItem Value="1">Emp 1</asp:ListItem>
<asp:ListItem Value="2">Emp 2</asp:ListItem>
<asp:ListItem Value="3">Emp 3</asp:ListItem>
</asp:DropDownList>
Step 2: C# - Execute the code that updates the controls:
第2步:C# - 执行更新控件的代码:
protected void ddlName_SelectedIndexChanged(object sender, EventArgs e)
{
// Make a call to database/method to retrieve the Employee record based on
// Name/ID, which is bound to the drop down. Assumption that you will
// use an object called Employee with name & location properties)
Employee emp = GetEmployeeByID(ddlName.SelectedValue);
txtExp.Text = emp.Name;
txtLoc.Text = emp.Location;
}
#2
0
if you want to show both the things then you have to concatenate the values where you are fetching the records for binding the dropdownlist, i am pasting the code, which will tell you how you can get the experience by name(selected value in dropdown)
如果你想要显示这两个东西,那么你必须连接你正在获取用于绑定下拉列表的记录的值,我粘贴代码,它将告诉你如何通过名称获得体验(下拉列表中的选定值)
first step is :
第一步是:
save the datatable from which you are binding the dropdown into ViewState
将用于绑定下拉列表的数据表保存到ViewState中
then on dropdown's selectedindexchanged event use the following code:
然后在dropdown的selectedindexchanged事件中使用以下代码:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (ViewState["dt"] != null)
{
DataTable dt = (DataTable)ViewState["dt"];
if (dt.Rows.Count > 0)
{
string sqlFilter = "name = '" + DropDownList1.SelectedItem.Text + "'";
DataRow[] data = dt.Select(sqlFilter);
if (data.Length > 0)
{
TextBox1.Text = data["Exp"].ToString();
}
}
}
}
or if you want to pass it to db to get the experience and location then do it in the following way:
或者如果要将其传递给db以获取经验和位置,请按以下方式执行:
clsArtBook obj = new clsArtBook();
DataTable dt = new DataTable();
dt = obj.getDetailsbyName(DropDownList1.SelectedItem.Text);
if (dt.Rows.Count > 0) {
TextBox1.Text = dt.Rows[0]["expeince"].ToString();
TextBox1.Text = dt.Rows[0]["location"].ToString();
}
create a stored proc which will get the details by name like :
创建一个存储过程,它将按名称获取详细信息,如:
create proc proc_GetDetailsByName
(
@name varchar(100)
)
as
if exists(select 1 from tblemployee where name=@name)
begin
select location,experince from tblemployee where name=@name
end
it will resolve your problem
它会解决你的问题