GetPostBackEventReference - dynamic argument

Discussion in 'ASP .Net Building Controls' started by Monty, Jan 15, 2007.

  1. Monty

    Monty Guest

    Hello,

    I'm building a sever control that is essentially a container that holds a
    bunch of DIVs. I want the click event on any of the DIVs to bubble up to the
    container which calls the __doPostback procedure as given by the server-side
    call to ScriptManager.GetPostBackEventReference(Me, sArg). I only want to
    have the __doPostback call on the table level, not on each and every Div
    (there are a lot). So, essentially I would like my call to look like this:

    __doPostback ('My$Control1', e.srcElement.id)

    Unfortunately, GetPostBackEventReference provides the second argument in
    quotes, so it looks like this:

    __doPostback ('My$Control1', 'e.srcElement.id')

    And therefore my return argument is the literal value "e.srcElement.id", not
    the ID of the source element as I would like. Any help here? I could
    hardcode the call like this:

    "__doPostback(" & me.UniqueID & ", e.srcElement.id)"

    but I'm trying to do it the "right" way. Any suggestions?? TIA,

    Monty
     
    Monty, Jan 15, 2007
    #1
    1. Advertising

  2. Hello Monty,

    From your description, you're developing a custom ASP.NET webserver control
    which need to do the following things:

    ** include multiple <div> elements
    ** let the each <div> raise a control postback event when user click on it
    ** pass the client-side <div> ID (which trigger the postback event) into
    the __doPostBack client procedure,

    is this correct?

    Based on my research, when you use
    Page.ClientScript.GetPostBackEventReference method, it will always treat
    the second parameter(argument) as a plain string text and the output script
    statement will wrapper it with a single quote pair( ''). If you want to
    embeded script code, you can do an additional replace on the returned
    script statement. e.g.

    ========================
    protected override void OnPreRender(EventArgs e)
    {
    base.OnPreRender(e);

    string dopost1 =
    Page.ClientScript.GetPostBackEventReference(this, "{0}");
    string dopost2 =
    Page.ClientScript.GetPostBackEventReference(this, "{0}");


    div1.Attributes["onclick"] = dopost1.Replace("'{0}'",
    "event.srcElement.id");
    div2.Attributes["onclick"] = dopost2.Replace("'{0}'",
    "event.srcElement.id");
    }
    =========================

    Or if you're also attaching an "onclick" handle for each "<div>" tag, you
    can consider directly embeded the div's "ClientID" statically, e.g.

    ===================
    protected override void OnPreRender(EventArgs e)
    {
    base.OnPreRender(e);

    string dopost1 =
    Page.ClientScript.GetPostBackEventReference(this, div1.ClientID);
    string dopost2 =
    Page.ClientScript.GetPostBackEventReference(this, div2.ClientID);


    div1.Attributes["onclick"] = dopost1;
    div2.Attributes["onclick"] = dopost2;
    }
    =========================

    Hope this helps some.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead



    ==================================================

    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications.



    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscriptions/support/default.aspx.

    ==================================================



    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Jan 16, 2007
    #2
    1. Advertising

  3. Monty

    Monty Guest

    Yes, that first method helps. Much better than hard-coding. Thank you!

    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > Hello Monty,
    >
    > From your description, you're developing a custom ASP.NET webserver
    > control
    > which need to do the following things:
    >
    > ** include multiple <div> elements
    > ** let the each <div> raise a control postback event when user click on it
    > ** pass the client-side <div> ID (which trigger the postback event) into
    > the __doPostBack client procedure,
    >
    > is this correct?
    >
    > Based on my research, when you use
    > Page.ClientScript.GetPostBackEventReference method, it will always treat
    > the second parameter(argument) as a plain string text and the output
    > script
    > statement will wrapper it with a single quote pair( ''). If you want to
    > embeded script code, you can do an additional replace on the returned
    > script statement. e.g.
    >
    > ========================
    > protected override void OnPreRender(EventArgs e)
    > {
    > base.OnPreRender(e);
    >
    > string dopost1 =
    > Page.ClientScript.GetPostBackEventReference(this, "{0}");
    > string dopost2 =
    > Page.ClientScript.GetPostBackEventReference(this, "{0}");
    >
    >
    > div1.Attributes["onclick"] = dopost1.Replace("'{0}'",
    > "event.srcElement.id");
    > div2.Attributes["onclick"] = dopost2.Replace("'{0}'",
    > "event.srcElement.id");
    > }
    > =========================
    >
    > Or if you're also attaching an "onclick" handle for each "<div>" tag, you
    > can consider directly embeded the div's "ClientID" statically, e.g.
    >
    > ===================
    > protected override void OnPreRender(EventArgs e)
    > {
    > base.OnPreRender(e);
    >
    > string dopost1 =
    > Page.ClientScript.GetPostBackEventReference(this, div1.ClientID);
    > string dopost2 =
    > Page.ClientScript.GetPostBackEventReference(this, div2.ClientID);
    >
    >
    > div1.Attributes["onclick"] = dopost1;
    > div2.Attributes["onclick"] = dopost2;
    > }
    > =========================
    >
    > Hope this helps some.
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    >
    > ==================================================
    >
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    > ications.
    >
    >
    >
    > Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    > where an initial response from the community or a Microsoft Support
    > Engineer within 1 business day is acceptable. Please note that each follow
    > up response may take approximately 2 business days as the support
    > professional working with you may need further investigation to reach the
    > most efficient resolution. The offering is not appropriate for situations
    > that require urgent, real-time or phone-based interactions or complex
    > project analysis and dump analysis issues. Issues of this nature are best
    > handled working with a dedicated Microsoft Support Engineer by contacting
    > Microsoft Customer Support Services (CSS) at
    > http://msdn.microsoft.com/subscriptions/support/default.aspx.
    >
    > ==================================================
    >
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no
    > rights.
    >
    >
    >
    >
     
    Monty, Jan 16, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bort

    GetPostBackEventReference

    Bort, Feb 19, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    5,624
  2. Trevor Benedict R
    Replies:
    1
    Views:
    446
    =?Utf-8?B?RGF2ZQ==?=
    Jun 19, 2004
  3. Mr Newbie
    Replies:
    1
    Views:
    3,602
    Mr Newbie
    Nov 7, 2005
  4. Bhushit Joshipura

    defaulting argument to previous argument

    Bhushit Joshipura, Dec 29, 2003, in forum: C++
    Replies:
    5
    Views:
    416
  5. =?Utf-8?B?TWFyaw==?=

    Handling OnClick and GetPostBackEventReference

    =?Utf-8?B?TWFyaw==?=, Mar 5, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    2,673
    =?Utf-8?B?TWFyaw==?=
    Mar 6, 2007
Loading...

Share This Page