Note that there are some explanatory texts on larger screens.

plurals
  1. POasp.net In a repeater is it possible to call a public function from another class?
    text
    copied!<p>Say I have this repeater which makes use of a public function called Test in the code-behind.</p> <pre><code>&lt;asp:Repeater ID="Repeater1" runat="server"&gt; &lt;HeaderTemplate&gt; &lt;table&gt; &lt;/HeaderTemplate&gt; &lt;ItemTemplate&gt; &lt;tr&gt; &lt;td&gt;&lt;%# Eval("MyCol1")%&gt;&lt;/td&gt; &lt;td&gt;&lt;%# Eval("MyCol2")%&gt;&lt;/td&gt; &lt;td&gt;&lt;%# Test((int)Eval("MyCol1"))%&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/ItemTemplate&gt; &lt;FooterTemplate&gt; &lt;/table&gt; &lt;/FooterTemplate&gt; &lt;/asp:Repeater&gt; </code></pre> <p>In my code-behind, I have this function</p> <pre><code>public string Test (int Value) { return "Test"+Value.ToString(); } </code></pre> <p>This works fine but in my website I will have similar repeaters on various pages and most of them will need to call the Test function. Instead of having it in the code-behind of each web page would it be possible to put it in a public static class and call it directly from the repeater? Something like this (which does not work):</p> <pre><code>&lt;td&gt;&lt;%# MyStaticClass.Test((int)Eval("MyCol1"))%&gt;&lt;/td&gt; </code></pre> <p>The only solution I've come up with would be to change the function in the code-behind to:</p> <pre><code>public string Test (int Value) { return MyStaticClass.Test(Value); } </code></pre> <p>But it would be neater if I didn't have to put any code in the code-behind of each webpage (ie I would prefer the repeater to call the static function directly).</p> <p>Any ideas or suggestions?</p>
 

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