Click anywhere of DataGrid row to fire ButtonColumn event

H

Hardy Wang

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?
 
S

Steven Cheng[MSFT]

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.
 
H

Hardy Wang

Thanks Steven,
It works perfectly for me!

--
WWW: http://www.imagestation.com/members/hardywang
ICQ: 3359839
yours Hardy
Steven Cheng said:
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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Top