[解决]如何使用'方法重载'为4种不同的方法从数据库填充DropDownList。 [英] [Solved] How to use 'Method Overloading' for 4 different Methods to fill DropDownList from Database.
问题描述
场景:
我有4种不同的下拉列表用于不同的目的。
获得员工姓名和电子邮件地址。
DATATEXTFIELD =详细信息(emp_code,Empnames)和DATAVALUEFIELD =电子邮件
获得员工权限名称。
DATATEXTFIELD = Empnames和DATAVALUEFIELD = emp_code
仅获得员工姓名第3名。
DATATEXTFIELD =详细信息(emp_code,Empnames )和DATAVALUEFIELD = emp_code
仅获得优先等级(低,正常,高,重要)。
DATATEXTFIELD = previousflag_desc和DATAVALUEFIELD = priorflag_id
我需要在另一个类(EXEMPCLASS.CS)中创建一个方法并在我的主类中调用此方法(ACCOUNT。 CS)用于每个下拉列表...
目前我正在使用这个没有重载方法的东西。
如何为所有这些DROPDOWNS创建一个完美的单一方法..
这是我的代码..
Scenario:
I have 4 different dropdownlists for different purposes.
1st to get the Employee names and email addresses.
"DATATEXTFIELD = Detail(emp_code, Empnames) and DATAVALUEFIELD = email"
2nd to get the Employee authority names.
"DATATEXTFIELD = Empnames and DATAVALUEFIELD = emp_code"
3rd to get the Employee names only.
"DATATEXTFIELD = Detail(emp_code, Empnames) and DATAVALUEFIELD = emp_code"
4th to get the Priority Levels(LOW,NORMAL,HIGH,CRITICAL) only.
"DATATEXTFIELD = priorflag_desc and DATAVALUEFIELD = priorflag_id"
All I need to create a single method in another class(EXEMPCLASS.CS) and to call this method in my main class(ACCOUNT.CS) to use for every dropdownlist...
Currently I am using this thing without overloading method.
HOW TO CREATE A PERFECT SINGLE METHOD FOR ALL THESE DROPDOWNS..
here is my code..
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (HttpContext.Current.Session["empcode"] != null)
{
getauthoptions(Session["empcode"].ToString()); // here getting the current LOGGED IN PERSON from the session to get the authority member above him from the database.
}
prioritydatabinding();
empemailbind();
popdata();
}
}
protected void popdata()
{
DataTable adp = new DataTable();
OracleConnection dbConn = Conn.getConnection();
string query = "select emp_code, (empname '-' emp_code)detail from TABLE1";
OracleCommand cmd = new OracleCommand(query, dbConn);
dbConn.Open();
OracleDataAdapter dr = new OracleDataAdapter(cmd);
dr.Fill(adp);
DropDownList4.DataSource = adp;
DropDownList4.DataTextField = "detail";
DropDownList4.DataValueField = "emp_code";
DropDownList4.DataBind();
dbConn.Close();
}
protected void empemailbind()
{
DataTable adp3 = new DataTable();
OracleConnection dbConn = Conn.getConnection();
string query3 = "select email, (empname '-' emp_code)detail from TABLE1";
OracleCommand cmd3 = new OracleCommand(query3, dbConn);
dbConn.Open();
OracleDataAdapter dr3 = new OracleDataAdapter(cmd3);
dr3.Fill(adp3);
DropDownList2.DataSource = adp3;
DropDownList2.DataTextField = "detail";
DropDownList2.DataValueField = "email";
DropDownList2.DataBind();
dbConn.Close();
}
protected void prioritydatabinding()
{
DataTable adp2 = new DataTable();
OracleConnection dbConn = Conn.getConnection();
string query2 = "select priorflag_id, priorflag_desc from TABLE2";
OracleCommand cmd2 = new OracleCommand(query2, dbConn);
dbConn.Open();
OracleDataAdapter dr2 = new OracleDataAdapter(cmd2);
dr2.Fill(adp2);
priority0.DataSource = adp2; // priority0(DROPDOWNLIST)
priority0.DataTextField = "priorflag_desc";
priority0.DataValueField = "priorflag_id";
priority0.DataBind();
dbConn.Close();
}
protected void getauthoptions(string empcode)
{
DataTable adp4 = new DataTable();
OracleConnection dbConn = Conn.getConnection();
string query4 = "select EMP_CODE, olphrm.get_name(emp_code) empname from TABLE1, TABLE3 where EMP_CODE = AUTHPARENT_ID and USER_ID=" + empcode + " order by 2";
OracleCommand cmd4 = new OracleCommand(query4, dbConn);
dbConn.Open();
OracleDataAdapter dr4 = new OracleDataAdapter(cmd4);
dr4.Fill(adp4);
DropDownList3.DataSource = adp4;
DropDownList3.DataTextField = "empname";
DropDownList3.DataValueField = "emp_code";
DropDownList3.DataBind();
dbConn.Close();
}
请帮助.....代码工作正常,因为下拉菜单完全填满但想要使用此功能在单个重载方法中。
谢谢。
PLEASE HELP.....Code is working fine as dropdowns are getting filled perfectly but want to use this in a single Overload method.
Thanks.
推荐答案
您可以为dropdownlist绑定创建一个方法获取下拉列表数据的一种方法如下所述:
You can create one method for dropdownlist binding and one method which gets data for dropdownlist as mentioned below :
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.BindData();
}
}
private void BindData()
{
DropDownList4.DataSource = this.GetData(1);
DropDownList4.DataTextField = "detail";
DropDownList4.DataValueField = "emp_code";
DropDownList4.DataBind();
DropDownList2.DataSource = this.GetData(2);
DropDownList2.DataTextField = "detail";
DropDownList2.DataValueField = "email";
DropDownList2.DataBind();
priority0.DataSource = this.GetData(3); // priority0(DROPDOWNLIST)
priority0.DataTextField = "priorflag_desc";
priority0.DataValueField = "priorflag_id";
priority0.DataBind();
DropDownList3.DataSource = this.GetData(4);
DropDownList3.DataTextField = "empname";
DropDownList3.DataValueField = "emp_code";
DropDownList3.DataBind();
}
private DataTable GetData(int type)
{
DataTable adp = new DataTable();
var query = string.Empty;
switch (type)
{
case 1:
query = "select emp_code, (empname '-' emp_code)detail from TABLE1";
break;
case 2:
query = "select email, (empname '-' emp_code)detail from TABLE1";
break;
case 3:
query = "select priorflag_id, priorflag_desc from TABLE2";
break;
case 4:
if (HttpContext.Current.Session["empcode"] != null)
{
query = "select EMP_CODE, olphrm.get_name(emp_code) empname from TABLE1, TABLE3 where EMP_CODE = AUTHPARENT_ID and USER_ID=" +
Session["empcode"].ToString() + " order by 2";
}
break;
}
if (query != string.Empty)
{
OracleConnection dbConn = Conn.getConnection();
OracleCommand cmd = new OracleCommand(query, dbConn);
dbConn.Open();
OracleDataAdapter dr = new OracleDataAdapter(cmd);
dr.Fill(adp);
dbConn.Close();
}
return adp;
}
这篇关于[解决]如何使用'方法重载'为4种不同的方法从数据库填充DropDownList。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!