开发者

How can i pass asp.net server control type in function?

开发者 https://www.devze.com 2023-02-22 03:33 出处:网络
I am writing a function in my class file which connects to the db and loads the data. I want to pass a query, asp.net control type and control id to the function, such that when i call the function ,

I am writing a function in my class file which connects to the db and loads the data. I want to pass a query, asp.net control type and control id to the function, such that when i call the function , it can load the data ba开发者_如何学Csed on control type(where i will be writing a switch case for each control type). Some thing like this :

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;
}

Can anybody tell me how can i pass Controltype and controlId in this function.


Here is how you can do it:

 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;
    }
}

I have one CheckBoxList and DropDownList in my HTML code as below:

 <asp:CheckBoxList ID="chkBxList" runat="server">
  </asp:CheckBoxList>
  <asp:DropDownList ID="ddlList" runat="server"/>

Hope this helps...


Try this code:

DropDownList Ll = new DropDownList();
Ll = (DropDownList)control;
0

精彩评论

暂无评论...
验证码 换一张
取 消