ImageButton loses CommandArgument

Discussion in 'ASP .Net' started by Bigloopy, Apr 4, 2008.

  1. Bigloopy

    Bigloopy Guest

    I have a datagrid which I am adding BoundColumns to it programatically.
    Tehre are already 5 columns defined in the aspx as template columns, two link
    buttons and two image buttons. The datagrid should dsiplay the two link
    buttons, then the dynamically generated bound columns, then the 3 image
    buttons. The problem is when I insert the BoundColumns into the grid using
    the DataGrid.AddAt function I lose the databinding on the CommandArgument of
    the image buttons when capturing the OnCommand event. The explictly
    instantiated command however does keep its value.

    The sender and CommandEventArgs are almost useless to me at this point, it
    is supopsed to contain the ID of the row of data it represents. If I could
    get the number iof the row in terms of the datagrid I could work around it by
    using the DataKeys collection. DO I need to do something special in the
    databinding or viewstate? One way or another I need to figure out which row
    I clicked on, either in context of the data grid itself or the ID of the row
    of data.
    Bigloopy, Apr 4, 2008
    #1
    1. Advertising

  2. I feel your pain :)

    Here is the way I do it

    1. Drop somewhere on a page a LinkButton (let say with id = LinkButton1),
    wire up Click event to LinkButton1_OnClick
    2. Make a Text property empty... We do not want to see that LinkButton. It's
    a decoy to generate Click event

    3. In every row of your datagrid instead of button have it like this
    <input type="button" OnClick="__doPostBack('LinkButton1','<%#Eval("ID")%>')"
    >

    NOTE: not a server control. Just plain html. you can have a link if you want
    to..
    like <a
    href="javascript:__doPostBack('LinkButton1','<%#Eval("ID")%>')">Click Me
    </a>

    4. in LinkButton1_OnClick get the row ID by reading __EVENTARGUMENT
    int iRowId = Int32.Parse(Request.Form["__EVENTARGUMENT"]);


    PS: code is simplified to make it clear.
    In reality you will need to use LinkButton1.ClientID instead of hardcoded
    value 'LinkButton1'

    So <a href="javascript:__doPostBack('LinkButton1','<%#Eval("ID")%>')">Click
    Me </a>
    will be
    <a
    href="javascript:__doPostBack('<%=LinkButton1.ClientID%>','<%#Eval("ID")%>')">Click
    Me </a>


    Good luck.
    George.



    "Bigloopy" <> wrote in message
    news:...
    >I have a datagrid which I am adding BoundColumns to it programatically.
    > Tehre are already 5 columns defined in the aspx as template columns, two
    > link
    > buttons and two image buttons. The datagrid should dsiplay the two link
    > buttons, then the dynamically generated bound columns, then the 3 image
    > buttons. The problem is when I insert the BoundColumns into the grid
    > using
    > the DataGrid.AddAt function I lose the databinding on the CommandArgument
    > of
    > the image buttons when capturing the OnCommand event. The explictly
    > instantiated command however does keep its value.
    >
    > The sender and CommandEventArgs are almost useless to me at this point, it
    > is supopsed to contain the ID of the row of data it represents. If I
    > could
    > get the number iof the row in terms of the datagrid I could work around it
    > by
    > using the DataKeys collection. DO I need to do something special in the
    > databinding or viewstate? One way or another I need to figure out which
    > row
    > I clicked on, either in context of the data grid itself or the ID of the
    > row
    > of data.
    George Ter-Saakov, Apr 4, 2008
    #2
    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. Solomon Shaffer

    CommandArgument in TemplateColumn

    Solomon Shaffer, Jan 16, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    697
    Solomon Shaffer
    Jan 16, 2004
  2. Gilles T.

    ImageButton CommandArgument

    Gilles T., Feb 9, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    5,439
    Alvin Bruney [MVP]
    Mar 2, 2004
  3. Gilles T.
    Replies:
    1
    Views:
    461
    Chris Jackson
    Feb 10, 2004
  4. Mehdi
    Replies:
    0
    Views:
    1,863
    Mehdi
    Feb 15, 2004
  5. =?Utf-8?B?QmVuamFtaW4gSm9sZGVyc21h?=

    LinkButton CommandArgument bound property loses value on postback

    =?Utf-8?B?QmVuamFtaW4gSm9sZGVyc21h?=, Aug 21, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    4,107
    =?Utf-8?B?QmVuamFtaW4gSm9sZGVyc21h?=
    Aug 21, 2004
Loading...

Share This Page