Link Button Not Working in EditItemTemplate of Custom Gridview

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello.<br/>
<br/>
<br/>
<br/>
I am in need of help.<br/>
<br/>
I have this Code below for a Custom serverGridControl asp.net. <br/>
<br/>
The Grid populates. The edit button fires a command event. but the update does not fore the event.<br/>
<br/>
<br/>
<br/>
I have created the controls in the CreateChildControls<br/>
<br/>
and binds the grids in onPreRender and onLoad<br/>
<br/>
<br/>
<br/>
If Any body can shed some light. i will be great full. Thank you.




<pre class="prettyprint <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GrdTestForm.aspx.cs" Inherits="TestGridPage.GrdTestForm" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd

<html xmlns="http://www.w3.org/1999/xhtml
<head runat="server
<title></title>
<%@ Register TagPrefix="cc1" Namespace="TestGridControl" Assembly="TestGridControl" %>
</head>
<body>
<form id="form1" runat="server



<cc1:GridTestControl runat="server" ID="dd" >
<Columns>

<asp:TemplateField HeaderText="Controls
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnkEdit" CommandName="Edit" Text="Edit" CausesValidation="False"/>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton runat="server" ID="lnkUpdate" CommandName="Update" Text="Update" CausesValidation="False </asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField runat="server" HeaderText="cc
<ItemTemplate>
<asp:Label runat="server" ID="lbl </asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txt </asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</cc1:GridTestControl>




</form>
</body>
</html>
[/code]
<br/>
<pre class="prettyprint using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestGridControl
{
[DefaultProperty("Text")]
[ParseChildren(true, "Columns")]
[ToolboxData("<{0}:GridTestControl runat=server></{0}:GridTestControl>")]
public class GridTestControl : CompositeControl
{
List<GridView> lstGrds = new List<GridView>();


[Bindable(true),
Localizable(true),
PersistenceMode(PersistenceMode.InnerProperty)]
public DataControlFieldCollection Columns
{
get
{
var dd = new DataControlFieldCollection();

if (Context != null && Context.Session != null)
{
if (ViewState["Columns"] != null)
return (DataControlFieldCollection)ViewState["Columns"];
}
return dd;
}
set { ViewState["Columns"] = value; }
}

private int _count = 1;
private GridView CreateGrid(String id)
{
System.Diagnostics.Debug.WriteLine(string.Format("Open CreateGrid: {0}", _count++));
var grdDetails = new GridView
{
ID = id,
Width = Width,
AllowSorting = true,
ShowFooter = true,
AutoGenerateColumns = true,
EnableViewState = true
};

grdDetails.RowCommand += GrdDetailsRowCommand;
grdDetails.RowEditing += GrdRowEditing;
grdDetails.RowUpdating += GrdRowUpdating;
grdDetails.RowCancelingEdit += GrdRowCancelingEdit;
grdDetails.Sorting += GridViewSorting;

foreach (DataControlField c in Columns)
{
grdDetails.Columns.Add(c);
}
System.Diagnostics.Debug.WriteLine(string.Format("Close CreateGrid: {0}", _count++));
return grdDetails;

}



private void GridViewSorting(object sender, GridViewSortEventArgs e)
{
throw new NotImplementedException();
}

private void GrdRowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
throw new NotImplementedException();
}

private void GrdRowUpdating(object sender, GridViewUpdateEventArgs e)
{
throw new NotImplementedException();
}

private void GrdRowEditing(object sender, GridViewEditEventArgs e)
{
System.Diagnostics.Debug.WriteLine(string.Format("Open GrdRowEditing: {0}", _count++));
var grd = (GridView)sender;


grd.EditIndex = e.NewEditIndex;


System.Diagnostics.Debug.WriteLine(string.Format("Close GrdRowEditing: {0}", _count++));
}

private void GrdDetailsRowCommand(object sender, GridViewCommandEventArgs e)
{

}

protected override void CreateChildControls()
{
System.Diagnostics.Debug.WriteLine(string.Format("open CreateChildControls: {0}", _count++));
const int grdCount = 1;

for (var i = 0; i < grdCount; i++)
{
lstGrds.Add(CreateGrid("grd_" +i));
}

base.CreateChildControls();

System.Diagnostics.Debug.WriteLine(string.Format("Close CreateChildControls: {0}", _count++));
}

private void BindGrid(GridView grd)
{
System.Diagnostics.Debug.WriteLine(string.Format("Open BindGrid: {0}", _count++));
var dt = new DataTable();
dt.Columns.Add("ID");

for (int i = 0; i < 2; i++)
{
dt.Rows.Add(new object[] {i});
}

grd.DataSource = dt;
grd.DataBind();
System.Diagnostics.Debug.WriteLine(string.Format("Close BindGrid: {0}", _count++));
}

protected override void OnLoad(EventArgs e)
{
System.Diagnostics.Debug.WriteLine(string.Format("Open OnLoad: {0}", _count++));
foreach (var grd in lstGrds)
{
BindGrid(grd);
Controls.Add(grd);
}

base.OnLoad(e);
System.Diagnostics.Debug.WriteLine(string.Format("Close OnLoad: {0}", _count++));
}
protected override void OnPreRender(EventArgs e)
{
System.Diagnostics.Debug.WriteLine(string.Format("Open OnPreRender: {0}", _count++));
Controls.Clear();
foreach (var grd in lstGrds)
{
BindGrid(grd);
Controls.Add(grd);
}

base.OnPreRender(e);
System.Diagnostics.Debug.WriteLine(string.Format("Close OnPreRender: {0}", _count++));
}

protected override void Render(HtmlTextWriter writer)
{
System.Diagnostics.Debug.WriteLine(string.Format("Open Render: {0}", _count++));


base.Render(writer);
System.Diagnostics.Debug.WriteLine(string.Format("Close Render: {0}", _count++));
}


}
}
[/code]
<br/>


View the full article
 
Back
Top