can i prevent a button from posting back when clicked?

Discussion in 'ASP .Net' started by bill, Jul 18, 2006.

  1. bill

    bill Guest

    I dynamically create buttons and associate them with an event using
    AddHandler.

    I want all the button events to fire at one time, when the page is posted,
    instead of when each button is clicked.

    How I stop the buttons from posting back when they are clicked?

    Thanks
     
    bill, Jul 18, 2006
    #1
    1. Advertisements

  2. bill

    neilmcguigan Guest

    why would you want to do that?
     
    neilmcguigan, Jul 18, 2006
    #2
    1. Advertisements

  3. bill

    bill Guest

    If I tell you why, will you tell me how?

    My page is built dynamically with a number of controls for data entry
    purposes (like a questionnaire), including radio button lists, dropdown
    lists, checkbox lists, textboxes... Each control is wired to a common event
    handler which processes the input when the page is posted.

    In addition, I have constructed a scale or slider control for input which
    consists of a table with 100 cells, each containing a button corresponding
    to a value between 1 and 100. The value is the command argument of the
    button. When one of the buttons is clicked it fires an event, and the
    selected value is saved. This event should be the common event handler used
    by the other controls on the page.

    I want all the control events to be processed one time when the page is
    posted.

    Thanks
    Bill
     
    bill, Jul 18, 2006
    #3
  4. Shouldn't you use radio buttons, or checkboxes, or dropdowns, or some other
    controls to let your users answer the questionaire?

    A button indicates "do something now".

    After the user clicks a button to select an answer, the button isn't clicked
    anymore - the user couldn't see what they selected anyway.

    Buttons are not meant to have values in terms of data entry. You do not
    enter data by clicking buttons.

    You enter data by selecting the appropriate radio button, or typing
    something into an input control, or selecting something from a dropdown.

    You can stop buttons from posting back. However, when the page does finally
    post back to the server, nothing will happen. You would have to manually in
    javascript keep track of which buttons were clicked, send that up to the
    server on the next postback, and process this manually. You can do this,
    but from what you described it sounds like having buttons for data entry is
    not the right model to begin with.
     
    Marina Levit [MVP], Jul 18, 2006
    #4
  5. bill

    bill Guest

    I would much prefer to create this interface the way I think it should look,
    and work the way Marina Levit thinks it should work.

    However, the client does not want a row of radio buttons or checkboxes or a
    dropdown - they want a solid bar marked by 100 dots, each corresponding to a
    value which will be saved accordingly.

    I'm interested in what model you would recommend to achieve this.

    Buttons work fine, except that they will post back. If I could just set
    AutoPostBack to false for the button, wire up an event handler, and treat
    the button click just like a dynamically created RadioButtonList
    SelectedIndexChanged event when the page does post back (when a 'Save'
    button is clicked) it would work exactly as the client wants.

    Thanks!

    Bill
     
    bill, Jul 18, 2006
    #5
  6. bill

    sam Guest

    I'd just create a custom control for that row of dots. You can render
    it however you want it with a strictly client side onclick for every
    dot where you just set an attached hidden field to the number you pass
    in on the onlick (from 1-100 depending on the dot clicked). Then just
    do a page.request.form("hidden_field_id") to get that value on the
    sever side. Or something more clever if you want.

    I'm not sure exactly what you are doing but you should just make
    everything client side. Dont add all those clunky server postback
    buttons to make up the row of dots.

    -Sam
     
    sam, Jul 18, 2006
    #6
  7. You could probably tie the onclick client side event to a javascript method
    that cancels the postback for each button while updating a hidden client
    side string array field with each button that had been pressed, leaving a
    submit button to effect the postback. Marina is right though, buttons are
    not the best approach for this - its fiddly and over complicated for what
    you describe.

    Does your client have no comprehension of good user centric design, and no
    understanding of what specific elements of HTML should be used for? Go with
    radio groups or checkboxes - it just makes sense.

    Regards

    John Timney (MVP)
     
    John Timney \(MVP\), Jul 18, 2006
    #7
  8. bill

    bill Guest

    Can you point me to a helpful resource for building a custom control which
    would raise a server side event?
     
    bill, Jul 19, 2006
    #8
  9. bill

    bill Guest

    Bottom line: No.
     
    bill, Jul 19, 2006
    #9
  10. bill

    BillE Guest

    Radio buttons or checkboxes don't particularly make sense if the client
    requires a granularity in value from 1 to 100.

    I agree with the problems with buttons, since I can't prevent autopostback
    and wire up a deferred event.

    I guess I'll have to use a client side javascript solution, although I
    prefer to keep my logic on the server.
     
    BillE, Jul 20, 2006
    #10
  11. bill

    sam Guest

    Hey uhh sorry I'm kindof time pressed right now and my laptop is out
    for repair.

    Just implement IPostBackEventHandler in your composite control class
    and call Page.RegisterRequiresPostbackEvent(this) and then you will
    always get that clickback in the method of that interface (I can't
    remember exactly the name now). Override the Render() method and
    output whatever html you want to its completely up to you (although if
    you inhereit from CompositeControl it will put a span tag around
    everything).

    I can help you more if you need it. Right now I'm supposed to be
    working :).

    -Sam
     
    sam, Jul 20, 2006
    #11
  12. bill

    addup Guest

    *sigh* since you insist on doing it the "fiddly way"
    There are plenty of references out there, for building custom controls.

    here's a quick 'n dirty ScaleControl.vb

    Imports System.ComponentModel
    Imports System.Web.UI

    <DefaultProperty("Value"), ToolboxData("<{0}:ScaleControl
    runat=server></{0}:ScaleControl>")> Public Class ScaleControl
    Inherits System.Web.UI.HtmlControls.HtmlInputHidden

    <Bindable(True), Category("Appearance"), DefaultValue("0")> Shadows
    Property Value() As Integer
    Get
    Return CInt(MyBase.Value)
    End Get
    Set(ByVal Value As Integer)
    MyBase.Value = Value
    End Set
    End Property
    Protected Overrides Sub Render(ByVal output As
    System.Web.UI.HtmlTextWriter)
    Dim i As Integer

    MyBase.Render(output)
    For i = 0 To 99
    output.Write(String.Format("<INPUT type=""Button""
    value=""{1}"" onclick=""javascript:if(document.getElementById('{0}')){{
    document.getElementById('{0}').value = {1} }}; return false;"" />",
    Me.ClientID, i))
    Next
    End Sub
    End Class

    not pretty, but it'll give you a place to start.

    To use, just drop it on a page, like any other control

    Hope this helps
    -- a --
     
    addup, Jul 21, 2006
    #12
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.