Load Web User Control in cs file
I created a web project with ASP.NET. I get several WUC and .aspx web pages.
Now I'd like to create an "utility.cs" file to avoid rewriting always the same code, but one method I'd move into utility.cs use a WUC component. So how can I "load" WUC into a .cs file?Summarily:
utility.cs:
public static class utility
{
public void pLoadDDL(DropDownList ddl, string query, string idField, string descField){...}
public void pLoadCheckableDropDownList(MultiSelectDropDown msdd, string queryPopola,string queryVista, string strIdField, string strDataField){...}
}
Multi SelectDropDown is a WUC. If I had added this code into a web page (.aspx) I would have be able to add a directive like this:
<%@ Register TagPref开发者_运维技巧ix="ddms" TagName="MultiSelectDropDown" src="WUC/MultiSelectDropDown.ascx" %>
that I can't add into a simple .cs file. And I don't want to move my code to an aspx file because it's only a file from witch code is copied...it's not a web page.
Thanks for your help.
Assuming WUC is a Web User Control, look at Page.LoadControl ().
You'll need something like:
MultiSelectDropDown dropDown = (MultiSelectDropDown) Page.Load("WUC/MultiSelectDropDown.ascx");
From there you can pass the dropDown
to your utility method.
Alberto what is the error?
Is your code above not compiling? just put a using or the fully qualified name which includes the namespace instead of only MultiSelectDropDown
My answer on this anyway, in general, is that if you are making a utility class useful and used by all controls you should not couple it with the specific Web User Controls you have written or you will/could end up having circular references.
if this is an utility class and all your WUC will eventually use it keep it generic and put the method which deals with custom controls like MultiSelectDropDown
inside the MultiSelectDropDown
itself, eventually as public method which could be called from all pages or from other classes.
Here is an MSDN article how to Dynamically load usercontrols from Code Behind
http://msdn.microsoft.com/en-us/library/c0az2h86.aspx
@StefanE : Maybe you didn't read my question carefully, or maybe my english is not good enough to explain well my issue. I wrote:
"Multi SelectDropDown is a WUC. If I had added this code into a web page (.aspx) I would have be able to add a directive like this:
<%@ Register TagPrefix="ddms" TagName="MultiSelectDropDown" src="WUC/MultiSelectDropDown.ascx" %>
that I can't add into a simple .cs file."
As you can see in your article "ASP.Spinner" is defined as variable into the same aspx page where the WUC is defined. So programmer can write something like:
ASP.Spinner myvariable = (ASP.Spinner)Page.LoadControl("full wuc path and name");
But if "ASP.Spinner" is not defined in the same context of the WUC(i.e. in my case, where I have to write several methods to fill/clear/and so on the WUC into a pure .cs file) ..........you can't.
Easy ;)
Here is an MSDN article how to Dynamically load usercontrols from Code Behind
精彩评论