I'm still stuck on this issue. After converting to 2.0 none of the linkbuttons in any of the datagrids click event will fire. I'm not getting any errors or any indication of what might be wrong. The page postsback, but the click event is not fired. I
think it might be a problem with viewstate as a custom datepicker control is not retaining it's state across postbacks. Everything is working fine in 1.1. Please, any suggestions or ideas would be most appreciated!!!
I have tried to copied your source to my machine and modify it to make it work (since your source is not complete), the onClick event of the linkButton do get fired and works ok on my v2 machine.
I have seen in your code you have specify an event handler for the OnItemDataBound event, so what exactly did you do in that event handler? If possible, can you post a more complete source for us to try to investigate and to repro it?
Hello and thanks for the response. Here is one of the pages I'm having a problem with:
Code Behind:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CDCCore;
using CDCWeb;
protected void ItemDataBound(object o, DataGridItemEventArgs e)
{
if(e.Item.FindControl("lbDelete") != null)
{
((LinkButton) e.Item.FindControl("lbDelete")).Attributes.Add("onClick", "return confirm('Are you sure you want to delete this item?');");
}
}
protected void DeleteClicked(object o, EventArgs e)
{
try
{
Utilities.ExecuteStoredProcNoResults("cdc", "deleteSite", "siteid", ((LinkButton)o).Attributes["siteid"]);
Response.Redirect(HttpContext.Current.Request.Url.PathAndQuery);
}
catch(System.Data.SqlClient.SqlException)
{
lblSqlExc.Visible = true;
lblSqlExc.Text = "This site contains references to it and cannot be deleted.";
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
#endregion
#region Methods
private void LoadSiteDropDown()
{
developersDDL.Items.RemoveAt(0);
developersDDL.Items.Insert(0, new ListItem("-- ALL --", ""));
ddlStatus.Items.Add(new ListItem("-- All --", ""));
ddlStatus.Items.Add(new ListItem("Active", "1"));
ddlStatus.Items.Add(new ListItem("Inactive", "0"));
}
I'm not having this problem if I create a new website from scratch in VS2005. I'm able to create a page with a datagrid that has a column of linkbuttons. When I click a link button, it's event is fired and everything works as it should. I have now converted
four projects from 1.1 to 2.0, and all of those projects have the same problem: linkbuttons in a datagrid will not have it's click event fired. I'm really needing to get this resolved, any help is greatly appreciated!!! Thanks
Not really. I was using a basepage that rendered all the html for the main layout and everything would extend from that class. I ended up removing the code that rendered the layout and creating a master page. Now everything works fine. I haven't really
had time to go through and pinpoint as to why it was not working with my basepage rendering the layout html. I'm just glad I can move on for now.
cognitronic
Member
90 Points
18 Posts
Linkbuttons In Datagrid Click Event Not Firing After Migrating To 2.0
Feb 26, 2006 04:54 PM|LINK
Hello,
After upgrading from 1.1 to 2.0, the click events of linkbuttons in a datagrid are not being fired. Here is a snippet of the ViewSource:
<table class="datagrid" cellspacing="0" cellpadding="0" rules="all" bordercolor="Black" border="1" id="dgSitesList" style="border-color:Black;border-width:1px;border-style:solid;width:100%;border-collapse:collapse;">
<tr class="datagridheader">
<td><a href="javascript:__doPostBack('dgSitesList$_ctl14$_ctl2','')">Name</a></td><td><a href="javascript:__doPostBack('dgSitesList$_ctl14$_ctl3','')">City</a></td><td>Zip</td><td>Lots</td><td>Actions</td>
</tr><tr>
<td>
CDC Main Office
</td><td>
Ceres
</td><td>
95307
</td><td>
<a id="dgSitesList__ctl15_lbSites" developerid="27" siteid="13" href="javascript:__doPostBack('dgSitesList$_ctl15$lbSites','')">
Lots
</a>
</td><td>
<a id="dgSitesList__ctl15_lbEdit" NAME="lbEdit" developerid="27" siteid="13" href="javascript:__doPostBack('dgSitesList$_ctl15$lbEdit','')"><img title='Edit site properties...' border='no' src='/cdcweb/images/edit.gif'/></a>
<a onclick="return confirm('Are you sure you want to delete this item?');" id="dgSitesList__ctl15_lbDelete" NAME="lbDelete" developerid="27" siteid="13" href="javascript:__doPostBack('dgSitesList$_ctl15$lbDelete','')"><img title='Edit site properties...' border='no' src='/cdcweb/images/delete.gif'/></a>
</td>
</tr><tr style="background-color:#EEEEEE;">
<td>
Copper Creek
</td><td>
Merced
</td><td>
95340
</td><td>
<a id="dgSitesList__ctl16_lbSites" developerid="1" siteid="2" href="javascript:__doPostBack('dgSitesList$_ctl16$lbSites','')">
Lots
</a>
</td><td>
<a id="dgSitesList__ctl16_lbEdit" NAME="lbEdit" developerid="1" siteid="2" href="javascript:__doPostBack('dgSitesList$_ctl16$lbEdit','')"><img title='Edit site properties...' border='no' src='/cdcweb/images/edit.gif'/></a>
<a onclick="return confirm('Are you sure you want to delete this item?');" id="dgSitesList__ctl16_lbDelete" NAME="lbDelete" developerid="1" siteid="2" href="javascript:__doPostBack('dgSitesList$_ctl16$lbDelete','')"><img title='Edit site properties...' border='no' src='/cdcweb/images/delete.gif'/></a>
</td>
</tr>
Here is the code:
<asp:DataGrid
id="dgSitesList"
CssClass="datagrid"
runat="server"
AutoGenerateColumns="False"
CellPadding=0
CellSpacing=0
BorderWidth="1"
BorderColor="#000000"
ShowFooter=True
width="100%"
OnItemDataBound="ItemDataBound"
AlternatingItemStyle-BackColor="#eeeeee"
AllowSorting=True
OnSortCommand="SortEventHandler">
<HeaderStyle CssClass="datagridheader">
</HeaderStyle>
<FooterStyle CssClass="datagridheader">
</FooterStyle>
<Columns>
<asp:TemplateColumn
HeaderText="Name"
SortExpression="name">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "name") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn
HeaderText="City"
SortExpression="city">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "city")%>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn
HeaderText="Zip">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "zip") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn
HeaderText="Lots">
<ItemTemplate>
<asp:LinkButton
Runat=server
OnClick="LotsClicked"
developerid='<%# DataBinder.Eval(Container.DataItem, "developerid") %>'
siteid='<%# DataBinder.Eval(Container.DataItem, "id") %>'
ID="lbSites">
Lots
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn
HeaderText="Actions">
<ItemTemplate>
<asp:LinkButton
Runat=server
OnClick="EditClicked"
developerid='<%# DataBinder.Eval(Container.DataItem, "developerid") %>'
siteid='<%# DataBinder.Eval(Container.DataItem, "id") %>'
Text="<img title='Edit site properties...' border='no' src='/cdcweb/images/edit.gif'/>"
ID="lbEdit"
NAME="lbEdit">
</asp:LinkButton>
<asp:LinkButton
Runat=server
OnClick="DeleteClicked"
developerid='<%# DataBinder.Eval(Container.DataItem, "developerid") %>'
siteid='<%# DataBinder.Eval(Container.DataItem, "id") %>'
Text="<img title='Edit site properties...' border='no' src='/cdcweb/images/delete.gif'/>"
ID="lbDelete"
NAME="lbDelete">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
The page appears to do a postback but the none of the onclick events ever get fired. There are no error messages. Please help!!
cognitronic
Member
90 Points
18 Posts
Re: Linkbuttons In Datagrid Click Event Not Firing After Migrating To 2.0
Mar 01, 2006 01:00 PM|LINK
I'm still stuck on this issue. After converting to 2.0 none of the linkbuttons in any of the datagrids click event will fire. I'm not getting any errors or any indication of what might be wrong. The page postsback, but the click event is not fired. I think it might be a problem with viewstate as a custom datepicker control is not retaining it's state across postbacks. Everything is working fine in 1.1. Please, any suggestions or ideas would be most appreciated!!!
charlesz
Member
87 Points
18 Posts
Microsoft
Re: Linkbuttons In Datagrid Click Event Not Firing After Migrating To 2.0
Mar 01, 2006 07:51 PM|LINK
I have tried to copied your source to my machine and modify it to make it work (since your source is not complete), the onClick event of the linkButton do get fired and works ok on my v2 machine.
I have seen in your code you have specify an event handler for the OnItemDataBound event, so what exactly did you do in that event handler? If possible, can you post a more complete source for us to try to investigate and to repro it?
Thanks
cognitronic
Member
90 Points
18 Posts
Re: Linkbuttons In Datagrid Click Event Not Firing After Migrating To 2.0
Mar 01, 2006 09:07 PM|LINK
Hello and thanks for the response. Here is one of the pages I'm having a problem with:
Code Behind:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CDCCore;
using CDCWeb;
namespace CDC
{
/// <summary>
/// Summary description for SitesList.
/// </summary>
public class SitesList : CDCMainBasePage
{
#region Declarations
protected DevelopersDDL developersDDL;
protected DropDownList ddlStatus;
protected DataGrid dgSitesList;
protected Label lblNoRecords;
protected HtmlTableCell tdStatus;
protected Label lblSqlExc;
protected HtmlTableRow trSave;
#endregion
#region Properties
protected string qsId
{
get
{
return Request.QueryString["developerid"];
}
}
protected bool isInsert
{
get
{
return isNull(qsId);
}
}
#endregion
#region Events
private void Page_Load(object sender, System.EventArgs e)
{
Title = rs.CDC_TITLE + "Sites List";
if(!IsPostBack)
{
if(!isInsert)
{
tdStatus.Visible = true;
}
else
{
tdStatus.Visible = false;
}
SortCriteria = "name";
SortDir = "asc";
ddlStatus.SelectedIndex = 1;
LoadSiteDropDown();
try
{
developersDDL.SelectedValue = qsId;
}
catch(ArgumentOutOfRangeException exc)
{
developersDDL.SelectedIndex = 0;
}
GetSitesList(GetFilters(), SortCriteria, SortDir);
}
}
protected void developersDDL_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("SitesList.aspx?developerid=" + developersDDL.SelectedValue + "&rtrnurl" + returnUrlId);
}
protected void ItemDataBound(object o, DataGridItemEventArgs e)
{
if(e.Item.FindControl("lbDelete") != null)
{
((LinkButton) e.Item.FindControl("lbDelete")).Attributes.Add("onClick", "return confirm('Are you sure you want to delete this item?');");
}
}
protected void DeleteClicked(object o, EventArgs e)
{
try
{
Utilities.ExecuteStoredProcNoResults("cdc", "deleteSite", "siteid", ((LinkButton)o).Attributes["siteid"]);
Response.Redirect(HttpContext.Current.Request.Url.PathAndQuery);
}
catch(System.Data.SqlClient.SqlException)
{
lblSqlExc.Visible = true;
lblSqlExc.Text = "This site contains references to it and cannot be deleted.";
}
}
protected void AddSiteClicked(object o, EventArgs e)
{
NavigateTo("/SiteProperties.aspx?developerid=" + qsId);
}
protected void ApplyFiltersClicked(object o, EventArgs e)
{
GetSitesList(GetFilters(), SortCriteria, SortDir);
}
protected void CancelClicked(object o, EventArgs e)
{
NavigateBack();
}
protected void EditClicked(object o, EventArgs e)
{
NavigateTo("/SiteProperties.aspx?siteid=" + ((LinkButton)o).Attributes["siteid"] + "&developerid=" + ((LinkButton)o).Attributes["developerid"]);
}
protected void LotsClicked(object o, EventArgs e)
{
NavigateTo("/LotsList.aspx?siteid=" + ((LinkButton)o).Attributes["siteid"] + "&developerid=" + ((LinkButton)o).Attributes["developerid"]);
}
protected void SortEventHandler(object o, DataGridSortCommandEventArgs e)
{
if(SortCriteria == e.SortExpression)
{
if(SortDir == "desc")
{
SortDir = "asc";
}
else
{
SortDir = "desc";
}
}
SortCriteria = e.SortExpression;
GetSitesList(GetFilters(), SortCriteria, SortDir);
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
#endregion
#region Methods
private void LoadSiteDropDown()
{
developersDDL.Items.RemoveAt(0);
developersDDL.Items.Insert(0, new ListItem("-- ALL --", ""));
ddlStatus.Items.Add(new ListItem("-- All --", ""));
ddlStatus.Items.Add(new ListItem("Active", "1"));
ddlStatus.Items.Add(new ListItem("Inactive", "0"));
}
private void GetSitesList(Hashtable filters, string sortcolumn, string sortdir)
{
ArrayList paramNames = new ArrayList();
ArrayList paramValues = new ArrayList();
foreach (string key in filters.Keys)
{
paramNames.Add(key);
paramValues.Add(filters[key]);
}
paramNames.Add("sortcolumn");
paramNames.Add("sortdir");
paramValues.Add(sortcolumn);
paramValues.Add(sortdir);
DataSet ds = Utilities.ExecuteStoredProc("cdc", "selectSites", paramNames, paramValues);
if(ds.Tables[0].Rows.Count > 0)
{
lblNoRecords.Visible = false;
dgSitesList.Visible = true;
dgSitesList.DataSource = ds;
dgSitesList.DataBind();
}
else
{
dgSitesList.Visible = false;
lblNoRecords.Visible = true;
lblNoRecords.Text = "No Records Found";
}
}
protected Hashtable GetFilters()
{
Hashtable htFilters = new Hashtable();
htFilters.Add("developerid", developersDDL.SelectedValue);
htFilters.Add("isactive", ddlStatus.SelectedValue);
return htFilters;
}
#endregion
}
}
Here is the ASPX Page:
<%@ Page language="c#" Codebehind="SitesList.aspx.cs" AutoEventWireup="false" Inherits="CDC.SitesList" %>
<%@ Register TagPrefix="sc" Namespace="CDCWeb" Assembly="CDCWeb"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>SitesList</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body MS_POSITIONING="GridLayout">
<div class="admincontent">
<div class="actions">
<div class="actionstitle">
Actions
</div>
<div style="MARGIN-TOP: 5px; FLOAT: right; WIDTH: 95px">
<table class="container">
<tr>
<td>
<table class="content">
<tr>
<td>
<asp:LinkButton
Runat=server
ID="lbAddSite"
CssClass="a"
onclick="AddSiteClicked">
Add Site
</asp:LinkButton>
</td>
</tr>
<tr>
<td>
<asp:LinkButton
Runat=server
ID="lbCancel"
CssClass="a"
onclick="CancelClicked">
Return
</asp:LinkButton>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</div>
<div class="admintitle">
Sites List
</div>
<div style="MARGIN-TOP: 5px; FLOAT: left; WIDTH: 700px">
<table class="container">
<tr>
<td>
<table class="content">
<tr>
<td>
Developers:
</td>
<td>
<sc:DevelopersDDL
runat=server
autopostback=true
onselectedindexchanged="developersDDL_SelectedIndexChanged"
id="developersDDL">
</sc:DevelopersDDL>
</td>
<td runat=server id="tdStatus">
Status:
<asp:DropDownList
Runat=server
ID="ddlStatus">
</asp:DropDownList>
</td>
</tr>
<tr>
<td align=right>
<asp:LinkButton
Runat=server
ID="lbApplyFilters"
onclick="ApplyFiltersClicked"
CssClass="a">
Apply Filters
</asp:LinkButton>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<div style="MARGIN-TOP: 5px; FLOAT: left; WIDTH: 700px">
<table class="container">
<tr>
<td>
<asp:Label
Runat=server
ID="lblNoRecords">
</asp:Label>
</td>
</tr>
<tr>
<td>
<asp:DataGrid
id="dgSitesList"
CssClass="datagrid"
runat="server"
AutoGenerateColumns="False"
CellPadding=0
CellSpacing=0
BorderWidth="1"
BorderColor="#000000"
ShowFooter=True
width="100%"
OnItemDataBound="ItemDataBound"
AlternatingItemStyle-BackColor="#eeeeee"
AllowSorting=True
OnSortCommand="SortEventHandler">
<HeaderStyle CssClass="datagridheader">
</HeaderStyle>
<FooterStyle CssClass="datagridheader">
</FooterStyle>
<Columns>
<asp:TemplateColumn
HeaderText="Name"
SortExpression="name">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "name") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn
HeaderText="City"
SortExpression="city">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "city")%>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn
HeaderText="Zip">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "zip") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn
HeaderText="Lots">
<ItemTemplate>
<asp:LinkButton
Runat=server
OnClick="LotsClicked"
developerid='<%# DataBinder.Eval(Container.DataItem, "developerid") %>'
siteid='<%# DataBinder.Eval(Container.DataItem, "id") %>'
ID="lbSites">
Lots
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn
HeaderText="Actions">
<ItemTemplate>
<asp:LinkButton
Runat=server
OnClick="EditClicked"
developerid='<%# DataBinder.Eval(Container.DataItem, "developerid") %>'
siteid='<%# DataBinder.Eval(Container.DataItem, "id") %>'
Text="<img title='Edit site properties...' border='no' src='/cdcweb/images/edit.gif'/>"
ID="lbEdit"
NAME="lbEdit">
</asp:LinkButton>
<asp:LinkButton
Runat=server
OnClick="DeleteClicked"
developerid='<%# DataBinder.Eval(Container.DataItem, "developerid") %>'
siteid='<%# DataBinder.Eval(Container.DataItem, "id") %>'
Text="<img title='Edit site properties...' border='no' src='/cdcweb/images/delete.gif'/>"
ID="lbDelete"
NAME="lbDelete">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</td>
</tr>
</table>
</div>
</div>
</body>
</html>
I really appreciate any help or suggestions you might have, thanks!!
deekay
Member
35 Points
7 Posts
Re: Linkbuttons In Datagrid Click Event Not Firing After Migrating To 2.0
Mar 02, 2006 07:38 PM|LINK
Try setting your autoeventwireup="true"
This is what I had to do after converting one of my projects when I had a similar problem.
cognitronic
Member
90 Points
18 Posts
Re: Linkbuttons In Datagrid Click Event Not Firing After Migrating To 2.0
Mar 02, 2006 08:48 PM|LINK
Thank for the reponse deekay,
I've added the following line in the web.config:
<pages autoEventWireup="true" />
Still no luck. If I unregister 2.0 and go back to 1.1, everything works great. As soon as I register 2.0 the linkbutton click events stop working.
cognitronic
Member
90 Points
18 Posts
Re: Linkbuttons In Datagrid Click Event Not Firing After Migrating To 2.0
Mar 05, 2006 06:21 PM|LINK
I'm not having this problem if I create a new website from scratch in VS2005. I'm able to create a page with a datagrid that has a column of linkbuttons. When I click a link button, it's event is fired and everything works as it should. I have now converted four projects from 1.1 to 2.0, and all of those projects have the same problem: linkbuttons in a datagrid will not have it's click event fired. I'm really needing to get this resolved, any help is greatly appreciated!!! Thanks
MS AspNetDev...
Member
43 Points
17 Posts
Re: Linkbuttons In Datagrid Click Event Not Firing After Migrating To 2.0
Mar 13, 2006 04:18 AM|LINK
cognitronic
Member
90 Points
18 Posts
Re: Linkbuttons In Datagrid Click Event Not Firing After Migrating To 2.0
Mar 13, 2006 02:22 PM|LINK
silviuks
Member
35 Points
7 Posts
Re: Linkbuttons In Datagrid Click Event Not Firing After Migrating To 2.0
Apr 11, 2006 03:13 PM|LINK
I don't know if it will help you to solve your problem.
<asp:GridView ID="gv1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Project name">
<ItemTemplate>
<asp:HyperLink ID="lnkProjDetails" runat="server" Text='<%# Eval("projectName") %>'
NavigateUrl='<%# "~/Projects.aspx?p=own&a=details&ID=" + DataBinder.Eval(Container.DataItem, "ID") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
hope it's usefull.