与Girdview的下拉列表 [英] Dropdownlist with Girdview
问题描述
大家好,
仅获得输出,这是其项目默认的第一人,没有获得员工其他部门的详细信息,例如...在下拉列表中的第一项是测试(部门)...当我更改其他部门时,输出也将是相同的测试(部门) ).
请有人帮我解决这个问题...
请在下面找到代码...
Hi All,
Am getting output only which is its Item default firstone, am not geting employee details other department for example...in dropdown first item is Testing(Department)...when i change other department also the output will be the same Testing (Department).
pls someone help me to resolve this issue...
pls find the code below...
public partial class Default2 : System.Web.UI.Page
{
SqlConnection conn;
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
conn = new SqlConnection("Data Source=.;Initial Catalog=Employee;Integrated Security=True");
cmd.CommandText = "SELECT Department_Name FROM Department_Master";
cmd.Connection = conn;
conn.Open();
DropDownList1.DataSource = cmd.ExecuteReader();
DropDownList1.DataTextField = "Department_Name";
DropDownList1.DataBind();
conn.Close();
string sqlquery = "SELECT Employee_Master.Employee_Name, Employee_Master.Date_of_join, DATEDIFF(day, Employee_Master.Date_of_join, GETDATE()) AS TotalWorkedDays FROM Department_Master INNER JOIN Employee_Master ON Department_Master.Department_ID = Employee_Master.Department_ID";
conn = new SqlConnection("cn");
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlquery, conn);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
conn.Close();
}
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
string sqlquery = "SELECT Employee_Master.Employee_Name, Employee_Master.Date_of_join, DATEDIFF(day, Employee_Master.Date_of_join, GETDATE()) AS TotalWorkedDays FROM Department_Master INNER JOIN Employee_Master ON Department_Master.Department_ID = Employee_Master.Department_ID where Department_Name = ''" + DropDownList1.SelectedItem + "''";
conn = new SqlConnection("cn");
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlquery, conn);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
conn.Close();
}
}
问候,
Sara
Regards,
Sara
推荐答案
尝试使用DropDown选定值更改事件
并在页面加载时执行此操作
Try using DropDown Selected Value changed event
and on page load do this
<pre lang="midl">conn = new SqlConnection("Data Source=.;Initial Catalog=Employee;Integrated Security=True");
cmd.CommandText = "SELECT * FROM Department_Master";
cmd.Connection = conn;
conn.Open();
DropDownList1.DataSource = cmd.ExecuteReader();
DropDownList1.DataTextField = "Department_Name";
DropDownList1.DataValueField="deptID";
DropDownList1.DataBind();
conn.Close();
在更改的选定值中,尝试此==>
In Selectd Value changed try this ==>
string sqlquery = "SELECT Employee_Master.Employee_Name, Employee_Master.Date_of_join, DATEDIFF(day, Employee_Master.Date_of_join, GETDATE()) AS TotalWorkedDays FROM Department_Master INNER JOIN Employee_Master ON Department_Master.Department_ID = Employee_Master.Department_ID where DeptID="+Dropdownlist.selectedValue;
conn = new SqlConnection("cn");
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlquery, conn);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
conn.Close();
将PageLoad代码放入
Hi,
Put PageLoad code into
if (!IsPostBack)
{}
Sara,
在代码中,您定义了连接"conn",并在
Sara,
In the code you defined the connection "conn", and you opened the connection in
page_load
事件中打开了连接,因此无需在
event, so that no need to open it again in
DropDownList1_SelectedIndexChanged1
中再次打开它.因此,您可以从DropDownList1_SelectedIndexChanged1事件中删除以下代码
. So you can remove this following code from DropDownList1_SelectedIndexChanged1 event
conn = new SqlConnection("cn");
conn.Open()
并且,以上代码不是在"DropDownList1_SelectedIndexChanged1"事件中使用"selectedvaluechanged"或"selectedindexchanged"事件绑定网格的最佳实践
如果要为下拉列表框分配值
and, the above code is not a best practice to bind grid in the event "DropDownList1_SelectedIndexChanged1" use "selectedvaluechanged" or "selectedindexchanged" event
If you want to assign values to dropdownlist box
DropDownList1.DataSource = cmd.ExecuteReader();
uiCboIntType.DisplayMember = "Employee_Name";
uiCboIntType.ValueMember = "Employee_Id";
现在您可以在
中编写代码
DropDownList1_selectedvaluechanged
我认为这对您有帮助
斯里达·帕特纳亚克(Sridhar Patnayak)
Now you can write code in
DropDownList1_selectedvaluechanged
I think it is helpful to you
Sridhar Patnayak
这篇关于与Girdview的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!