473,403 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Click anywhere of DataGrid row to fire ButtonColumn event

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
Sep 14 '06 #1
2 7182
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}').clic k();",
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.

Sep 15 '06 #2
Thanks Steven,
It works perfectly for me!

--
WWW: http://www.imagestation.com/members/hardywang
ICQ: 3359839
yours Hardy
"Steven Cheng[MSFT]" <st*****@online.microsoft.comwrote in message
news:mf**************@TK2MSFTNGXA01.phx.gbl...
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}').clic k();",
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.



Sep 15 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mark | last post by:
I have two buttons in a DataGrid. If I leave the ButtonType = "LinkButton", the EditCommand and DeleteCommand events fire fine. If I change th ButtonType = "PushButton", the events do not fire. ...
2
by: Nick Gilbert | last post by:
We have a Datagrid which contains a template column with a button in it, as well as an actual button column. If either of the buttons are clicked, the ItemCommand event does not fire. If...
5
by: J McD | last post by:
Hi I have a DataGrid with an ImageButton column. When I click on an imagebutton I get a postback but it doesn't run the OnImgBtnClick method. I can actually comment out the line where I add this...
6
by: z. f. | last post by:
Hi, i have a datagrid with a delete button for each row in the grid. when the delete button is clicked i need to ask the user in a "confirm" message box if he's sure he wants to delete. the...
1
by: Lenin Sakthees via .NET 247 | last post by:
Hi all, I am using a datagrid to show values from a SQL table. My problem is, I want to show the details of the each row when the user clicks the 'View details' buttoncolumn. I am able to fire a...
5
by: Vik | last post by:
If there are a few Select buttons in a datagrid, is there a way to distinguish in code which button was clicked? Thanks.
1
by: sergeyr3 | last post by:
Hey guys, I am new here, so i hope this works out: I have a datagrid which I populate with data from XML file. In EditItemTemplate I have a dropdownlist. How do I fire myDataGrid_UpdateCommand...
1
by: kevin | last post by:
Hi, I'm working with VS 2005 and Framework 2.0 I have a datagrid with a link and the selectedindexchanged will not fire. The page posts back but does nothing. Protected Sub...
2
by: Iain | last post by:
Hi All Using Delphi 2006 developer - C# Project I have the following 2 event handlers for the datagrid - see botton of page Both events will fire off correctly but i have a problem collecting...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.