ASP Button in a Datalist

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

  1. #1

    Default ASP Button in a Datalist

    Hello,

    I have a datalist that contains an asp button. I have the need to pass a
    value that is bound to my datalist along the button when the on_click event
    is fired. My datalist creates a button for each row in my datasource, so
    each button needs to pass a unique value. My questions are:

    1. How do I pass the value with the button without making it visible on the
    button or form.
    2. Is there a way to grab this value in the button's event parameters, the
    sender as system.object or the e as system.eventArgs

    This button was previosly an anchor tag that passed the value through the
    HREF querystring, but I did not like how it lost all the viewstate when I
    redirected back.

    Any help is appreciated,
    Thanks


    Chris Fink Guest

  2. Similar Questions and Discussions

    1. datalist please help soon
      H data I'm binding to the DataList is information about products in my product database. Perhaps passed in the QueryString to this page is a...
    2. DataList: how to use??
      I am having difficulity understanding just exactly how to use the DataList (may be using it in the wrong context) Problem: I have a dataset that...
    3. ASP:DATALIST
      Hello, Does anyone know of a way to prevent the asp:datalist control from creating html code table and table row tags when it is rendered. I...
    4. ASP DataList
      Hello, I have an ASP Datalist in which I am binding a value within the <itemtemplate> section as follows: <%#...
    5. UpdateCommand with Datalist
      Hi, I have a datalist that I use to display and edit records to a user. Datalist shown below; visual basic...
  3. #2

    Default Re: ASP Button in a Datalist

    Chris,

    The only controls that will have an event are PAGE level controls.
    Once you place a button inside a Datalist, it is now a CHILD of the datalist
    and will need to use the Datalist's events instead.

    For the button, you will need to assign, through databinding, these
    attributes

    CommandArguement
    CommandName

    BUTTON EXAMPLE
    <asp:button id="Button1" runat="server" text="Push me" commandarguement='<%#
    Container.DataItem("primaryKey") %>' commandname="Action" />
    This sets the CommandArguement equal to the primary key of the record.

    Now, for the event that will occur when button is pushed - This is the
    Datalist's ItemCommand Event

    Private Sub DataList1_ItemCommand(source as object, e as datalisteventargs)
    Handles Datalist1.ItemCommand
    'Assign a variable to hold CommandArgument

    Dim primaryKey as Integer
    primaryKey = CInt(e.CommandArguement)

    'use this primaryKey variable in logic below
    'Also, if there are more than one button (edit, delete, copy, etc)
    'You put that "Action" in the CommandName attribute and use like so...

    If e.CommandName = "Delete" Then
    OleDbCommand1.CommandText = "DELETE DISTINCT FROM table WHERE
    [primaryKey]="& primaryKey &";"
    End If

    End sub

    Hope this helps,
    Severin

    "Chris Fink" <chris@chrisfink.com> wrote in message
    news:e%23g45CkRDHA.940@TK2MSFTNGP11.phx.gbl...
    > Hello,
    >
    > I have a datalist that contains an asp button. I have the need to pass a
    > value that is bound to my datalist along the button when the on_click
    event
    > is fired. My datalist creates a button for each row in my datasource, so
    > each button needs to pass a unique value. My questions are:
    >
    > 1. How do I pass the value with the button without making it visible on
    the
    > button or form.
    > 2. Is there a way to grab this value in the button's event parameters, the
    > sender as system.object or the e as system.eventArgs
    >
    > This button was previosly an anchor tag that passed the value through the
    > HREF querystring, but I did not like how it lost all the viewstate when I
    > redirected back.
    >
    > Any help is appreciated,
    > Thanks
    >
    >

    sampsons Guest

  4. #3

    Default ASP Button in a Datalist

    Hi.

    There is a way:

    First, change the buttons in your list to be control
    buttons so they are using the OnCommand event instead of
    OnClick. That makes you able to add a command parameter to
    the buttons.

    This code is from an application I'm making now. The
    list's DataSource is a dataset, and when the button is
    pushed another page opens. I use the session object for
    storing different kinds of data.

    Hope this helps you!

    Ingeborg


    private void dataList_ItemDataBound(object sender,
    System.Web.UI.WebControls.DataListItemEventArgs e)
    {
    DataRowView drv = (DataRowView) e.Item.DataItem;

    Button anyButton = (Button)e.Item.FindControl
    ("anyButton");
    anyButton.Command += new
    System.Web.UI.WebControls.CommandEventHandler
    (anyButton_Command);
    anyButton.CommandArgument = drv.Row["id"].ToString
    ();
    }

    protected void anyButton_Command(object sender,
    System.Web.UI.WebControls.CommandEventArgs e)
    {
    Session["Id"] = int.Parse(e.CommandArgument.ToString
    ());
    Response.Redirect("newpage.aspx");
    }





    >-----Original Message-----
    >Hello,
    >
    >I have a datalist that contains an asp button. I have the
    need to pass a
    >value that is bound to my datalist along the button when
    the on_click event
    >is fired. My datalist creates a button for each row in my
    datasource, so
    >each button needs to pass a unique value. My questions
    are:
    >
    >1. How do I pass the value with the button without making
    it visible on the
    >button or form.
    >2. Is there a way to grab this value in the button's
    event parameters, the
    >sender as system.object or the e as system.eventArgs
    >
    >This button was previosly an anchor tag that passed the
    value through the
    >HREF querystring, but I did not like how it lost all the
    viewstate when I
    >redirected back.
    >
    >Any help is appreciated,
    >Thanks
    >
    >
    >.
    >
    Ingeborg 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