May 27, 2010

How to check for session timeout (asp.net, c#)

For checking Session Time Out use following code:
if (Context.Session != null)

Page.Previos is null error (ASP.NET, C#)

Use Page.Previous with Server.Transfer for perfect results. It works perfectly with “Transfer of the page”

With response.redirect it is not working properly – sometimes returning null value.

Could not load type error in asp.net 2.0

Change “CodeBehind” file to “CodeFile” in aspx.cs file

How to put value in asp.net textbox placed in content page using JavaScript? (ASP.NET, C#, JavaScript)

When using master page and content page in asp.net, original ids of the controls (used in aspx page) are changed during execution of the page, because the whole page will be served as HTML page to the viewer. The ids are prefixed with ContentPlaceHolder ID and some other information.

So what we can do is, we can match the original ids of the control, within this new ids generated by asp.net server to find that control.

Once we get the control, we can do our normal java scripting.

Example : A search page opens a new window upon clicking on "search" button. This new window passes some value to search page's textbox and closes itself upon clicking on its "Click" button. Both pages uses a master page.

For example I have a master page and 2 content pages(page1.aspx, page2.aspx).

Page1.aspx have following code:

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script type="text/javascript">
function OpenPopup()
{
    window.open("Default2.aspx","List","scrollbars=yes,resizable=yes,width=550,height=480");
    return false;
}
</script>

<asp:TextBox ID="tbx_Name" runat="server"></asp:TextBox>
<asp:Button ID="bt_search" runat="server" Text="search" />

</asp:Content>

Page1.aspx.cs page have following code:

protected void Page_Load(object sender, EventArgs e)
{
     bt_search.Attributes.Add("onclick", "javascript:return OpenPopup()");
}

Page2.aspx have following code:

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script language="javascript">
function GetUserValue(val)
{
       frm=window.opener.document.forms[0];
       for (var i=0;i<frm.length;i++)
      {
            str=frm.elements[i].id;
            if (str.search("tbx_Name")>=0)
               { frm.elements[i].value="testing...";
               }
        }
      window.close();
}
</script>

<asp:Button ID="bt_UserName" runat="server" Text="Click" OnClick="bt_Click" />
<asp:Button ID="bt_Close" runat="server" Text="Close" />

</asp:Content>

Page2.aspx.cs page have following code:

protected void Page_Load(object sender, EventArgs e)
{
      bt_UserName.Attributes.Add("onclick", "javascript:GetUserValue('" + "Tim Johnson" + "')")
}

protected void bt_Click(object sender, EventArgs e)
{
      if (!Page.IsClientScriptBlockRegistered("ClosePopUp")) 
      {
              RegisterStartupScript("ClosePopUp", "<script language=\"javascript\">function CloseMe()      {window.close();window.history.back(); };</script>");
      }

      bt_Close.Attributes.Add("onclick", "ClosePopUp()");
}

Connection String - Adding connection string to Web.config file and accessin it (ASP.NET, C#)

Web.config File:

We can add conncetion string in web.config file for security reasons, in the following way:

<configuration>

<appSettings>

<add key="ConnectString" value="Server=servername;Database=databasename;uid=yourUserId;pwd=yourPassword"/>

Default.aspx.cs page:

Getting Connection String from web.config in c# program )(For example, Default.aspx.cs page_load event)

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectString Name"].ConnectionString);

May 20, 2010

How to display an error message of a vaildation control all the time?

To display an error message of validation control all the time, set that error message in the "Text" property of validation control.