I'm currently working on some ajax polling stuff and I don't want do pass through all the process if nothing has changed in the xml file I'm using for my asynchronous process.

However after each polling I want to set the value DateTime.Now, but cannot do it with

Request.Form[ "lastPoll" ] = DateTime.Now.ToUniversalTime().ToString();

because this is read only.

This is how i get the last polled datetime:

DateTime lastPoll = Convert.ToDateTime(Request.Form[ "lastPoll" ]).ToUniversalTime();

Thanks for your help.

I found some ninja thing, but if you have anything else, it would be appreciated.

NameValueCollection oQuery = Request.QueryString;
oQuery = (NameValueCollection)Request.GetType().GetField("_queryString",BindingFlags.NonPublic | BindingFlags.Instance).GetValue(Request);
PropertyInfo oReadable = oQuery .GetType().GetProperty("IsReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
oReadable.SetValue(oQuery, false, null);
oQuery["lastPoll"] = DateTime.Now.ToString();
oReadable.SetValue(oQuery, true, null);
commented: Informative. +8

Nevermind, this is not working :(

Request form collection is read-only so, we cannot directly modify it but reflection api will do that.

MethodInfo WritableMethod;
        MethodInfo ReadOnlyMethod;
        BindingFlags Flags = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;

        WritableMethod = Request.Form.GetType().GetMethod("MakeReadWrite", Flags);
        ReadOnlyMethod = Request.Form.GetType().GetMethod("MakeReadOnly", Flags);

        FieldInfo FormField = Request.GetType().GetField("_form", Flags);

        WritableMethod.Invoke(Request.Form, null);

        Request.Form["no"] = "100";

        FormField.SetValue(Request, Request.Form);
        ReadOnlyMethod.Invoke(Request.Form, null);

        Response.Write("<br/>" + Request.Form["no"]);

Thanks for the answer, however, it's not completly working. If I am simply adding a Request.Form["lastPoll"]; I'll get the correct value. However, if I alert right after the the previous code has ran, I'mgetting a null value.

This is my jQuery. ScriptToCheckTheFile is where I am setting the Date with the method you provided me.

The last function is only executed after ScriptTocheckTheFile.aspx has ran, so lastPoll should have changed.

Last poll is currently defined globally as null and is not used elsewhere.

function Poll2() {  $("#scripts").load("Ajax/ScriptToCheckTheFile.aspx", { 
projectId: $("#projectId").val(), 
lang: ($("#projectId").val().charAt(0) == "E") ? "eng" : "fra", 
lastPoll: lastPoll,
projectName: $("#projectId option:selected").text() }, 

function(result) {   

alert(lastPoll);

}

Thanks for the answer, however, it's not completly working. If I am simply adding a Request.Form["lastPoll"]; I'll get the correct value. However, if I alert right after the the previous code has ran, I'mgetting a null value.

This is my jQuery. ScriptToCheckTheFile is where I am setting the Date with the method you provided me.

The last function is only executed after ScriptTocheckTheFile.aspx has ran, so lastPoll should have changed.

Last poll is currently defined globally as null and is not used elsewhere.

function Poll2() {  $("#scripts").load("Ajax/ScriptToCheckTheFile.aspx", { 
projectId: $("#projectId").val(), 
lang: ($("#projectId").val().charAt(0) == "E") ? "eng" : "fra", 
lastPoll: lastPoll,
projectName: $("#projectId option:selected").text() }, 

function(result) {   

alert(lastPoll);

}

I am not affecting the global lastPoll var, I'm only affecting the dataname, and when we get out of the curly brackets, it's gone. I'd name a way to put it public or to set directly the variable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.