开发者

Force an event handler to fire for a custom control on every postback

开发者 https://www.devze.com 2023-02-10 04:55 出处:网络
I suspect my issue revolves around a poor understanding of how custom event handlers work in ASP.NET. I\'ve read several articles but the target audiences have been attempting something different from

I suspect my issue revolves around a poor understanding of how custom event handlers work in ASP.NET. I've read several articles but the target audiences have been attempting something different from what I am.

I've created a UserControl that is dynamically placed onto my page during the Init event. It populates and works great, but I'm left scratching my head at how to retrieve data from it during a postback.

It's effectively a multipick combobox that is manipulated entirely using clientside JavaScript (I'm using jQuery heavily). Inside the control container element, I render an empty DIV element that contains all of the selected items. As the user selects items, I generate DIVs within that container DIV that includes some markup. Of interest to me on the server side are the contents of a couple of INPUT fields.

After selecting an item, the rendered HTML looks something like:

<div class="multiPickContainer">
  <div class="multiPickItem">
    <input type="text" />
    <input type="hidden" />
  </div>
</div>

The problem, of course, is that these selected items were generated on the client side and the server doesn't have any knowledge of them (e.g. there's no runat="server" - it's pure HTML).

On any post[back], I'd like an event handler to fire within my custom control that enables me to examine wh开发者_JAVA技巧at elements are in this container DIV. I'd like this event to fire regardless of whether the user made any selections (the control may have been prepopulated, in which case I'd still like to know what selections are in it).

What I'm after is something like this:

private GenerateControl(TemplateControl page){
  var control = page.LoadControl("~/MyControl.ascx") as MyControl;
  control.OnPostBack += HandlePostback;
}

I may be completely off track and this may actually be impossible (or a stupid idea). The only alternative I can think of is to emit actual ASP.NET controls and hook the built-in event handlers.

Can anyone make any suggestions, or point me to any good literature that discusses creating custom event handlers?


If you give your inputs a name attribute, the values will show up in Request.Form

0

精彩评论

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