Showing posts with label strange. Show all posts
Showing posts with label strange. Show all posts

Wednesday, March 28, 2012

UpdatePanel - need synchronous postback from child button

I have several buttons in UpdatePanel and I want one the button to cause synchronous postback. It may sound strange but without going into details - I need the button within UpdatePanel and I need this button to cause sync postback. This button cannot be outside of UpdatePanel.

I tried settingChildrenAsTriggers="false"UpdateMode="conditional"but that still causes async postback when the button is clicked - ScriptManager.IsInAsyncPostBack returns "true".

How can I get around it?

thanks,

-Stan

Hi There,

You can make use of trigger in your updatepanel

example: Button1 is full postback

<asp:ScriptManagerID="ScriptManager1"runat="server">

</asp:ScriptManager>

<asp:UpdatePanelID="UpdatePanel1"runat="server">

<Triggers>

<asp:PostBackTriggerControlID="Button1"/>

</Triggers>

<ContentTemplate>

<asp:ButtonID="Button1"runat="server"Text="Button"/>

<asp:ButtonID="Button2"runat="server"Text="Button" /

</ContentTemplate>

</asp:UpdatePanel>


That's exactly what I need, thanks!

Monday, March 26, 2012

UpdatePanel / Gridview / ButtonField - Strange Behaviour

Hi everyone,

I have a strange problem that I've managed to narrow down to being an issue with the UpdatePanel control. Hopefully someone could advise me...

I have an UpdatePanel which contains a Gridview which isnot bound to a dataset etc. I populate the gridview manually by constructing my own table, defining grid columns then binding it to my table etc. Several of the columns in the gridview are of typeButtonField withButtonType set to "Button"

Now, all of this is working fine...no problems whatsoever.

I decided later that rather than a button I wanted a link instead, so where I construct the GridView's columns using a ButtonField column type, I simply changed
"MyCol.ButtonType = ButtonType.Button" to "MyCol.ButtonType = ButtonType.Link"

My problem is that by making this simple change, the page now does not work. The page still renders exactly as expected, but when I click on one of the link buttons, none of the events are fired...instead, I get the icon at the bottom left of explorer indicating an error on the page. When I look at the error all I get is:

Line: 1
Char: 1
Error: Object expected
Code: 0
URL: xxxxxxxxxx

Remember, the only change I made was to change the ButtonType, nothing else.

This issue seems to be related to the UpdatePanel as when I remove the Ajax controls from the page, it performs exactly as expected...that is, my code to catch the MyGrid_RowCommand event fires whether the ButtonType is Button or Link. When I put the UpdatePanel back, the page only works when ByttonType is set to Button, but not for Link.

Please note that I do not want a HyperLink field. I want a ButtonField so that I can catch the click events and process them. My Code that I'm using for testing this is below.

Thanks in advance,

MJ

To test this, try the code as-is below to replicate the error. Change the "Code-Behind" and set the ButtonType to Button and it will work. For the third test, remove the Ajax controls from the Web Page and the code will work with ButtonType set to Button or Link

HTML PAGE

<%@dotnet.itags.org. Page Language="VB" AutoEventWireup="true" CodeFile="HomePage.aspx.vb" Inherits="_HomePage" %>
<!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>Testing</title>
</head>
<body>
<form id="form1" runat="server">
<atlas:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="True">
</atlas:ScriptManager>
<atlas:UpdatePanel ID="UpdatePanel1" runat="server" EnableViewState="true" >
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:GridView ID="MyGrid" runat="server" />
<asp:Label ID="lblResults" runat="server" />
</ContentTemplate>
</atlas:UpdatePanel>
</form>
</body>
</html>

CODE BEHIND


Partial Class _HomePage
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

MyGrid.DataBind()

End Sub
Protected Sub MyGrid_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyGrid.DataBinding

Dim MyRowPtr As Integer
Dim MyTable As New Data.DataTable

'Initialise Grid properties
With MyGrid
.AutoGenerateColumns = False
.BorderWidth = 1
.CellPadding = 0
.CellSpacing = 0
.Columns.Clear()
.EnableViewState = True
End With

'Create Table Columns
'Create Grid Columns
For MyRowPtr = 1 To 3
Dim lTableCol As New Data.DataColumn
lTableCol.ColumnName = Chr(MyRowPtr + 64)
MyTable.Columns.Add(lTableCol)

Dim lGridCol As New ButtonField
With lGridCol
.ButtonType = ButtonType.Link ' <======= CHANGE THIS LINE HERE
.ControlStyle.BorderWidth = 0
.ControlStyle.BorderStyle = BorderStyle.NotSet
.ControlStyle.BackColor = Drawing.Color.Azure
.CausesValidation = True
.HeaderText = Chr(MyRowPtr + 64)
.DataTextField = lTableCol.ColumnName
.CommandName = MyRowPtr.ToString
End With

MyGrid.Columns.Add(lGridCol)

Next

'Create 2 rows in the data table
Dim MyRow As Data.DataRow

MyRow = MyTable.NewRow
MyRow.BeginEdit()
MyRow("A") = "Row0 Col1"
MyRow("B") = "Row0 Col2"
MyRow("C") = "Row0 Col3"
MyRow.EndEdit()
MyTable.Rows.Add(MyRow)

MyRow = MyTable.NewRow
MyRow.BeginEdit()
MyRow("A") = "Row1 Col1"
MyRow("B") = "Row1 Col2"
MyRow("C") = "Row1 Col3"
MyRow.EndEdit()
MyTable.Rows.Add(MyRow)

MyTable.AcceptChanges()

MyGrid.DataSource = MyTable
MyGrid.Visible = True

End Sub

Protected Sub MyGrid_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles MyGrid.RowCommand

Dim SelectedRow As Integer
Dim SelectedColumn As Integer

'Re-bind the grid...required to save the formatting of the buttons
MyGrid.DataBind()

SelectedRow = Convert.ToInt32(e.CommandArgument)
SelectedColumn = Convert.ToInt32(e.CommandName)

Me.lblResults.Text = "You selected Column " & SelectedColumn.ToString & ", Row " & SelectedRow.ToString

End Sub
End Class

Check out this thread, see if the posted solution helps:

http://forums.asp.net/thread/1257072.aspx


Curious why do you create GridView columns programatically? You can define the columns in the aspx and still have dynamic table built on the server. I have GridView with linkbuttrons and it works fine. I wonder what happens if you declare ButtonField this way..


Matt M:

Check out this thread, see if the posted solution helps:
http://forums.asp.net/thread/1257072.aspx

Matt,

Thanks for the link. I read through it and found 3 "potential" scenario's I could try that might be a fix for my problem. I tried all 3 and I'm happy to say that one (but only one) worked.

The solution that worked for me was placing this code in my page_load event:
Page.ClientScript.GetPostBackEventReference(me, String.Empty)

It seems that this is a known bug, and according to the post, was supposed to have been fixed in prior releases (I'm on the July release). Is there somewhere where I can log this as a bug, or that it's still a bug?

Anyway, I'm all go again...thank you so much for responding to my original post, I appreciate it.

MJ


StanB:

Curious why do you create GridView columns programatically? You can define the columns in the aspx and still have dynamic table built on the server. I have GridView with linkbuttrons and it works fine. I wonder what happens if you declare ButtonField this way..

I Stan,

Yes, you're right, I could have done it the way you have suggested. The problem in my case is that the number (and type) of columns will change regularly depending on user interaction, so this is why I can't set them up at design time.

MJ

UpdatePanel / DataGrid / Internet Explorer Lock up

Hi!

I'm facing a strange problem and would like to know if anybody can help me out!
I've a page with 5 UpdatePanels which all use the same asynchronous Postback Trigger (a DropDown Box).

e.g.

<asp:ScriptManagerID="_scriptManager"runat="server"EnablePartialRendering="true">

<Services>

<asp:ServiceReferencePath="../services/positions.asmx"/>

</Services>

</asp:ScriptManager>

<asp:UpdatePanelID="UpdatePanel2"runat="server">

<ContentTemplate>

<asp:GridViewBorderWidth="0"CellSpacing="0"cellpadding="3"Width="100%"ID="grdCash"runat="server"ForeColor="#333333"GridLines="Horizontal"AutoGenerateColumns="False"ShowFooter="False">

<FooterStyleBackColor="#5D7B9D"Font-Bold="True"ForeColor="White"/>

<RowStyleBackColor="#F7F6F3"ForeColor="#333333"/>

<EditRowStyleBackColor="#999999"/>

<SelectedRowStyleBackColor="#E2DED6"Font-Bold="True"ForeColor="#333333"/>

<PagerStyleBackColor="#284775"ForeColor="White"HorizontalAlign="Center"/>

<HeaderStyleBackColor="#5D7B9D"Font-Bold="True"ForeColor="White"/>

<AlternatingRowStyleBackColor="White"ForeColor="#284775"/>

<Columns>

<asp:BoundFieldItemStyle-Width="80"DataField="SEGMENT"HeaderText="SEGMENT/CCY"HeaderStyle-HorizontalAlign="center"ItemStyle-HorizontalAlign="center"/>

<asp:BoundFieldDataField="DATUM"HeaderText="DATUM"HtmlEncode="False"DataFormatString="{0:dd.MM.yyyy}"HeaderStyle-HorizontalAlign="center"ItemStyle-HorizontalAlign="center"/>

<asp:BoundFieldDataField="FREE_CASH"HeaderText="FREE CASH"HtmlEncode="False"DataFormatString="{0:N}"HeaderStyle-HorizontalAlign="left"ItemStyle-HorizontalAlign="left"/>

<asp:BoundFieldDataField="SETT_TRANS"HeaderText="SETT.TRANS"HtmlEncode="False"DataFormatString="{0:N}"HeaderStyle-HorizontalAlign="right"ItemStyle-HorizontalAlign="right"/>

<asp:BoundFieldDataField="CPNS"HeaderText="CPNS"HtmlEncode="False"DataFormatString="{0:N}"HeaderStyle-HorizontalAlign="right"ItemStyle-HorizontalAlign="right"/>

<asp:BoundFieldDataField="REDEMPTIONS"HeaderText="REDEMPTIONS"HtmlEncode="False"DataFormatString="{0:N}"HeaderStyle-HorizontalAlign="right"ItemStyle-HorizontalAlign="right"/>

<asp:BoundFieldDataField="CASH_TRANSFERS"HeaderText="TRANSFERS"HtmlEncode="False"DataFormatString="{0:N}"HeaderStyle-HorizontalAlign="right"ItemStyle-HorizontalAlign="right"/>

<asp:BoundFieldDataField="DEPOSITS"HeaderText="DEPOSITS"HtmlEncode="False"DataFormatString="{0:N}"HeaderStyle-HorizontalAlign="right"ItemStyle-HorizontalAlign="right"/>

<asp:BoundFieldDataField="MAT_TD"HeaderText="MAT.TD"HtmlEncode="False"DataFormatString="{0:N}"HeaderStyle-HorizontalAlign="right"ItemStyle-HorizontalAlign="right"/>

<asp:TemplateFieldHeaderText="VALID"HeaderStyle-HorizontalAlign="center"ItemStyle-HorizontalAlign="center">

<ItemTemplate>

<asp:ImageID="imgValid"BorderStyle="None"runat="server"/>

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

</ContentTemplate>

<Triggers>

<asp:AsyncPostBackTriggerControlID="drpFonds"/>

</Triggers>

</asp:UpdatePanel>


The strange thing is that after a couple of refreshes (through the async postback trigger drpFonds) the Internet Explorer ( on the client ) locks-up and CPU utilization goes up to 100%.
I use some javascript code on this page, but there aren't any loops or timers (like setinterval etc.)
The partial postback executes some SQL queries and updates the datagrid(s) - nothing else happens.
I can't figure out what's wrong. Can anybody give me some hints on how to debug such a problem?

Thank you!

Regards
Holger

I had a problem just like that. It turned out that I was using a

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(calledFunction)

without removing it using

Sys.WebForms.PageRequestManager.getInstance().remove_endRequest(calledFunction)

Every time the update panel posted back it added another delegate call to the event. So over time the target function was being called over and over and over until it crashed the browser.

UpdatePanel + Composite COntrol + ViewState

I have a composite control that relies on the ViewState for some properties. however I am getting very strange behaviors.

If I set the following in webform1.aspx:

MyControl.XMLFileName = "MyFile.xml";

and inside my composite control I have:

public string XMLFileName
{
get
{
return ViewState["XMLfileName"] == null? "" : ViewState["XMLfileName"].ToString();
}
set { ViewState["XMLfileName"] = value; }
}

Wherever I am in my application: ViewState["XMLfileName"] is null.

If I go to the composite control and break inside of it, XMLFileName is "" (Viewstate["XMLfileName"] null). However, when I go to Webform1.aspx, and I look at the XMLFileName property it's set to the correct string.

How can my webform see the property of the composite control (from outside), but when I'm inside my composite control, I can't get the property?

Here's the aspx:

<asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server" EnableViewState="true">
</asp:ScriptManager>

....
<asp:UpdatePanel ID="upTest" runat="server" UpdateMode="conditional" EnableViewState="true">
<ContentTemplate>
<cw:Form ID="frmSample" runat="server" EnableViewState="true" />
</ContentTemplate>
</asp:UpdatePanel>

Hey,

ViewState is loaded at particular times, and that may be the problem you are experiencing. When do you look at viewstate to check the property value?


What happens is:
on the ASPX.CS Page:
-Set a few properties
-Call Control.LoadForm


inside my COmposite controls LoadForm function:
public void LoadForm()
{
...This is where I access the properties.
}

=========
in the ASPX.CS page the properties are not null/"", they are the right values. Inside the LoadForm() method, they are empty (the viewstate["value"] is null.


I also tried to set hte VIewState properly in the ASPX.CS page.

However, whenever I do: this.ViewState, ViewState, ViewState[...] or access the ViewState in any way from my CompositeControl I get:

'(What I did)' threw an exception of type 'System.ArgumentNullException' System.Web.UI.StateBag {System.ArgumentNullException}

I'm not sure how to get around this.

I need to set values in my control that I will not know at design time, only at run time. And those values need to stick seeing the control posts back to itself many times.


Hey,

OK, you set a few properties, and LoadForm is your own method, that does what? And, right after you set a value, you still can't get the property value in debugging? If after post back, make sure your LoadForm data doesn't occur too early, before viewstate is loaded. And, when referencing ViewState, if the key isn't present in the viewstate, then an exception will raise; always do null checks with viewstate data.


All of this is happening off of a button click.

protected void btnLoadForm_Click(object sender, EventArgs e)
{
LoadData();
}

protected override void LoadData()
{
frmSample.FormType = FormManagement.Entities.FormTypes.ItemDetail;
frmSample.XMLFileName = ddlSampleForms.SelectedValue;
Trace.Write("**Start rendering Form Control");
frmSample.LoadForm(false);
Trace.Write("**End Rendering Form Control");
}

When does the viewstate get loaded? Before OnInit()? And is it OnInit() of the Page, or OnInit() of the control?



Hey,

After Init, so this should work... http://www.15seconds.com/issue/020102.htm

So does the code actually work, but you can't debug it?


I changed the order the way the data was loaded in the Form.

I was always trying to load the data on the OnInit() method of the composite control - and no viewstate is loaded then.

I got it working now. :D

Saturday, March 24, 2012

UpdatePanel and custom Extender: two components with the same id cant be added to the appl

I am experiencing some strange behavior in my UpdatePanel which contains a ListView with a DataPager control. The items in the list view each have a custom ModalPopupExtender that is dynamically generated as well as a custom extender control that does some manipulation on an image.

The first page of results loads correctly, and I can move to the next page fine as well. However, when I move back to the first page, Firebug shows me the 'two components with the same id can't be added to the application' exception thrown on each of the items on the first page of the list, on the behavior associated with my custom extender. This to me seems that the partial update does not destroy or dispose the extender's behaviors when I move to the next page, so when I try to render the first page again, those extenders are still sitting in memory.

Now the kicker is, if I go to the next page and back to the first page a second time, my behaviors load correctly. Repeat and they don't show up and new errors are fired. I know this has to do with the lifecycle but I can't seem to pin it down.

Now my custom extender is fairly straightforward and only calls the base dispose() method for its own dispose() function. What am I missing?


I have narrowed down the issue to the fact that the Extender control uses a callback to attach an event to an image that has loaded dynamically from a web service call:

_onMethodComplete : function(result, userContext, methodName)
{
// Wire up image effect
var i = this.get_element();
i.onload = function(i)
{
asyncEffect(i); // This function lives in an external effects.js file
}

// Do after onload event to pick up change
this._setImage(result);
}

Doing this prevents the extender from disposing, possibly because it's still got a handle to an external JS file. I blanked out the image's onload event after the effect completes and the extender works for images that weren't previously on the page, but is still not disposing the extender correctly. Commenting out the wire up code above corrects the issue (obviously without the effects the JS file would bring).

How can I cleanly dispose an extender that references an external JS script?