473,765 Members | 1,994 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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" AutoGenerateCol umns="False"
AllowSorting="T rue" AllowPaging="Tr ue" runat="server"
EnableViewState ="True">
<Columns>
<asp:ButtonColu mn Text="" CommandName="Se lect" Visible="false" />
<asp:BoundColum n DataField="MyTe xt" SortExpression= "" />
</Columns>
<PagerStyle Mode="NumericPa ges" />
</asp:DataGrid>

in code-behind
private void MyGrid_ItemData Bound(object sender,
System.Web.UI.W ebControls.Data GridItemEventAr gs e) {
if (e.Item.ItemTyp e == ListItemType.It em
|| e.Item.ItemType == ListItemType.Al ternatingItem
|| e.Item.ItemType == ListItemType.Se lectedItem) {
LinkButton button = (LinkButton)e.I tem.Cells[0].Controls[0];
e.Item.Attribut es["onclick"] = Page.GetPostBac kClientHyperlin k(button,
"");
}
}

private void dLeads_ItemComm and(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs 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 ClientScriptMan ager.RegisterFo rEventValidatio n method
in order to register the postback or callback data for validation." error
message, unless I turn EnableEventVali dation of the page off. And I don't
think RegisterForEven tValidation 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 7208
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="S qlDataSource1"
AutoGenerateCol umns="False" CellPadding="4" ForeColor="#333 333"
GridLines="None " OnItemCreated=" DataGrid1_ItemC reated"
OnItemDataBound ="DataGrid1_Ite mDataBound">
<Columns>
<asp:BoundColum n DataField="id"
HeaderText="id" ></asp:BoundColumn >
<asp:BoundColum n DataField="name "
HeaderText="nam e"></asp:BoundColumn >
<asp:TemplateCo lumn >
<ItemTemplate >
<asp:LinkButt on ID="linkSelect " runat="server"
CausesValidatio n="false" CommandName="Se lect"
Text="Select"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateCol umn>
</Columns>
............... ............... ........
</asp:DataGrid>
=========ItemDa taBound function======= ==========

protected void DataGrid1_ItemD ataBound(object sender, DataGridItemEve ntArgs
e)
{
if (e.Item.ItemTyp e == ListItemType.It em || e.Item.ItemType ==
ListItemType.Al ternatingItem)
{
LinkButton btn = e.Item.FindCont rol("linkSelect ") as LinkButton;

e.Item.Attribut es["onclick"] =
string.Format(" document.getEle mentById('{0}') .click();",
btn.ClientID);

btn.Style[HtmlTextWriterS tyle.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/managednewsgrou ps/default.aspx#no tif
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.comw rote in message
news:mf******** ******@TK2MSFTN GXA01.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="S qlDataSource1"
AutoGenerateCol umns="False" CellPadding="4" ForeColor="#333 333"
GridLines="None " OnItemCreated=" DataGrid1_ItemC reated"
OnItemDataBound ="DataGrid1_Ite mDataBound">
<Columns>
<asp:BoundColum n DataField="id"
HeaderText="id" ></asp:BoundColumn >
<asp:BoundColum n DataField="name "
HeaderText="nam e"></asp:BoundColumn >
<asp:TemplateCo lumn >
<ItemTemplate >
<asp:LinkButt on ID="linkSelect " runat="server"
CausesValidatio n="false" CommandName="Se lect"
Text="Select"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateCol umn>
</Columns>
............... ............... ........
</asp:DataGrid>
=========ItemDa taBound function======= ==========

protected void DataGrid1_ItemD ataBound(object sender,
DataGridItemEve ntArgs
e)
{
if (e.Item.ItemTyp e == ListItemType.It em || e.Item.ItemType ==
ListItemType.Al ternatingItem)
{
LinkButton btn = e.Item.FindCont rol("linkSelect ") as
LinkButton;

e.Item.Attribut es["onclick"] =
string.Format(" document.getEle mentById('{0}') .click();",
btn.ClientID);

btn.Style[HtmlTextWriterS tyle.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/managednewsgrou ps/default.aspx#no tif
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
5763
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. Does this buttonType property change how the events fire? Suggestions? I have the ItemCommand event wired up to, but that only fires for the LinkButton as well. A few code samples below ... Thanks!
2
2034
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 however, the ButtonType of the ButtonColumn is changed to "LinkButton" instead of "PushButton", the event fires normally! Why on earth is this happening? I presume this a bug in .NET? (We are using 1.1 final).
5
6174
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 Click event to the ImageButton Column and it makes no difference, I still get a postback. This is driving me crazy...something seems to be causing an OnClick postback and it isn't my Click event I've included the code below....any help will be...
6
3165
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 problem is that the <asp:ButtonColumn ButtonType="PushButton" CommandName="Delete" .... don't have a property to allow onclick to run client side script . how can this be,
1
3399
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 event when the user clicks the button column here is my html for the datagrid ************************************************************************************** <Columns> <asp:HyperLinkColumn Target="_blank" DataNavigateUrlField="Title"...
5
6554
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
578
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 upon SelectedIndexChanged event in the dropdownlist? thank you. -Sergey <ASP:DataGrid id="myDataGrid" runat="server"
1
5163
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 DataGrid1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.SelectedIndexChanged Label2.Text = "what the H"
2
1985
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 the correct data in teh Update event - I have noted the problem in the event handler
0
9566
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9393
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10153
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8830
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7371
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5272
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5413
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3530
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2800
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.