do you mean a server event handler? If so, the page must go through its whole lifecycle.
Wednesday, March 28, 2012
Direct method call from html event
Difference between Callback and Webmethod
Hi,
What's the difference between using a callback event handler and a method defined as a WebMethod? One major difference I see is that a callback event involves some aspects of the page lifecycle. When is one advisable to be used over the other?
Thanks.
Hi,
On client side, the difference isn't very big. Both of them will use xmlHttpRequest to send to request to the server.
But it's different on server side. When work with callback, a new instance of the page is created to server the request, and a corresponding method defined in it will be fired. While with web method, no instance of page will be created, instead, an instance of the web service is created.
The latter one is a light-weighted way.
Monday, March 26, 2012
Determining the source of a partial rendering postback
Is there a way to determine (on the server) what trigger/control/event was the source of the postback? How about something that indicates what update panel(s) will be rendered?
I see that the script manager has an IsInAsyncPostBack property, but I'm curious if there is something more specific.
UpdatePanel.IsInPartialRendering does not seem to work.
You could check the _EventTarget for a hint on what triggered the PostBack.
What would you do differently if you knew what triggered the PostBack?
You could use the ScriptManager.AsyncPostBackSourceElementID to determine the source of the asynchronous postback. In terms of what UpdatePanels will get updated, in server code I don't think there is a summary list that lists them because the Update() method can be called by any code and tracking that is difficult if not impossible(?). However, on the client you can use the pageLoaded or pageLoading events of the PageRequestManager to get information about what panels will be created, updated or deleted.
Determine what element caused the Postback when handling the endRequest event
Hi All,
I have an UpdatePanel with 2 buttons. Pressing either of them causes a Postback and the panel gets refreshed.
I'm handling the endRequest event on the client side. Is there a way to determine which of the page elements caused the Postback?
Thanks,
Uzi
Try here:
http://www.eggheadcafe.com/articles/20050609.asp
Hi,
you can determine the postback element on the client side with the PageRequestManager. Attach to the beginRequest event and you can save the postback control. More information here:
http://ajax.asp.net/docs/ClientReference/Sys.WebForms/BeginRequestEventArgsClass/default.aspx
Regards
Marc André
Here is a neat way to get it done:
In the 'BeginRequest' event handler insert the postback element to the UserContext of the WebRequest:
function BeginRequestHandler(sender, args){
var elemName = args.get_postBackElement().id;var request = args.get_request();request.set_userContext(elemName);
}
Then, when the request ends, at the 'EndEvent' get the information from the Response object:
function EndRequestHandler(sender, args){
var response = args.get_response();alert(response._webRequest.get_userContext());
}
Thanks,
Uzi
Detecting which UpdatePanel was updated for endRequest
I've got an issue where I'm trying to execute some javascript when a specific UpdatePanel on my page (I have about 5) is updated. I've added event handlers for initializeRequest and endRequest. I know that I can get the ID of the element that *caused* the postback - but I really don't want that. I need the id of the UpdatePanel that is being updated.
Then endRequest method that gets called after an UpdatePanel gets updated is called for any and all UpdatePanels - this ambiguity makes it difficult to know which UpdatePanel caused this request.
Any ideas?
Cheers,
Bobby
The endRequest event doesn't have the information you want. If you handle the pageLoaded event, the args parameter that gets passed in to your handler has a panelsUpdated property which returns an array containing the panels that were updated.
Detailsview ItemUpdate Event with UpdatePanel
I have a grid with individuals in it, the user selects and individual which populates a Detailsview below. Both gridview and Detailsview are wrapped with UpdatePanels. The user can then edit the individual in the Detailsview and submit changes when updateing. Now the updatepanel wrapping the gridview has an <asp:AsyncPostBackTrigger tied back to the Detailsview's ItemUpdated event.
I ultimately want the grid of selected individuals to update whenever the user makes changes to the individuals through the supporting gridview, the trigger I am using is not providing the functionality I am looking for, what approach should I take next? or is this trigger not working properly?
I can provide code-snippets, but figured it would be better to get a high level overview then look at some of my mess :)
Thanks in advance to anyone who offers help.
Here are some sample codes for your reference..ASPX
<div>
<asp:UpdatePanel ID="gvUpnl" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="gvProduct" runat="server" AutoGenerateSelectButton="true" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true" AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True" DataKeyNames="ProductID" DataSourceID="sqlDS" OnRowCommand="gvProduct_RowCommand">
<Columns>
<asp:TemplateField HeaderText="ProductID" SortExpression="ProductID">
<ItemTemplate>
<asp:Label ID="lblProductID" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" SortExpression="Quantity" />
<asp:BoundField DataField="TransactionDate" HeaderText="TransactionDate" ReadOnly="True"
SortExpression="TransactionDate" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sqlDS" runat="server" ConnectionString="<%$ ConnectionStrings:ProductConnectionString %>" DeleteCommand="DELETE FROM [Product] WHERE [ProductID] = @.ProductID" InsertCommand="INSERT INTO [Product] ([ProductName], [UnitPrice], [Quantity], [TransactionDate]) VALUES (@.ProductName, @.UnitPrice, @.Quantity, @.TransactionDate)" SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice], [Quantity], [TransactionDate] FROM [Product]" UpdateCommand="UPDATE [Product] SET [ProductName] = @.ProductName, [UnitPrice] = @.UnitPrice, [Quantity] = @.Quantity, [TransactionDate] = @.TransactionDate WHERE [ProductID] = @.ProductID">
<DeleteParameters>
<asp:Parameter Name="ProductID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="ProductName" Type="String" />
<asp:Parameter Name="UnitPrice" Type="Double" />
<asp:Parameter Name="Quantity" Type="Int32" />
<asp:Parameter Name="TransactionDate" Type="DateTime" />
<asp:Parameter Name="ProductID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="ProductName" Type="String" />
<asp:Parameter Name="UnitPrice" Type="Double" />
<asp:Parameter Name="Quantity" Type="Int32" />
<asp:Parameter Name="TransactionDate" Type="DateTime" />
</InsertParameters>
</asp:SqlDataSource>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="gvProduct" EventName="RowCommand" />
</Triggers>
</asp:UpdatePanel>
<asp:UpdatePanel ID="dvUpnl" runat="server">
<ContentTemplate>
<asp:DetailsView ID="dvProduct" runat="server" DataKeyNames="ProductID" AllowPaging="True" DataSourceID="dvDS" AutoGenerateEditButton="true" AutoGenerateInsertButton="true" AutoGenerateDeleteButton="true">
</asp:DetailsView>
<asp:SqlDataSource ID="dvDS" runat="server" ConnectionString="<%$ ConnectionStrings:ProductConnectionString %>">
<SelectParameters>
<asp:Parameter Name="ProductID" Type="Int32" Direction="Input" DefaultValue="1"/>
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name="ProductID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="ProductID" Type="Int32" Direction="Input" />
<asp:Parameter Name="ProductName" Type="String" />
<asp:Parameter Name="UnitPrice" Type="double" />
<asp:Parameter Name="Quantity" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</ContentTemplate>
</asp:UpdatePanel>
</div
Behind Code:
protected void Page_Load(object sender, EventArgs e)
{
string cString = System.Configuration.ConfigurationManager.AppSettings["ProductConnectionString"];
dvDS.ConnectionString = cString;
dvDS.SelectCommand = "SELECT ProductID,ProductName,UnitPrice,Quantity,TransactionDate FROM Product WHERE ProductID=@.ProductID";
dvDS.UpdateCommand = "UPDATE Product SET ProductName=@.ProductName,UnitPrice=@.UnitPrice,Quantity=@.Quantity WHERE ProductID=@.ProductID";
}
protected void gvProduct_RowCommand(object sender, GridViewCommandEventArgs e)
{
Label lblProductID = gvProduct.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("lblProductID") as Label;
string productID = lblProductID.Text;
if(e.CommandName == "Select")
{
dvDS.SelectParameters["ProductID"].DefaultValue = productID;
}
else if(e.CommandName == "Edit")
{}
else if(e.CommandName == "Delete")
{}
}
Wish the above can help you.
DetailsView & ObjectDataSource: Cancel Insert but Retain User Input
Hi,
I need to cancel an insert operation during a DetailsView/ObjectDataSource insert event, and display to the user the same insert page with their previously entered values. For example, after a server side input validation the user may need to modify some of the values they entered.
Canceling insert is easy in the ObjectDataSource.Inserting event handler: e.Cancel = true;
However, this cancels insert mode and the values the user input are lost. The ObjectDataSource.Inserting event handler has access to the input parameters, which contain the values the user has entered. It is possible to put the DetailsView object back into insert mode: DetailsView1.DefaultMode = DetailsViewMode.Insert; but I don't know how to programatically assign the user values to the fields, since I do not know how to get a handle to the field.
How does one retain the user input values in insert mode when canceling an inserting event in the DetailsView/ObjectDataSource programming model?
Thanks,
Paul
You should validate the input values in the DetailsView ItemInserting event, rather than in the ObjectDataSource Inserting event. In such case the input values will be retained though you cancel the inserting event
protected void DetailsView1_ItemInserting (object sender, DetailsViewInsertEventArgs e)
{
// validate
e.Cancel = true;
}
Thanks
-Mark post(s) as "Answer" that helped you
Wednesday, March 21, 2012
DefaultButton in Firefox
I added a DefaultButtion to a normal asp panel to handle enter key event. This works fine in IE, but has problem in FireFox.
This asp panel is in anupdate panel.
When user presses enter key in FireFox, this does not trigger OnClick function of the button, it only triggers client side function OnClientClick. My submit button code is in server side OnClick function. So when user presses the enter key, nothing is saved.
I debugged the javascript, traced to following function:
function WebForm_FireDefaultButton(event, target) {
if (event.keyCode == 13 &&
!(event.srcElement &&
event.srcElement.tagName.toLowerCase() == "textarea")) {
var defaultButton;
if (__nonMSDOMBrowser) {
defaultButton = document.getElementById(target);
} else {
defaultButton = document.all[target];
}
if (defaultButton && typeof defaultButton.click != "undefined") {
defaultButton.click(); myapplication continues. But web application which does not use Ajax stops here and posts back. ?
event.cancelBubble = true;
if (event.stopPropagation) {
event.stopPropagation();
}
return false;
}
}
return true;
}
Is this problem related to update panel. I used the approach inhttp://forums.asp.net/thread/1685533.aspx. (UseSubmitBehavior="False" OnClientClick="ResetDefault" ).But that does not work.
Any suggestion will be much appreciated.
Any luck with this issue? Sounds like I have the same issue as you and I also tried the ResetDefault function with no luck. I reset the __defaultFired variable (i think that's the name of it) inside the page_prerender using the registerclientstartupscript method with no luck. I noticed that the the enter button does cause a postback, it just doesn't fire the button that it is suppose to be firing. If you look at the Request.Forms collection the button is missing from it. If you look at the same collection after clicking the button with the mouse it is there. I hope someone can shed a little light on this matter. I am going to try to create a simple page to reproduce the issue.