Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Hints: </p> <p>You'll need to pass a System.Type to your method ...</p> <p>You'll need to use Object.GetType() and also the typeof operator. You can also create an instance of a <strong>Type</strong> of a class by using its qualified name if its in the currently executing assembly OR the <a href="http://msdn.microsoft.com/en-us/library/system.type.assemblyqualifiedname.aspx" rel="nofollow">AssemblyQualifiedName</a> </p> <p>Basic idea below:</p> <p><strong>WebForm</strong></p> <pre><code>&lt;h2&gt;Passing Types&lt;/h2&gt; &lt;asp:Panel runat="server" ID="pnl1"&gt; &lt;p&gt; &lt;asp:TextBox runat="server" ID="txt1" /&gt; &lt;/p&gt; &lt;p&gt; &lt;asp:DropDownList runat="server" ID="ddl1" /&gt; &lt;/p&gt; &lt;p&gt; &lt;asp:HyperLink runat="server" ID="hl1" Text="Link1" NavigateUrl="http://www.google.co.uk" Target="_blank" /&gt; &lt;/p&gt; &lt;/div&gt; &lt;asp:Label runat="server" ID="lout" /&gt; &lt;div&gt; &lt;p&gt; &lt;asp:DropDownList runat="server" ID="ddlTypes"&gt; &lt;/asp:DropDownList&gt; &amp;nbsp; &lt;asp:Button runat="server" ID="btnGetValueFromType" Text="Get Value By Control Type" onclick="btnGetValueFromType_Click" /&gt; &lt;/p&gt; &lt;/asp:Panel&gt; </code></pre> <p><strong>CodeBehind</strong> (<em>Apologies, its been quite a while since I did any</em> VB.net <em>so this example is in</em> C#<em>)</em></p> <pre><code> protected void Page_Load(object sender, EventArgs e) { if (! IsPostBack){ buildGui(); } } private void buildGui(){ List&lt;string&gt; lstStrings = new List&lt;string&gt;(5); lstStrings.Add("one"); lstStrings.Add("two"); lstStrings.Add("three"); lstStrings.Add("four"); lstStrings.Add("five"); ddl1.DataSource = lstStrings; ddl1.DataBind(); //Populate the Types Dropdown with some fullyqualified type names (includes the assembly name,version number, PublicKey etc.) Dictionary&lt;string, string&gt; lstTypes = new Dictionary&lt;string, string&gt;(3); lstTypes.Add(this.ddl1.GetType().Name, this.ddl1.GetType().AssemblyQualifiedName); lstTypes.Add(this.txt1.GetType().Name, this.txt1.GetType().AssemblyQualifiedName); lstTypes.Add(this.hl1.GetType().Name, this.hl1.GetType().AssemblyQualifiedName); //lstTypes.Add(this.hl1.GetType().Name, this.hl1.GetType().AssemblyQualifiedName); lstTypes.Add(this.CRJunk01.GetType().Name, this.CRJunk01.GetType().AssemblyQualifiedName); ddlTypes.DataSource = lstTypes; ddlTypes.DataValueField = "value"; ddlTypes.DataTextField = "key"; ddlTypes.DataBind(); } protected void btnGetValueFromType_Click(object sender, EventArgs e) { Type t = Type.GetType(ddlTypes.SelectedValue); lout.Text = "Looking for a type: " + t.ToString(); foreach(Control c in this.pnl1.Controls){ if (c.GetType().Equals(Type.GetType(ddlTypes.SelectedValue)))//Look for controls whose type is the same as the type selected in ddl1. { lout.Text += string.Format("&lt;br/&gt;Got Control of type: {0}", c.GetType().Name); //Cast to your custom Interface/BaseClass and extract the value. lout.Text += "&lt;br/&gt;Value is: " + ((ICrJunkControl)(c)).ControlValue; } } } </code></pre> <p>This is the sort of thing you'll probably need to 'leverage'. </p> <p>On a final note, it will probably be best if all your custom controls inherit from a base class or an interface. That way, when you've located the object you want based on its type you can cast it to the interface or base class and then get the value of a bespoke shared member e.g. <code>ControlValue</code>. The implementation of your base classes will override this. This will help to overcome problems related to disparate controls having different members that expose their state.</p> <p>e.g. </p> <p>Textbox -> Text<br> DropdownList -> SelectedValue<br> Hyperlink -> Text/NavigateUrl </p> <p>HTH,</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload