By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,925 Members | 732 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,925 IT Pros & Developers. It's quick & easy.

Datagrid with dynamic columns and updatepanel - ajax.net

P: 2
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.

(DEFAULT.ASPX)
[HTML]
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:sqldatasource id="SqlDataSource1" runat="server"
connectionstring="Server=s-netbe1;UID=netbe;PWD=netbe;Database=versione2-tmp;"
selectcommand="SELECT * FROM wtesti"
></asp:sqldatasource>
<div>
-<%=DateTime.Now%>-
<asp:updatepanel id="upddgd" runat="server">
<contenttemplate>
<asp:label id="labprova" runat="server"></asp:label>
<asp:datagrid id="dgdprova" runat=server datasourceid="sqldatasource1" autogeneratecolumns="false">
</asp:datagrid>
</contenttemplate>
</asp:updatepanel>
</div>
</form>
</body>
</html>

[/HTML]

(DEFAULT.ASPX.VB)
Expand|Select|Wrap|Line Numbers
  1. Partial Class _Default
  2.     Inherits System.Web.UI.Page
  3.  
  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
  21.  
  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
  32.  
  33. End Class
  34.  
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
Share this Question
Share on Google+
1 Reply


P: 2
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.