我已将leaveID
,leavename
和type name
保存到我的数据库中。
现在,当我选择leaveID
时,我想自动填写假名(文本框)并自动输入名称(下拉列表)。问题是当我选择dropdownlist LeaveID
时,Leave Name(文本框)会自动填充,但typename(下拉列表)不会填充。我的ddlid_SelectedIndexChanged
事件处理程序是,
protected void ddlid_SelectedIndexChanged(object sender, EventArgs e)
{
string str = "select leavename from LeaveType where leaveID='" +
ddlid.SelectedItem.Text + "'";
SqlDataReader dr = conn.query(str);
if (dr.Read())
{
txtid.Text = dr[0].ToString();//VALUE FILLS AUTOMATICALLY ACCORDING TO SELECTED ID
}
string str1 = "select TypeName from Leavetype where LeaveID='"+
ddlid.SelectedItem.Text+"'";
SqlDataReader dr1 = conn.query(str1);
if (dr1.Read())
{
ddltype.SelectedItem.Text= dr1[0].ToString();//VALUE NOT FILLS AUTOMATICALLY ACCORDING TO SELECTED ID
}
}
答案 0 :(得分:0)
试试这个:
ddlType.SelectedValue = ddlType.Items.FindByText(dr1[0].ToString()).Value;
此外,您应该听取Soner关于使用参数化查询的建议,以防止Sql注入攻击。