Thursday, January 15, 2015

Finding PostBack Control from ClientSide JavaScript in ASP .net.

We can Find the Control Creating PostBack in ASP .net webform from ClientSide Script using following Code and Also can Capture the end of Asynchronous postback event in case of update panel  where generally focus is lost after asynchronous post back.

 $(function () {

 Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function (sender, e) {
         if (sender._postBackSettings.sourceElement.id == '<%=ddlLedger.ClientID %>')

                       //Your Control creating postback
                      $("#ctl00_ContentPlaceHolder1_ddlSubLedger_chosen .chosen-single").focus();
         else {
                  //do your thing
                }

            });        
});


Hence on endrequest we can get the postback control on completion of post back and capture the after post back event to do what needed to like may be a focus to next control which is usually lost in case we are using update panel or in case of chosen dropdown.

Wednesday, January 7, 2015

Ajax autoComplete extender Full Working example.

In This Example I am going to Show how to use AutoCompleteExtender of AjaxControlToolkit in asp .net.

For this we need a textBox , and one HiddenField for Storing Selected Items Value.

<asp:TextBox ID="txtSubledger" runat="server" Visible="true" AutoPostBack="True"                                                     OnTextChanged="ddlSubLedger_textChanged"></asp:TextBox>
                                              

  <ajax:AutoCompleteExtender ID="AutoCompleteExtender4" MinimumPrefixLength="3" runat="server"
                                                    TargetControlID="txtSubledger" ServiceMethod="GetCompletionListSubledger" FirstRowSelected="True"
                                                    UseContextKey="True" DelimiterCharacters="" Enabled="True">
 </ajax:AutoCompleteExtender>


  <asp:HiddenField runat="server" ID="ddlSubLedger" />


now on code behind we need a webservice for ServiceMethod="GetCompletionListSubledger" 

 [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static string[] GetCompletionListSubledger(string prefixText, int count, string contextKey)
    {
        DataTable dtAN = TblSubLedgerService.getsubldegertablebyename(prefixText);
        Dictionary<string, string> dt = new Dictionary<string, string>();
        foreach (DataRow dr in dtAN.Rows)
        {
            dt.Add(dr["SubLedgerID"].ToString(), dr["SubLedgerName"].ToString());
        }
        return (from m in dt.Values.ToArray<string>() select m).Take(count).ToArray();
    }


a text Changed event for textbox

   protected void ddlSubLedger_textChanged(object sender, EventArgs e)
    {
        if (!string.IsNullOrWhiteSpace(txtSubledger.Text))
            SetModuleNameListforSubledger(txtSubledger.Text, 3);
        else ddlSubLedger.Value = "";
    }
 

text Changed event calls SetModuleNameListforSubledger Which will basically call previous function of web service but will set hidden field in case of selection of any item.

    protected void SetModuleNameListforSubledger(string prefixText, int count)
    {
        DataTable dtAN;
        dtAN = TblSubLedgerService.getsubldegertablebyename(prefixText);
        if (dtAN == null || dtAN.Rows.Count < 1)
        {
            ddlSubLedger.Value = "";
            return;
        }
        ddlSubLedger.Value = dtAN.Rows[0]["SubledgerID"].ToString();
    }

Here, TblSubLedgerService.getsubldegertablebyename(prefixText) is in Service class and will fetch data from the database with filter.

         public static DataTable getsubldegertablebyename(string subledger)
        {
            const string query = "select top(20) SubLedgerID,code+' '+SubLedgerName as SubLedgerName from tbl_SubLedger where  code+' '+SubLedgerName like @Subledger";
            var tbl = DAO.GetDataTable(query, args => args.Add("@Subledger", string.Concat("%", (string.Concat(subledger, "%")))));
            return tbl;
        }


 Now, we can get selected data from autocomplete in hidden field.

Monday, January 5, 2015

Increase Session Time Out in ASP .NET Application.

We Can Increase Session Time Out Using Following Two Methods.

1. Using Web.Config File.
2. Using IIS setting in the windows server(Application Server).

Using Web.Config File.

Using  web.Config file we need to add timeout="minutes" in  sessionstate of system.web section of web.config file.

<system.web>
        <sessionState timeout="540"/>
.....


 Using IIS setting in the windows server(Application Server).


Change the following time-outs in Internet Services Manager .Choose a value greater than the default of 20.
    1. Select Default Web Site > Properties > Home Directory > Application Settings > Configuration > Options.
    2. Enable the session state time-out and set the Session timeout for 60 minutes.

    3. Select Application Pools > DefaultAppPool > Properties.
    4. From the Performance tab under Idle timeout, set Shutdown worker processes after being idle for a value higher than 20.

The default session time-out setting on IIS is 20 minutes but it can be increased to a maximum of 24 hours or 1440 minutes. See Microsoft article Q233477 for details about increasing the timeout in IIS.
Symptom
When returning to the logon page for Web Interface, users often encounter an Error: Your session with the web-server expired. You have been logged out.
Cause
Web Interface 2.0 picks up the session timeout setting from IIS.
Resolution
1. Start Internet Services Manager 5.0.
2. For explicit authentication, right-click the /Citrix/MetaFrameXP/default virtual directory and view the Properties.
    For desktop credentials, pass-through users edit the /Citrix/MetaFrameXP/integrated virtual directory.
For Smart Card users, edit the /Citrix/MetaFrameXP/certificate virtual directory.
3. In the Application Settings section, click Configuration.
4. Select the App Options tab.
5. Ensure Enable session state is selected.
The default session time-out setting on IIS is 20 minutes but it can be increased to a maximum of 24 hours or 1440 minutes. See Microsoft article Q233477 for details about increasing the timeout in IIS.
NFuse Classic 1.71 and earlier
When Internet Explorer is configured never to check for newer versions of Web pages, NFuse Classic will not launch an application after a session is allowed to expire. To reproduce this problem:
    1. Configure your browser settings for 'Temporary Internet files and set Check for newer versions of stored pages to Never.
    2. Log on to Citrix NFuse Classic and view your published applications.
    3. Leave the system idle for 20 minutes or longer so that the Web server session expires.
    4. Click an application icon. The system returns you to the Logon page and a message appears stating that your session expired.
    5. Log on again and click the same application as before. You are again returned to the Logon page even though your session should not have expired this time.
Solutions
      • Clear the Temporary Internet Items folder
      • Upgrade to Web Interface 2.0 or later
      • Set Check for Newer versions of stored pages to Automatically
The default session time-out setting on IIS is 20 minutes but it can be increased to a maximum of 24 hours or 1440 minutes. See Microsoft article Q233477 for details about increasing the timeout in IIS.