470,638 Members | 1,579 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,638 developers. It's quick & easy.

Datagrid with dynamic columns and updatepanel - ajax.net

An updatepanel contains a datagrid, this datagrid has columns generated by code-behind: some columns should have async postback, others should have sync postback.

Here a sample to reproduce the behaviour: it's a webform with some VB code-behind, you shoud create a new AJAX-enabled application and past the following code.

<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:sqldatasource id="SqlDataSource1" runat="server"
selectcommand="SELECT * FROM wtesti"
<asp:updatepanel id="upddgd" runat="server">
<asp:label id="labprova" runat="server"></asp:label>
<asp:datagrid id="dgdprova" runat=server datasourceid="sqldatasource1" autogeneratecolumns="false">


Expand|Select|Wrap|Line Numbers
  1. Partial Class _Default
  2.     Inherits System.Web.UI.Page
  4.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  5.         Dim dat As New BoundColumn
  6.         dat.DataField = "titolo1"
  7.         dgdprova.Columns.Add(dat)
  8.         Dim ajx As New ButtonColumn
  9.         ajx.CommandName = "ajax"
  10.         ajx.Text = "Ajax"
  11.         dgdprova.Columns.Add(ajx)
  12.         Dim pst As New ButtonColumn
  13.         pst.CommandName = "post"
  14.         pst.Text = "Postback"
  15.         dgdprova.Columns.Add(pst)
  16.         Me.dgdprova.DataBind()
  17.     End Sub
  18.     Protected Sub dgdprova_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgdprova.ItemCommand
  19.         Me.labprova.Text = "PB " + Now.ToLongTimeString
  20.     End Sub
  22.     Protected Sub dgdprova_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgdprova.ItemDataBound
  23.         Select Case e.Item.ItemType
  24.             Case ListItemType.AlternatingItem, ListItemType.Item
  25.                 Dim cella As New TableCell
  26.                 cella = e.Item.Cells(2)
  27.                 Dim tr As New PostBackTrigger
  28.                 tr.ControlID = cella.Controls(0).ClientID
  29.                 Me.upddgd.Triggers.Add(tr)
  30.         End Select
  31.     End Sub
  33. End Class
When I run the project it give the following error:

A control with ID 'dgdprova_ctl02_ctl01' could not be found for the trigger in UpdatePanel 'upddgd'.

[InvalidOperationException: A control with ID 'dgdprova_ctl02_ctl01' could not be found for the trigger in UpdatePanel 'upddgd'.]

System.Web.UI.UpdatePanelControlTrigger.FindTarget Control(Boolean searchNamingContainers) +315
System.Web.UI.PostBackTrigger.Initialize() +12
System.Web.UI.UpdatePanelTriggerCollection.Initial ize() +65
System.Web.UI.UpdatePanel.Initialize() +37
System.Web.UI.UpdatePanel.OnLoad(EventArgs e) +51
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Control.LoadRecursive() +131
System.Web.UI.Control.LoadRecursive() +131
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1436

Any help would be appreciated!
Nov 23 '07 #1
1 8053
I finally found a solution!! I must use the RegisterPostBackControl method of the ScriptManager control of the page

Expand|Select|Wrap|Line Numbers
  1.     Protected Sub dgdprova_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgdprova.ItemDataBound
  2.         Select Case e.Item.ItemType
  3.             Case ListItemType.AlternatingItem, ListItemType.Item
  4.                 ScriptManager1.RegisterPostBackControl(e.Item.Cells(2).Controls(0))
  5.         End Select
  6.     End Sub

Now the second buttoncolumn will post synchronously
Nov 23 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by lu | last post: by
7 posts views Thread by smithb1028 | last post: by
2 posts views Thread by brett | last post: by
1 post views Thread by =?Utf-8?B?T2xlZw==?= | last post: by
1 post views Thread by Theo | last post: by
13 posts views Thread by SAL | last post: by
1 post views Thread by Stoney L | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.