ASP.NET session in Javascript - asp.net

I have the following code invoked from a button click - js event
function onOkReason() {
alert("called");
var session = $('<%=Session["A"]%>');
if (session == null) {
<%Session["A"]%> = "1";
alert("1");
}
else {
<%Session["A"]%> = null;
alert("2");
}
alert(session);
}
It does not seem to work...Any pointers as to what may be wrong...

The <% %> is evaluated when the page is processed on the server, you can't then set it from the client - it's to late, the page has been processed and sent back to the clients browser.
You'll need to post back to the server (either full or an AJAX call) to set the data.

Related

Delete a record as ending a session

I want to delete a record from data base as a user close the browser. the record which i want to delete is belong to the user who close the browser or log out. I write this code in global but it doesn't work. Can any body help me.
Note: I want to delete the record that has username of log outed user.
thank you very much
the global code:
void Session_End(object sender, EventArgs e)
{
string connStr = ConfigurationManager.ConnectionStrings["dbconn"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(connStr);
SqlCommand sqlcmd = new SqlCommand("delete ChatRoomList where UserName=#UserName", sqlconn);
sqlcmd.Parameters.AddWithValue("#UserName", (string)Session["User"]);
sqlconn.Open();
sqlcmd.ExecuteNonQuery();
sqlconn.Close();
}
There is no reliable way to achieve this. Session_End event would only fire for in-process session state storage and the trigger point would be session expiration which is different than the browser close or user log-out.
So in your case, you may observe the event when your session actually gets timed out (if you have a logout link then you can force session expiry using Session.Abandon)
There are some other ways such as making an AJAX call to server to tell that user has logged out (or closing the browser window) and they may provide you with better results but again not 100% reliable. The most reliable way that I can think of is to have your own timer - i.e. to ping the server (using AJAX call) periodically from browser side and when pings are not received within certain time-out value, assume the user session to be dead/ended.
It will work on IE
window.onbeforeunload = function (event) {
if (((window.event.clientX || event.clientX) < 0) || ((window.event.clientY || event.clientY) < 0)) // close button
{
//call here you you jQuery function to delete either page method, web service or http
}
if ((window.event.clientX < 0) || (window.event.clientY < 0)) // close button
{
//call here you you jQuery function to delete either page method, web service or http
}
else if (window.event.altKey == true || window.event.ctrlKey == true) // ALT + F4
{
//call here you you jQuery function to delete either page method, web service or http handler
}
else // for all other unload events
{
}
}
});

Ajax code is faster or updatepanel is faster to populate dropdown in asp.net

I have 20 ajax editable drop-downs in a form. Each drop-down binds with respect to other's selectedindex change.
I have lots of record coming from the database for binding, and some operations. What will perform better?
Should I use this code to bind the drop-down:
var XmlHttp;
//Creating object of XMLHTTP For AJAX Method
function CreateXmlHttp() {
//Creating object of XMLHTTP in IE
try {
XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (oc) {
XmlHttp = null;
}
}
//Creating object of XMLHTTP in Mozilla and Safari
if (!XmlHttp && typeof XMLHttpRequest != "undefined") {
XmlHttp = new XMLHttpRequest();
}
}
function GetAppStoreLnk(id) {
var txtnameid = document.getElementById(id);
CreateXmlHttp();
var requestUrl = "Default2.aspx?id="+txtnameid+"";
if (XmlHttp) {
XmlHttp.onreadystatechange = function() { getschemename(txtnameid) };
XmlHttp.open("GET", requestUrl, true);
XmlHttp.send(null);
}
}
function getschemename(id)
{
// To make sure receiving response data from server is completed
if(XmlHttp.readyState == 4) {
// To make sure valid response is received from the server, 200 means response received is OK
if(XmlHttp.status == 200) {
var strData = XmlHttp.responseText;
if(strData != "") {
var arrscheme = strData.split("|");
id.length = 0;
for(i=0; i<arrscheme.length-1; i++) {
var strscheme = arrscheme[i];
var arrschnm = strscheme.split("~");
id.options[i] = new Option();
id.options[i].value = arrschnm[0];
id.options[i].text = arrschnm[1];
}
} else {
id.length = 0;
id.options[0] = new Option();
id.options[0].value = "";
id.options[0].text = "Scheme Name is not available";
}
document.body.style.cursor = "auto";
}
else {
id.length = 0;
id.options[0] = new Option();
id.options[0].value = "";
id.options[0].text = "server is not ready";
document.body.style.cursor = "auto";
}
}
}
Or should I go for UpdatePanel?
Which one will be the better option for me?
Or is there a better spolution?
I want very good performance as this page is used frequently by our client, and we want to make sure it is fast.
I am using vs2010.
The AJAX route will be faster. UpdatePanels make things look like partial page loads, but in fact, it returns the whole page and just updates the section inside your UpdatePanel.
UpdatePanel does not provide the efficiency that we normally associate with AJAX. Did you know, for example, that when UpdatePanel control performs asynchronous AJAX callback to the server to update its content, the request contains all the content of a publication automatically ASP. NET conventional, including status display.
An application is more effective preferring to use asynchronous calls to WebMethods or page methods instead of using UpdatePanel.
Link : http://msdn.microsoft.com/fr-fr/magazine/cc163413.aspx
I agree with the rest of the answers, the evil UpdatePanel decreases the overall performance of the page because in order to work, it must send the whole page ViewState back and forth in order to execute the whole page life cycle on every async post
With simple AJAX calls this is totally different, the calls only get the data you need and the performance is considerably better
However - danger danger DR Robinson
Since you are using WebForms and your intention is to populate 20 DropDownLists, you can try doing it with AJAX calls and you will notice that apparently that works correctly. but if you try to post your page with one value added to a DropDownList via javascript/jquery, an exception will be thrown.
This is because ASP.Net WebForms by default validates the values posted to the server, if the values are not registered in the ViewState, a security exception will be thrown because of an attempt to tamper the page content.
Sadly you cannot disable this option at control level, you would have to disable it at page level:
<%# Page EnableEventValidation="false" ....
In WebForms this is not recommended though...

How to know server response in ResponseEnd method of a RadAjaxManager

Im using Rad ajax manager, RadAjaxLoadingPanel in my webform.
I have two panels in my form, Panel1 is having Create account controls and another Panel2 is for Thank you notes.
When user created an account successfully i need to hide Panel 1 and show Panel 2.
Im using ResponseEnd method to do make visible/hide using Javascript below method.
function ResponseEnd(sender, arguments) {
//hide the loading panel and clean up the global variables
if (currentLoadingPanel != null) {
currentLoadingPanel.hide(currentUpdatedControl);
}
ShowTY();
currentUpdatedControl = null;
currentLoadingPanel = null;
}
function ShowTY(){
document.getElementById('<%= Panelty.ClientID %>').style.visibility = "visible";
document.getElementById('<%= Panelty.ClientID %>').style.display = "block";
document.getElementById('<%= Panelsu.ClientID %>').style.visibility = "false";
document.getElementById('<%= Panelsu.ClientID %>').style.display = "none";
}
If user already exist or any db server error i need show Panel1 display error message in a Label
For this I need to write a condition to check whether server response succeeded or not in the above method.
Please let me know how i can know the server response or how i can handle this issue.....
Please reply soon
Thanks
According to the documentation: There is no way to pass data from an event on the server to the client side event handler.
I would suggest moving the logic to configure the display (hiding/showing elements, displaying error messages) to the server, where the code to create the account is. Since you are using the loading panels, this should be straightforward:
if(accountCreatedSuccessfully) {
Panelty.Visible = true;
Panelsu.Visible = false;
}
else {
// TODO: display the error messages somewhere, in a label
Panelty.Visible = false;
Panelsu.Visible = true;
}

How can I protect my asp.net Handler page

I'm using this practice to add comments using AJAX , by sending the data to an ASP.NET Handler which collect the information and then insert the comment, but I am afraid that any one could use it , am I wrong !?
//AddComment.ashx
public void ProcessRequest (HttpContext context) {
CommentsDB db = new CommentsDB();
db.InsertComment(new Comment(context.Request["name"].ToString(), context.Request["comment"].ToString(), "no", int.Parse(context.Request["id"].ToString())));
context.Response.ContentType = "text/plain";
context.Response.Write("succeed");
}
//Comments.js
function AddComment()
{
n = document.getElementById('txtName').value;
c = document.getElementById('txtComment').value;
i = document.getElementById('ctl00_ContentPlaceHolder1_thread').value;
m = document.getElementById('ctl00_ContentPlaceHolder1_Label1');
if(n == "" || c == "" || n.length > 100 || c.length > 400)
{
m.innerHTML = "<center><font color=black size=3><b><font color=red>*</font> An error has occurred</b></font></center><br>";
return;
}
m.innerHTML = "";
document.getElementById('btn').disabled = true;
$.post("./Handlers/AddComment.ashx", {'name':n, 'comment':c, 'id':i}, function(Response){
m.innerHTML = "<center><font color=black size=3><b>accepted</b> <img src=./Images/success-icon.png></font></center><br>";
});
}
Your assumption is correct, that your users can potentially make their own HTTP requests to your handler, and provide bogus data. They could also manipulate your page markup in their browsers (with any developer toolbar) and do the same.
So, you're going to want to do some validation on your server side if you're worried about this. If your application requires authentication, just look up the current user's name in the handler's ProcessRequest method, rather than posting it.
I think that's what your question is getting at. Also, clean up your markup, center and font tags are deprecated.
If you require that the commenters to be logged in than check for the actual user (stored on the web server - in session for example).
Or if you allow non authenticated comments, than consider using some captcha to protect against automated requests.

How to open new window with streamed document in ASP.NET Web Forms

I have an ASP.NET Web Forms application. I want to have a button to post back to the server that will use my fields on my form (after validation) as parameters to a server process that will generate a document and stream it back to the browser. I want the form to be updated with some status results.
What is the best way to achieve this? Right now, I've got the button click generating the document and streaming it back to the browser (it's a Word document and the dialog pops up, and the Word document can be opened successfully) but the page doesn't get updated.
I have jQuery in my solution, so using js isn't an issue if that is required.
I have a very similar process on one of my servers, and the way I've handled it is to create a temporary document on the server rather than doing a live stream. It requires a bit of housekeeping code to tidy it up, but it does mean that you can return the results of the generation and then do a client-side redirect to the generated document if successful. In my case, I'm using jQuery and AJAX to do the document generation and page update, but the same principle should also apply to a pure WebForms approach.
This was way more difficult to do than I thought. The main issue is with opening a new browser window for a Word document. The window briefly flashes up, then closes - no Word document appears. It seems to be a security issue.
If i click a button on my page, I can stream the Word doc back as the response, and the browser dialog pops up allowing me to Open/Save/Cancel, but of course, my page doesn't refresh.
My final solution to this was to use a client script on the button click to temporarily set the form's target to _blank. This forces the response to the click on the postback to go to a new browser window (which automatically closes after the download dialog is dismissed):
<asp:Button Text="Generate Doc" runat="server" ID="btnGenerateDoc"
onclick="btnGenerateDoc_Click" OnClientClick="SetupPageRefresh()" />
My SetupPageRefresh function is as follows:
function SetupPageRefresh() {
// Force the button to open a new browser window.
form1.target = '_blank';
// Immediately reset the form's target back to this page, and setup a poll
// to the server to wait until the document has been generated.
setTimeout("OnTimeout();", 1);
}
Then my OnTimeout function resets the target for the form, then starts polling a web service to wait until the server process is complete. (I have a counter in my Session that I update once the process has completed.)
function OnTimeout() {
// Reset the form's target back to this page (from _blank).
form1.target = '_self';
// Poll for a change.
Poll();
}
And the Poll function simply uses jQuery's ajax function to poll my web service:
function Poll() {
var currentCount = $("#hidCount").val();
$.ajax({
url: "/WebService1.asmx/CheckCount",
data: JSON.stringify({ currentCount: currentCount }),
success: function (data) {
var changed = data.d;
if (changed) {
// Change recorded, so refresh the page.
window.location = window.location;
}
else {
// No change - check again in 1 second.
setTimeout("Poll();", 1000);
}
}
});
}
So this does a 1 second poll to my web service waiting for the Session's counter to change from the value in the hidden field on the page. This means it doesn't matter how long the server process takes to generate the Word document (and update the database, etc.) the page won't refresh until it's done.
When the web service call comes back with true, the page is refreshed with the window.location = window.location statement.
For completeness, my Web Service looks like this:
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService1 : WebService
{
[WebMethod(EnableSession=true)]
public bool CheckCount(int currentCount)
{
if (Session["Count"] == null)
Session["Count"] = 0;
var count = (int)Session["Count"];
var changed = count != currentCount;
return changed;
}
}
Hopefully that helps somebody else!

Resources