locked
event problem RRS feed

  • Question

  • User1965945065 posted

    Hi, I have a problem with events, actually I don't have an idea how could it be solved so I'm asking you

    I have 4 custom controls, first one is called init and it's only one wich is placed on aspx page, only thing that this init control has to do is determine "state", by that state it determines what is next control that it has to set as it's child control, In this specific case it chooses install as its child control. Install control renders some html and sets two more controls as its children. these controls are form, and button, but they are programed so that they always render same html

    form control, and two button controls generate following output:
    <form id="mainform" name="mainform" method="post" action="Default.aspx">
     <input id="__EVENTTARGET" name="__EVENTTARGET" type="hidden" value="" />
     <input id="__EVENTARGUMENT" name="__EVENTARGUMENT" type="hidden" value="" />
     <script type="text/javascript">
      var theForm = document.forms['mainform'];
      if (!theForm)
       {
        theForm = document.mainform;
       }
      function __doPostBack(eventTarget, eventArgument)
       {
        if (!theForm.onsubmit || (theForm.onsubmit() != false))
         {
          theForm.__EVENTTARGET.value = eventTarget;
          theForm.__EVENTARGUMENT.value = eventArgument;
          theForm.submit();
          }
        }
     </script>
    </form>
    <input id="dec" type="button" value="Back" onclick="javascript:__doPostBack('ctl02','dec')" class="installbtn" />
    <input id="inc" type="button" value="Next" onclick="javascript:__doPostBack('ctl03','inc')" class="installbtn" />

    This output is little modified source from this link
    Reason for not using htmlcontrols.htmlform and standard asp.net button is that asp.net form uses viewstate no matter I have disabled it on more places, so I have improvised on it by doing it this way. As you can see input tags aren't part of the form, but postback works anyway. Problem is that it works only under two conditions and they are that install control renders html code from button and form control above by itself (not adding it as child), and if its placed directly on aspx page, like <custom:install runat="server" />. Postback always happens but raisepostbackevent sub never triggers. So what I want to do is keep this button and form control because same two conditions where required with htmlcontrols.htmlfrom and asp.net button, except that I would like that sub wich is triggered on event stays in my button control, but it wouldn't be any problem that it goes up control hierarchy to install or init controls, but the thing is I don't know how could this problem be solved. Event bubbling isn't what I need because this is kind of a public event because it changes values in shared variable wich is stored in special class, so it's not like one control fires event wich must be executed in control up the hierarchy, in my case it doesn't really matter where is event executed, except as I have said I would like that it is executed in my button control with raisepostbackevent sub, but the problem is that after postback it isn't executed

    I have been adding child controls on following way: in createchildcontrols() I have added Controls.Add(install), and I haven't been using ensurechildcontrols
    Sorry if my english is bad

    Friday, August 22, 2008 1:09 PM

Answers

  • User-16411453 posted

     It's possible to fix your so called control, by viewing the actual source from the browser.  There you will see that the id on the form tag changed, and some other id's changed as well.

    The use of JavaScript for your control is poor practice.  Best Practice is to let the server do the work instead of the client, so the server can track the states of the control and provide reliable and accurate function. Change your control to work on the server side.

    On the buttons causing postback, or asyncpostback if using a update panel, you have to add an event handler usually call "addHandler" and make a trigger if using a update panel. AddHandler point to an event usually labeled

    Protected Sub ibApply_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)

            System.Threading.Thread.Sleep(500)
            Dim Context As HttpContext = HttpContext.Current
            Save_Data()
            Context = Nothing

        End Sub

    for an ImageButton 

     

     

    ibApply = New ImageButton
    	With ibApply
    .ID = [ID] & "_ibReply"
    .ImageAlign = ImageAlign.AbsMiddle
    AddHandler ibApply.Click, AddressOf ibApply_Click
    .ImageUrl = cs.GetWebResourceUrl(rsType, "ICE5Software.Admin_SetupV140.GoldApply.gif")
    .ValidationGroup = "_Basic"
    End With
    tdDone_Right.Controls.Add(ibApply)

    Dim ibReply_Trigger As AsyncPostBackTrigger
    ibReply_Trigger = New AsyncPostBackTrigger
    With ibReply_Trigger
    .ControlID = [ID] & "_ibReply"
    .EventName = "Click"
    End With
    UpdatePanel_Setup_DynamicCart_Activation.Triggers.Add(ibReply_Trigger)
      
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 22, 2008 5:34 PM

All replies

  • User-16411453 posted

     It's possible to fix your so called control, by viewing the actual source from the browser.  There you will see that the id on the form tag changed, and some other id's changed as well.

    The use of JavaScript for your control is poor practice.  Best Practice is to let the server do the work instead of the client, so the server can track the states of the control and provide reliable and accurate function. Change your control to work on the server side.

    On the buttons causing postback, or asyncpostback if using a update panel, you have to add an event handler usually call "addHandler" and make a trigger if using a update panel. AddHandler point to an event usually labeled

    Protected Sub ibApply_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)

            System.Threading.Thread.Sleep(500)
            Dim Context As HttpContext = HttpContext.Current
            Save_Data()
            Context = Nothing

        End Sub

    for an ImageButton 

     

     

    ibApply = New ImageButton
    	With ibApply
    .ID = [ID] & "_ibReply"
    .ImageAlign = ImageAlign.AbsMiddle
    AddHandler ibApply.Click, AddressOf ibApply_Click
    .ImageUrl = cs.GetWebResourceUrl(rsType, "ICE5Software.Admin_SetupV140.GoldApply.gif")
    .ValidationGroup = "_Basic"
    End With
    tdDone_Right.Controls.Add(ibApply)

    Dim ibReply_Trigger As AsyncPostBackTrigger
    ibReply_Trigger = New AsyncPostBackTrigger
    With ibReply_Trigger
    .ControlID = [ID] & "_ibReply"
    .EventName = "Click"
    End With
    UpdatePanel_Setup_DynamicCart_Activation.Triggers.Add(ibReply_Trigger)
      
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 22, 2008 5:34 PM
  • User1965945065 posted

    ok, now you try something: for start make one simple control and name it control1. put it on page like <custom:control1 runat="server" />, do not put anything else on that page, nothing else musn't be on that page. then in that control dim new htmlcontrols.htmlform, and add it as a child to your control by doing controls.add(formname), then insert button into that new form by doing formname.controls.add(button), after doing that add handlers and subs wich will execute on event, this worked for me just fine, but then create another simple control and name it control2. then add control1 as a child to control2 by doing in control2: controls.add(control1). then delete <custom:control1 runat="server" /> from page and insert <custom:control2 runat="server" />. and everything will remain just the same except what ever control you place handlers event will act just like it never triggered, but the postback exists, maybe it is something that I do wrong

    before using my method I have seen that it's obsolete, but as I see things difference isn't huge, please tell me if I am wrong
    let's say server gets request for some page and than he starts controls, and goes through preinit, init, initcomplete, preload, load, etc... then when he has rendered code he will response to the client with rendered code from control, and rendered code from page wich contains control and wich client requested. after he has sent code he will close all work he has done and he will wait for next request.
    now let's say that next request is postback from the page I have created up there. when that input tag is pressed he will start javascript function wich will store input's name into a hidden field of some form, then javascript code will submit form to the server as a request. now when server gets request he will read request and he will search specific hidden fields from form and if he doesn't see any values in it he will continue as usual, just like client clicked refresh or client sent first request wich doesn't contain any hidden values, but if server sees something he will determine event based on that data and then he will initialize another sub and forward data he has read to that sub or void function, than that sub will do something also based on that data. that something usually changes rendering so we get another output. in other words in base of request server will create necessary response. this is also why you sould get error when placing two forms on one page, let's say you did that, server will then write hidden fields into both forms but values will be written only on hidden filed of one form and than server won't know from what form he should read hidden fields, even that values are written on all hidden fields server would get pretty confused (only not if he would know when and wich form he should read).

    as I have said I don't see difference if I let asp.net to create a form in wich he will insert some hidden fields, and button that will submit form directly, or if I make javascript function wich will be initialized on a button click, and wich will store values into hidden fields and than submit form, what server does is the same from my view, and if somebody could say to my javascript to save something else into hidden fields and by that way "hack" my app he could do it also through input that has asp.net generated. I have decided this way because I can have bigger control what is posted to the server, now it is me who is telling javascript what to store and then submit not asp.net who has some global standards on what is posted, and that global standards are used on lot of pages, and if security is mattered then my page is kind of more secure because it is different and needs more time to study on it, also it is unique so if someone figures out that he could get lets say admin panel if he tells my javascript to submit something else it would only worth for this page, but if he figures out how to post different values in asp.net standards he would get chances to hack globally

    EDIT: I have fixed this problem yesterday (my yesterday), and I have done following:
    my form and button control are now rendering this html: (it is little more modified, and simplified)
    <form id="mainform" name="mainform" method="post" action="Default.aspx">
     <input id="store" name="store" type="hidden" value="" />
     <script type="text/javascript">
      function postback(btn)
       {
        if (!mainform.onsubmit || (mainform.onsubmit() != false))
         {
          mainform.store.value = btn;
          mainform.submit();
         }
        }
     </script>
    </form>
    <input id="dec" type="button" value="Back" onclick="javascript:postback('dec')" class="installbtn" />
    <input id="inc" type="button" value="Next" onclick="javascript:postback('inc')" class="installbtn" />

    then in the server code there is:
    if page.request.form("store") <> string.empty then
    if page.request.form("store") = "dec" then
     'do something
    end if
    if page.request.form("store") = "inc" then
     'do something else
    end if
    end if

    this server side code is executed in install control wich adds form and buttons as its children and wich is child of init control

    if you have any more ideas how could it be solved I am all ears, jkirkerx I will mark your post as answer because you tried to help me [;)]

    Saturday, August 23, 2008 6:11 AM