Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You may not necessarily need to use postbacks, but if you want to, you need to use something called <code>delegates</code>.<br/> check it out over here: <a href="http://www.akadia.com/services/dotnet_delegates_and_events.html" rel="nofollow noreferrer">link</a><br/></p> <p>Even if you don't use them, they are useful, and will allow you to add "custom" events to your user control.</p> <hr> <p><strong>Edit:</strong><br/> Here is some code you can use:<br/> First you define your delegate type. Think of a delegate like a function pointer. You will definte this in your code-behind:<br/></p> <pre><code>public delegate void OnChangeDelegate(object sender, EventArgs e); </code></pre> <p>Then you will define the actual event to be of type <code>OnChangeDelegate</code> type. <br/></p> <pre><code>public event OnChangeDelegate Change; </code></pre> <p>Then you create a method (or two) which "fires" your event:<br/> <em>(this is in your ascx.cs file)</em></p> <pre><code>public void ItemChange(object sender, EventArgs e) { if (sender != null &amp;&amp; sender is LinkButton) { string idx = ((LinkButton)sender).CommandArgument; FireMyChangeEvent(sender, idx); } </code></pre> <p>Also</p> <pre><code>private void FireMyChangeEvent(object sender, string index) { if (MyItemChange != null) { EventArgs e = new EventArgs(); Change(sender, new ItemSelectorEventArgs(index)); } } </code></pre> <p>So anywhere in your codebehind, you can pass a handle to the control and some extra stuff in the EventArgs object.</p> <p>Now the tricky part is getting your <code>&lt;li&gt;</code> s to cause a postback. Traditionally, you would use a <code>&lt;select&gt;</code> and <code>&lt;option&gt;</code>s because of the native <code>onclick</code> event, but since you are rendering a <code>&lt;ul&gt;</code> and <code>&lt;li&gt;</code>s You might want to wrap them with an <code>&lt;asp:LinkButton runat="server"&gt;</code> so you get an event firing, instead of the <code>&lt;a href="#"&gt;</code>, so you can say <code>OnClick</code> for that LinkButton, and in that event, you can just call your <code>ItemChange</code> method to fire your control's event, and pass the id of the item which you can send in the EventArgs of your event.</p> <p>I would definitely recommend reading the link I sent you, and remember that unless a control has <code>runat="server"</code> your server will not have any handle on it.</p> <p>**Update*: Here is some more code.</p> <p><strong>MY ASPX Code</strong> Note here that <code>WebUserControl1</code> is my user control and <code>MyClick</code> is the method defined in ASPX.cs code.<br/></p> <pre><code>&lt;ul&gt; &lt;uc1:WebUserControl1 ID="WebUserControl11" runat="server" OnChange="MyClick"/&gt; &lt;/ul&gt; </code></pre> <p><strong>My ASCX Code</strong> Note here that the CommandArgument is used to specify which item is clicked. Also, you can use this for a database id or some such value, I just use a simple integer ordinal.<br/></p> <pre><code>&lt;li&gt;&lt;asp:LinkButton ID="lb1" runat="server" OnClick="ItemChange" CommandArgument="1"&gt;item1&lt;/asp:LinkButton&gt;&lt;/li&gt; &lt;li&gt;&lt;asp:LinkButton ID="lb2" runat="server" OnClick="ItemChange" CommandArgument="2"&gt;item2&lt;/asp:LinkButton&gt;&lt;/li&gt; &lt;li&gt;&lt;asp:LinkButton ID="lb3" runat="server" OnClick="ItemChange" CommandArgument="3"&gt;item3&lt;/asp:LinkButton&gt;&lt;/li&gt; </code></pre> <p><strong>In APX.CS Code</strong><br/> Note here that <code>lblResult</code> is a simple<br/></p> <pre><code>&lt;asp:Label ID="lblResult" runat="server" /&gt; </code></pre> <p>I used this just for display purposes </p> <pre><code>protected void MyClick(object sender, EventArgs e) { int myitem = ((WebUserControl1.ItemSelectorEventArgs)e).ItemSelected; lblResult.Text = "You clicked Item " + myitem.ToString() + "!"; } </code></pre> <p><strong>Lastly</strong><br/></p> <p>This is the Custom Event Args class I created in my <code>ascx.cs</code> code:</p> <pre><code>public class ItemSelectorEventArgs : EventArgs { public int ItemSelected { get; set; } public ItemSelectorEventArgs(string indexSelected) { int tryGetANumber; if (int.TryParse(indexSelected, out tryGetANumber)) { this.ItemSelected = tryGetANumber; } else { this.ItemSelected = -1; } } } </code></pre> <p><strong>Remember</strong><br/> You are trapping the events of the HTML Controls inside your user control, gathering some info from them, and firing a custom event on your user control.</p>
    singulars
    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.
 

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