我正在我的类文件中编写一个函数,它连接到db并加载数据。我想将一个查询,asp.net控件类型和控件id传递给该函数,这样当我调用该函数时,它可以根据控件类型加载数据(我将为每个控件类型编写一个switch case) 。有点像这样:
public static string LoadData(string qry, Controltype, controlId)
{
string connstring = "......";
OleDbDataAdapter da = new OleDbDataAdapter
DataTable dt = new DataTable();
OleDbConnection con = new OleDbConnection(connstring);
con.Open();
da = new OleDbDataAdapter(qry, connstring);
dt = new DataTable();
da.Fill(dt);
switch(controltype)
{
case DropDownList:
......
......
break;
case GridView:
......
......
break;
}
有人能告诉我如何在这个函数中传递Controltype和controlId。
答案 0 :(得分:1)
以下是如何做到这一点:
protected void Page_Load(object sender, EventArgs e)
{
LoadData(ddlList, ddlList.ID);
LoadData(chkBxList, chkBxList.ID);
}
void LoadData(WebControl control, string id)
{
string typeName = control.GetType().Name;
switch (typeName)
{
case "DropDownList":
//do something here
break;
case "CheckBoxList":
//do something here
break;
}
}
我的HTML代码中有一个CheckBoxList和DropDownList,如下所示:
<asp:CheckBoxList ID="chkBxList" runat="server">
</asp:CheckBoxList>
<asp:DropDownList ID="ddlList" runat="server"/>
希望这会有所帮助......
答案 1 :(得分:-1)
试试这段代码:
DropDownList Ll = new DropDownList();
Ll = (DropDownList)control;