How do you set up the OnClick with ASP 2.0 ?

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default How do you set up the OnClick with ASP 2.0 ?

    In my 1.1 project, I was creating dynamically a LinkButton, and stored
    it into a Table.Row.Cell.Controls(). I was adding an MyOnClick handler
    to the Click property. I was doing this during the Load phase. It
    worked. Porting my code to 2.0, the Click event do not work anymore. It
    is not generated in the intermediate c# files created by the
    pre-compiling.

    After some investiguation on the net, it appears that one must connect
    a Click handler during the OnInit phase. But in that phase the
    ViewState is empty, so I cannot re-create my controls. So this works
    only if the control was on the postback page. Other posts seems to
    indicate that one must now use the AddAttributes to generate an
    "OnClick" attribute, calling a javascript, which will handle the detail
    of the submit if I want a post back.

    Is this the best way to generate a button click event handler, on a
    dynamically created control ?

    So many steps...

    guy Guest

  2. Similar Questions and Discussions

    1. onClick question
      I have a list menu in a form where I want to submit the form when option value is clicked.
    2. Onclick event
      I would like for certain Contribute users to be able to add on onclick event on links. How can they do this ? Thanks!
    3. onclick
      What is going with mine code ??. When I select page2 from radio button, I should have "form2" display on the page but it seems to me nothing...
    4. cfgrid and onclick
      I have a cfgrid in select mode row. On entering the page is no row selected. So I have a form button which is deactivated by default. Now, if the...
    5. OnClick problem
      Here's something I cant explain: I have a page with a form on it (checkboxes) and a hyperlink which is supposed to send the form to the server...
  3. #2

    Default Re: How do you set up the OnClick with ASP 2.0 ?

    And the answer is:

    You have to put

    guy Guest

  4. #3

    Default Re: How do you set up the OnClick with ASP 2.0 ?

    You have to add a Panel or PlaceHolder control on the page first.

    guy Guest

  5. #4

    Default Re: How do you set up the OnClick with ASP 2.0 ?

    guy wrote:
    > In my 1.1 project, I was creating dynamically a LinkButton, and stored
    > it into a Table.Row.Cell.Controls(). I was adding an MyOnClick handler
    > to the Click property. I was doing this during the Load phase. It
    > worked. Porting my code to 2.0, the Click event do not work anymore. It
    > is not generated in the intermediate c# files created by the
    > pre-compiling.
    >
    > After some investiguation on the net, it appears that one must connect
    > a Click handler during the OnInit phase. But in that phase the
    > ViewState is empty, so I cannot re-create my controls. So this works
    > only if the control was on the postback page. Other posts seems to
    > indicate that one must now use the AddAttributes to generate an
    > "OnClick" attribute, calling a javascript, which will handle the detail
    > of the submit if I want a post back.
    >
    > Is this the best way to generate a button click event handler, on a
    > dynamically created control ?
    >
    > So many steps...
    >
    Maybe I don't understand your problem, but the following code works fine
    for me:

    protected override void CreateChildControls()
    {
    LinkButton btn = new LinkButton();

    btn.Text = "Click Me";
    btn.Attributes.Add("onclick", "alert('Testing');");

    this.form1.Controls.Add(btn);
    }

    I just placed that in my Default.aspx.cs file and it works just fine.

    Am I missing something?

    Hope that helps,

    --
    Sean
    senfo Guest

  6. #5

    Default Re: How do you set up the OnClick with ASP 2.0 ?

    I believe I understand what your problem is. You are binding a
    collection to a GridView (or other databound control) and when you
    click a button you want to handle that event as a PostBack. To do that
    you can handle the RowCommand event on the GridView. That will provide
    the LinkButton as the sender argument to the event handler. What you
    want to do is attach the CommandName and CommandArgument so that button
    so you can pull it from the sender.

    To set the properties I handle the RowDataBound event. I use that to
    get to the LinkButton when the DataItem is defined so I can set the
    CommandArgument to a value which make sense for the event handler.

    [url]http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.as px[/url]

    If you are doing something other than a GridView, the technique is
    similar. There should be a RowCommand event while the event to bind to
    the row or item will be named differently.

    This sounds a little confusing at first, but once you have done it a
    few times it is easy. Let me know if you have any questions.

    Brennan Stehling
    [url]http://brennan.offwhite.net/blog/[/url]


    guy wrote:
    > In my 1.1 project, I was creating dynamically a LinkButton, and stored
    > it into a Table.Row.Cell.Controls(). I was adding an MyOnClick handler
    > to the Click property. I was doing this during the Load phase. It
    > worked. Porting my code to 2.0, the Click event do not work anymore. It
    > is not generated in the intermediate c# files created by the
    > pre-compiling.
    >
    > After some investiguation on the net, it appears that one must connect
    > a Click handler during the OnInit phase. But in that phase the
    > ViewState is empty, so I cannot re-create my controls. So this works
    > only if the control was on the postback page. Other posts seems to
    > indicate that one must now use the AddAttributes to generate an
    > "OnClick" attribute, calling a javascript, which will handle the detail
    > of the submit if I want a post back.
    >
    > Is this the best way to generate a button click event handler, on a
    > dynamically created control ?
    >
    > So many steps...
    Brennan Stehling Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139