command and click events

Discussion in 'ASP.NET / ASP.NET Core' started by Malin, Nov 16, 2004.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. Hi

    I have an application that is looping some articles out (information about each of them in amall tables). Every articletable includesone "publish button".

    Like:

    "Aticle one"
    By: Malin
    Date: 2004-04-04
    PUBLISH



    "Aticle two"
    By: Malin
    Date: 2004-05-05
    PUBLISH


    I have set CommandArgument to the articleid and then when command event fires when the button is clicked the method btnReg_command is invoked. In the method I take out the articleid from e.commandargument and puts it into the session object. Then I want to Resonse.Redirect to a pop up window. I know that this is not possible (from what I have read about it) so I have tried to solve it by adding the javascript to open the popup in the onclick event. Now this is the irritating part. Sometimes the commandevent fires more slowly then the clickevent and this gives the result that when the pop up opens the articleid from the oncommand method is not in place in session when the pop up loads and the articleid is used in the popup page's code behind. Sometimes the articleid is in the session object in time..and sometimes not.

    Is there a way I can make this work??

    Here is parts of my code...the relavant parts:

    The Button code:
    ----------------------------------------------------




    System.Web.UI.WebControls.Button btnReg = new Button();


    btnReg.Command += new System.Web.UI.WebControls.CommandEventHandler(this.btnReg_Command);


    btnReg.CommandArgument = dsResult.Tables[0].Rows.ItemArray[0].ToString(); (the articleid)


    btnReg.Attributes.Add("onclick", "javascript:window.open('regPublish.aspx','regwin','scrollbars=no,width=530,height=500');");





    The commandevent method code:
    ----------------------------------------------------



    protected void btnReg_Command(object sender, System.Web.UI.WebControls.CommandEventArgs e)


    {


    Session.Add("chosenArticle", e.CommandArgument.ToString());


    }





    -------------------------


    The problem is that when regPublish.aspx is opened it uses the "chosenArticle" in the session object and sometimes it is not there because the pop up is loaded faster than the commandevent i fired..I think??


    Any ideas about how I can solve this problem?


    Malin


    (I cannnotmake the blue text black ..sorry)
     
  2. Sounds like you want to do something like registering some client script that will basically open up your new window

    so on the commandevent or button click event

    just register some client javascript to do this using window.open

    I dont do much C#, but asp.net the code would be something like

    dim sb as New StringBuilder("<script language=""javascript"">window.open(url, '', args);</script>")

    page.RegisterClientStartupScript(sb.ToString())

    this will register the script to execute when the page loads up, so you should have the effect of the postback happening and after the postback is finished the client script will execute that will open up
    your popup window.

    I dont have access to my code or resources at the moment so some of the calls above maybe misspelt or something... Im getting old :)

    FYI:EVERYTHING ISPOSSIBLE :)

    Mikey
     
  3. Hi Michael

    I dont understand this I think.

    What exactly do I have to register to the button click-event?


    You wrote:
    "dim sb as New StringBuilder("<script language=""javascript"">window.open(url, '', args);</script>")
    page.RegisterClientStartupScript(sb.ToString())"


    How is the button going to know when to fire this? And also, how does the button know that it is supposed to fire this at all?

    The registerclientstartupscript is being registered to the page..so where does my button get involved?

    If this registerclientstartupscript is fired when page loads up ..does it fire when page is not postback as well?

    The popup is just supposed to open when a particular button is clicked. The page can be posted back to server in many other occasions so the pop up cannot be opened a a result of each post back.

    Everything is possible..=) yes I hope so. Sometimes it is just so hard to find a solution and thiswas really basic functionality in asp - now it seems so tricky with asp.net... At first I thought it was of course possible to just respone.redirect to a popup win.. why not? just response.redirect to a new window.. Why is that so impossible..I wonder..

    Thanks for your help [​IMG]
     
  4. Hi Malin,

    you put that code I suggested in the click event of the command button that you are posting back to, so that javascript will only be registered when the postback of that button event happens. understand ?

    dont forget nothing is rendered on the page yet, even the command events are happening for the button clicked for example, so you can put as much javascript as you want to happen in like this and it will only get registered when that button click happens, you page is completely rebuilt on a post back, it does not magically stay like it is and a postback happens it just seems to be like that.

    another alternative could be to do something like the below

    on the page_load (VB code, sorry)

    command_button.attributes.add("onclick","javascript:window.open('secondpage.aspx?articleid=1', '', args); return false;")

    build your command argument into the url like you are already doing (e.g secondpage.aspx?articleid=1). When you press the button client side script will execute the window.open as you require and the return false will stop the postback from happening, which you dont want now, if you want the postback anyway, then just make it return true

    hope this clarifies
    Michael
     
  5. Thanks for your answers Michael


    I hope that I soon will find the time to try your suggestions (right now I must do only boring things.. [​IMG] )





    Malin

    ~Postingat the DASP-forum since thebeginning ~
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page