Showing posts with label current. Show all posts
Showing posts with label current. Show all posts

Monday, March 26, 2012

Detect if a request is a partial postbak?

Hello,

is it possible to check if the current request is a partial postback or not?


I am getting PageRequestManagerParserErrorExceptions when i use a response filter that does a gzip compression in global.asax and my idea was to detect if the current request is a partial one. I that case i would not apply the filter.


The only question is if it is possible to detect the difference between normal and partial postbacks.

Any hints?

Ciao,

Ralf

ScriptManager.IsInAsyncPostBack.

Thanks!

My only problem is now that the process of applying the filter happens in global.asax and i have to find out how to access my scriptmanager instance (scriptmanager is contained in the masterpage) from within global.asax.


OK, i have added the scriptmanager to the application now so i cann access it from within global.asax but: When performing pagings in a gridview that is contained in an updatepanel the ScriptManager.IsAsyncPostback shows "false" (i have defined the paging events of the gridview as triggers so i expected to see IsAsyncPostback=true)

Is the IsAsyncPostBack property only valid at page level (i mean not useable in global.asax)?


I'm not sure, I'll defer to your experiences as an indicator that it's a page / instance level variable.

The request itself gets modified by the runtime, though, so that if you have an http reuqest fired by your updatepanel, it should have the additional http header of:

x-microsoftajax: Delta=true.

If you check the Request.Headers collection, that should be a way for your module to do the detection you're lookign for.

Hope that helps.

Paul


Hi Paul,

Yes, that helps! Now i can apply gzip/deflate filtersand use updatepanels without getting PageRequestManagerParserErrorExceptions by simply not using the filter on partial postbacks.

Thanks for the hint,

Ralf


Detect AJAX postback in Application.EndRequest

Anyone know if there's a way to detect whether the current request is an AJAX postback or a regular postback from inside the Application.EndRequest event of an HttpModule? I have a module that writes out information to the end of a normal request (using response.write). However, this causes an AJAX postback request to fail. Or maybe there's a better way to append some text to the end of a request than response.write?

Ideas?

Test for (Request["X-MicrosoftAjax"] == "Delta=true"). That request header is present in partial postbacks.


Most excellent. Thanks!

HOWEVER, I'm a bit PEEVED at Microsoft for making the client-side AJAX javascript so sensitive to the data that comes back. If any part of my code does a response.write anywhere, the stupid AJAX Javascript on the client goes wobble-kneed and dies. Sheesh! I'd really like to be able to write out some text (using response.write) in both the master-page AND the httpModule's end_request event, but the AJAX Javascript on the client won't allow it! What a pain.

I've seen some c# code on the internet that shows how to modify the response that AJAX returns, but it's confusing as it's doing text translation (i.e., the function of the code is getting in the way of me understanding what it's doing to the response string).

Thanks for the answer to the original question.


Use some sort of HTTP viewer to inspect a partial postback response, and you'll probably see a way to do what you want to. Once you see it, hands-on, it's not too difficult to figure out how to alter (or see why Response.Write kills it).

I use FireBug for that, myself. I think Fiddler is a comparable IE addon.

Wednesday, March 21, 2012

Default current date in Calendar

When using the Calendar extender, is there a way to put a default current date in the textbox?

Auto populate the text field of the textbox with <%# DateTime.Now.ToShortDateString() %> or make the assignment in code-behind.


I realized that I can assign in code behind but I don't want to do that, that's why I asked because I thought the calendar extender should allow me to do that easily. I can't do this, <%# DateTime.Now.ToShortDateString() %>, since this textbox is bind to a database.


I don't believe the calendar extender can do it automatically, but you could through JavaScript; though, you could also check the textbox and if nothing has been assigned after binding, then make the assignment.


bmains:

Auto populate the text field of the textbox with <%# DateTime.Now.ToShortDateString() %> or make the assignment in code-behind.

Okay, I thought I can try something like this:

<asp:TextBox ID="txtDate" runat="server" Text='<%# Bind("dateCreate", "{0:d}", DateTime.Now.ToShortDateString())%>' Width="80px"></asp:TextBox><br />   

But when compile it gave me this error:

Error 1 A call to Bind was not well formatted. Please refer to documentation for the correct parameters to Bind. E:\Website\Outlook\admin\admOLissue.aspx 110

Any othe ideas?


Hello,

I'm not sure, but I don't think Bind supports the last parameter. If that is the case, you could create a public method that takes an object as such:

public function FormatDate(objDate As Object) As String
if (objDate is Nothing orelse objDate.Equals(DbNull.Value)) then
return DateTime.Now.ToShortDateString()
else
return CType(objDate, DateTime).ToShortDateString()
end if
end function

And in the textbox:

<asp:TextBox Text='<%# FormatDate(Bind("dateCreate"))%>'


Okay, I got the following error when compiled:

Error 2 The name 'Bind' does not exist in the current context E:\Website\admOLissue.aspx 110

This error comes from this line:

<asp:TextBox Text='<%# FormatDate(Bind("dateCreate"))%>'

By the way, I'm using C#.


Try using Eval instead.


If I use Eval instead of Bind, will it bind to the database if I click on the Update button?


Updates are not based on Bind or Eval statements, but on the value in textbox, not the value passed through Bind or Eval statements... If Bind doesn't work anyway, try to see if Eval will work in that context...


Okay, that seems to fix it.Thanks so much!


Hi there,

I have tried the statement with Eval and didn't work for me. When I click on edit and the field shows the correct date. But once i click on update it doesn't show anything. Is there any other possible way of doing this?

Thanks


I'd be able to help better if you posted the relevant code. Post the control you are doing this with, as well as the parent control that it is in, and how you are updating.


I think the current date ends up in the bottom of the calendar control and you can click on it to take today's date. I know this isn't great, but with a little bit of user training, it can work. I'd prefer to be able to specify today's date to be the default date as well, but since it's not available, this is the best option I could find and not have to rig it up programmatically.


bmains:

Try using Eval instead.

Not sure why, but it's not working. The value will not get updated in the database. Code in .aspx page:

<asp:TextBox ID="txtModDate" runat="server" Text='<%# defaultDate(Eval("modDate"))%>'></asp:TextBox>

Code behind:

1public string defaultDate(Object crntDate)2 {3string currentDate = Convert.ToDateTime(crntDate).ToShortDateString();4if (!String.IsNullOrEmpty(currentDate))5 {6return DateTime.Now.ToShortDateString();7 }8else9 {10return currentDate;11 }

Here is the popup error:

Cannot insert the value NULL into the column 'modDate', table "myDB.dbo.myTable'; column does not allow nulls. INSERT fails. The statement has been terminated.