Translate

Friday, 16 November 2012

Alert Message from CodeBehind

1. How to Redirect a page after clicking OK of javascript alert box.

Way 1:

C# code behind:
string strScript = "<script>";
strScript += "alert('Type your message here');";
strScript += "window.location='TestPage.aspx';";
strScript += "</script>";
this.ClientScript.RegisterStartupScript(this.GetType(), "Startup", strScript);

Way 2:

 Response.Write("<script>alert('Successfully changed')</script>");
 Response.Write("<script>window.location.href='Default.aspx';</script>");

Way 3:

 ScriptManager.RegisterStartupScript(page, page.GetType(), "err_msg", "alert('" + error + "'); window.location='newpage.aspx';", true);

2. Alert message from Codebehind:

Way 1:


   ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Success');", true);


Way 2:

  Page.ClientScript.RegisterStartupScript(this.GetType(), "key", "alert('No Records Available');", true);


Way 3:
If we have used Update Panel, Use following code.

 ScriptManager.RegisterStartupScript(this.Page, typeof(UpdatePanel), "key", "alert('Success');", true);

2. Confirmation Message:


<script type="text/javascript">
        function Confirm(id, des) {
            // debugger;
            try {
                if (document.getElementById(id).value != "") {
                    var r = confirm("This is new Code.Do you want add this code?");
                    if (r == true) {
                        document.getElementById(des).value = "";
                        document.getElementById(des).focus();
                        return true;

                    }
                    else {
                        document.getElementById(id).value = "";
                        document.getElementById(id).focus();
                        return true;
                    }
                    return false;
                }
            }
            catch (e) {
            }
        }
     
    </script>

Thursday, 15 November 2012

Java Script Function

1. how to display block or none for control in javascript

<tr id="trBrowse" runat="server" style="display: none;">
      <td></td>
</tr>

trBrowse.Style.Add("display", "block");

How to get open folders list using C#?


To Open a particular folder in Explorer, just:
System.Diagnostics.Process.Start(@"explorer.exe", @"C:\Windows");
In fact, as Explorer is the asscoaited handler for folder, that could be written as:
System.Diagnostics.Process.Start(@"C:\Windows");
or
 string path="D:\\Test";
 System.Diagnostics.Process.Start("explorer.exe", path);


CheckForDuplicates confirmation message

protected void SaveButton_Click(object sender, EventArgs e)
{
    try
    {
        if (CheckForDuplicates())
        {
            //proceed normally
        }
    }
}

private bool CheckForDuplicates()
{
    //check database

            if (/*there are duplicates*/)
            {
                string message = "A duplicate name exists. Would you like to continue?";
                string scriptString = "<script language='javascript' 
                    type='text/javascript'>" + "return confirm('" + message + "');</script>";

                ScriptManager.RegisterStartupScript(this, this.GetType(), 
                    "script", scriptString, false);

                //here i would like to return their confirmation
            }
        }
    }
    return true;
}

Find Extented Ascii Value using javascript


<script type="text/javascript">
function CheckAll() {
var Titleid = document.getElementById('ID');
            var tit = Titleid.value;
            var T = [];
            for (var i = 0, j = Titleid.value.length; j--; i++) {
                T[i] = tit.charCodeAt(i);
                if (T[i] > 128) {
                    alert("Ascii found: " + String.fromCharCode(T[i]));
                    document.getElementById('ID').focus();
                    return false;
                }
            }

  return true;
        }
 </script>


Thursday, 4 October 2012

Web Config Helps


1. Connection String:


<connectionStrings>
    <add name="constr"  connectionString="Initial catalog=DB_Name;uid=sa;pwd=Password;Data Source=Server_IP" providerName="System.Data.SqlClient"/>
  </connectionStrings>


2.  Error - Operation is not valid due to the current state of the object

Whenever a postback is done, this error occurs when form fields are very large (text) in number. The stack trace is:
at System.Web.HttpValueCollection.ThrowIfMaxHttpCollectionKeysExceeded() at System.Web.HttpValueCollection.FillFromEncodedBytes(Byte[] bytes, Encoding encoding) at System.Web.HttpRequest.FillInFormCollection()
By default, the maximum value of MaxHttpCollection is 1000.
To solve this error, increase the MaxHttpCollection value. Try adding the following setting in yourweb.config's <appsettings> block.
  <configuration>
      <appSettings>
    <add key="aspnet:MaxHttpCollectionKeys" value="9999" />
  </appSettings>
</configuration>
It can solve your problem. If you have very large records on your page, say 600 records in a grid, and you click on Submit, then increase the value of MaxHttpCollection.

2. EnCrypt and Decrypt of Connection String in webconfig:

Encrypt connectionStrings in web.config of IIS based site
 
http://www.codeproject.com/images/minus.gif Collapse | Copy Code
aspnet_regiis.exe -pe "connectionStrings" -app "/SampleWebSite"
Here –pe indicates that the application is built as IIS based site. Second argument connectionStrings is the name ofconfiguration section needs to be encrypted. The Third argument -app indicates virtual directory and last argument is the name of virtual directory where application is deployed.
 
Decrypt connectionStrings in
 web.config of IIS based site
 
http://www.codeproject.com/images/minus.gif Collapse | Copy Code
aspnet_regiis.exe -pd "connectionStrings" -app "/SampleWebSite"
Till now we learned how to encrypt and decrypt connectionStrings section in web.config file using aspnet_regiis.exe command line tool now I will explain code behind method to encrypt and decrypt the connection string section inweb.config.

3. How to: Set the Culture and UI Culture for ASP.NET Web Page Globalization
Or

CurrentCulture incorrectly defaulting to en-US in ASP.net


<globalization uiCulture="en" culture="en-US" />

4. How to increase Session Timeout:

<httpRuntime executionTimeout="999999" maxRequestLength="2097151"/>

5. How to Set: Dundas chart Temporary location, Default Excel version , Date and Time in Web config.

<appSettings>
    <add key="CommandTimeout" value="60000"/>
    <add key="DefaultDate" value="12/30/1899"/>
    <add key="DefaultTime" value="12/30/1899"/>
    <add key="OLEDBConnection2" value="Microsoft.Jet.OLEDB.4.0"/>
    <add key="OLEDBConnection" value="Microsoft.ACE.OLEDB.12.0"/>
    <add key="ExcelVersion" value="Excel 12.0"/>
    <add key="ChartHttpHandler" value="Storage=memory;Timeout=180;Url=~/temp/;"/>
</appSettings>