Click anywhere of DataGrid row to fire ButtonColumn event

Discussion in 'ASP .Net' started by Hardy Wang, Sep 14, 2006.

  1. Hardy Wang

    Hardy Wang Guest

    Hi all,
    We have DataGrid control in Web Form, our client requires to be able to
    click anywhere of a row to fire the event same as LinkBotton column is
    clicked. We we did in ASP.NET 1.1 is

    in ASPX page
    <asp:DataGrid id="MyGrid" runat="server" AutoGenerateColumns="False"
    AllowSorting="True" AllowPaging="True" runat="server"
    EnableViewState="True">
    <Columns>
    <asp:ButtonColumn Text="" CommandName="Select" Visible="false" />
    <asp:BoundColumn DataField="MyText" SortExpression="" />
    </Columns>
    <PagerStyle Mode="NumericPages" />
    </asp:DataGrid>

    in code-behind
    private void MyGrid_ItemDataBound(object sender,
    System.Web.UI.WebControls.DataGridItemEventArgs e) {
    if (e.Item.ItemType == ListItemType.Item
    || e.Item.ItemType == ListItemType.AlternatingItem
    || e.Item.ItemType == ListItemType.SelectedItem) {
    LinkButton button = (LinkButton)e.Item.Cells[0].Controls[0];
    e.Item.Attributes["onclick"] = Page.GetPostBackClientHyperlink(button,
    "");
    }
    }

    private void dLeads_ItemCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e) {
    if (e.CommandName == "Select") {
    // my logic
    }
    }

    Basically, the idea is to hijack the event of ButtonColum of DataGrid to set
    to the <TR>.

    After I converted the code to ASP.NET 2.0, I cannot use this logic any more,
    because I get "Invalid postback or callback argument. Event validation is
    enabled using in configuration or in a page. For security purposes, this
    feature verifies that arguments to postback or callback events originate
    from the server control that originally rendered them. If the data is valid
    and expected, use the ClientScriptManager.RegisterForEventValidation method
    in order to register the postback or callback data for validation." error
    message, unless I turn EnableEventValidation of the page off. And I don't
    think RegisterForEventValidation can work with my situation.

    Anybody has idea, what is the solution in ASP.NET 2.0?


    --
    WWW: http://www.imagestation.com/members/hardywang
    ICQ: 3359839
    yours Hardy
     
    Hardy Wang, Sep 14, 2006
    #1
    1. Advertising

  2. Hello Hardy,

    As for the DataGrid control, in ASP.NET 2.0, the postback script does has
    changed significantly from ASP.NET 1.1. And there is postback script event
    validation which may prevent our custom postback script from work.

    My approach for doing line selection(when clicking anyway in datagriditem)
    is use some script to find the certain "Select" button and call its click
    method. Thus, the script does not directly postback the page but make use
    of the select button(make it hidden), and this means works in both ASP.NET
    1.1 and 2.0.

    For example:

    I use a template column with a linkbutton to act as select button and in
    ItemDataBound event, I hide the LinkButton and add some script code to
    programmaticaly invoke its "click()" method at client-side when
    DataGridItem is clicked.

    ========aspx template=====================

    <asp:DataGrid ID="DataGrid1" runat="server" DataSourceID="SqlDataSource1"
    AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333"
    GridLines="None" OnItemCreated="DataGrid1_ItemCreated"
    OnItemDataBound="DataGrid1_ItemDataBound">
    <Columns>
    <asp:BoundColumn DataField="id"
    HeaderText="id"></asp:BoundColumn>
    <asp:BoundColumn DataField="name"
    HeaderText="name"></asp:BoundColumn>
    <asp:TemplateColumn >
    <ItemTemplate>
    <asp:LinkButton ID="linkSelect" runat="server"
    CausesValidation="false" CommandName="Select"
    Text="Select"></asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateColumn>


    </Columns>
    ......................................
    </asp:DataGrid>


    =========ItemDataBound function=================

    protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs
    e)
    {
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
    ListItemType.AlternatingItem)
    {
    LinkButton btn = e.Item.FindControl("linkSelect") as LinkButton;

    e.Item.Attributes["onclick"] =
    string.Format("document.getElementById('{0}').click();",
    btn.ClientID);

    btn.Style[HtmlTextWriterStyle.Display] = "none";

    }
    }
    ======================================

    Hope this helps.

    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], Sep 15, 2006
    #2
    1. Advertising

  3. Hardy Wang

    Hardy Wang Guest

    Thanks Steven,
    It works perfectly for me!

    --
    WWW: http://www.imagestation.com/members/hardywang
    ICQ: 3359839
    yours Hardy
    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > Hello Hardy,
    >
    > As for the DataGrid control, in ASP.NET 2.0, the postback script does has
    > changed significantly from ASP.NET 1.1. And there is postback script event
    > validation which may prevent our custom postback script from work.
    >
    > My approach for doing line selection(when clicking anyway in datagriditem)
    > is use some script to find the certain "Select" button and call its click
    > method. Thus, the script does not directly postback the page but make use
    > of the select button(make it hidden), and this means works in both ASP.NET
    > 1.1 and 2.0.
    >
    > For example:
    >
    > I use a template column with a linkbutton to act as select button and in
    > ItemDataBound event, I hide the LinkButton and add some script code to
    > programmaticaly invoke its "click()" method at client-side when
    > DataGridItem is clicked.
    >
    > ========aspx template=====================
    >
    > <asp:DataGrid ID="DataGrid1" runat="server" DataSourceID="SqlDataSource1"
    > AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333"
    > GridLines="None" OnItemCreated="DataGrid1_ItemCreated"
    > OnItemDataBound="DataGrid1_ItemDataBound">
    > <Columns>
    > <asp:BoundColumn DataField="id"
    > HeaderText="id"></asp:BoundColumn>
    > <asp:BoundColumn DataField="name"
    > HeaderText="name"></asp:BoundColumn>
    > <asp:TemplateColumn >
    > <ItemTemplate>
    > <asp:LinkButton ID="linkSelect" runat="server"
    > CausesValidation="false" CommandName="Select"
    > Text="Select"></asp:LinkButton>
    > </ItemTemplate>
    > </asp:TemplateColumn>
    >
    >
    > </Columns>
    > ......................................
    > </asp:DataGrid>
    >
    >
    > =========ItemDataBound function=================
    >
    > protected void DataGrid1_ItemDataBound(object sender,
    > DataGridItemEventArgs
    > e)
    > {
    > if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
    > ListItemType.AlternatingItem)
    > {
    > LinkButton btn = e.Item.FindControl("linkSelect") as
    > LinkButton;
    >
    > e.Item.Attributes["onclick"] =
    > string.Format("document.getElementById('{0}').click();",
    > btn.ClientID);
    >
    > btn.Style[HtmlTextWriterStyle.Display] = "none";
    >
    > }
    > }
    > ======================================
    >
    > Hope this helps.
    >
    > 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.
    >
    >
    >
    >
    >
     
    Hardy Wang, Sep 15, 2006
    #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. Fernando Lopes
    Replies:
    0
    Views:
    3,202
    Fernando Lopes
    Apr 28, 2005
  2. misiek
    Replies:
    0
    Views:
    2,289
    misiek
    Feb 20, 2006
  3. Amy
    Replies:
    0
    Views:
    1,011
  4. nipe

    buttoncolumn doesn't fire event

    nipe, Mar 9, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    417
    Teemu Keiski
    Mar 9, 2007
  5. UJ

    Click anywhere in a row....

    UJ, May 12, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    114
    Eliyahu Goldin
    May 12, 2005
Loading...

Share This Page