我有以下员工表,
name | experience | location emp1 3 yrs aaa emp2 2 yrs bbb emp3 4 yrs ccc emp4 1 yr ddd
我有下拉列表,我已绑定到表格的名称列。
我有2个文本框:txtExp
和txtLoc
。
我希望根据下拉列表中的选定值,通过DDL的索引更改或按钮的单击事件,将文本框填充到表的体验和位置列。
我想在asp.net c#
答案 0 :(得分:1)
第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>
步骤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;
}
答案 1 :(得分:0)
如果你想要显示这两件事,那么你必须连接你正在获取用于绑定下拉列表的记录的值,我会粘贴代码,它将告诉你如何通过名称获得体验(选择的值)在下拉列表中)
第一步是:
将用于绑定下拉列表的数据表保存到ViewState
然后在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();
}
}
}
}
或者如果您想将其传递给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 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
它将解决您的问题