Tuesday, 22 May 2012

Add ListItemCollection Property To Custom Gridview

   private ListItemCollection m_Items=null;

   [DesignerSerializationVisibility(DesignerSerializationVisibility.Content),  
    NotifyParentProperty(true), 
    PersistenceMode(PersistenceMode.InnerProperty)
     ] 
        public virtual ListItemCollection Items
        {
            get
            {
                if (this.m_Items == null)
                {
                    this.m_Items = new ListItemCollection ();
                   
                }
                 return this.m_Items;
            }
        }

Saturday, 12 May 2012

Embedded StyleSheet File with Custom Control


How to embedded StyleSheet file with custom control? Question has been opened many time on ASP.NET forum.
So in this article, I will try to demonstrate how to achieve this task.
In General
In general, if you want to embedded any thing with custom control, you need first to make its "Build Action" property to be "Embedded Resource", and then you need to add to the custom control assemblies using the <Assembly> attribute typically above class name, and finally you need to register in the page so it can be rendered.
I will illustrate all these things in details through the article.

Step One: make it as Embedded Resource
 After you added the StyleSheet file to your Custom Control Class Library project, first thing you need to do is to make build action property for the stylesheet file as embedded resource, otherwise your page will not be able to see the stylesheet file.
To do this, from the VS do a right click on the stylesheet file, and click on Properties,  and then change the "Build Action" property to be "Embedded Resource".

Step Two: Add it to the Custom Controls Assemblies
 Second thing you need to do is to register the Css file to the custom control assemblies by adding the below code typically above class name

[assembly: WebResource("YourProjectName.StyleSheetFileName.css""text/css")]
namespace YourNameSpace
{
   public class MyCustomControl : CompositeControl
    {
       //Custom Control Rest Code

 Step Three: Register Css File on the Page
Finally, we have to tell the Page that our custom control has Css file needed to be registered when the final HTML is generated for that page. So we need to write a bit of code in the OnInit method.

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
   
    string css = "<link href=\"" + Page.ClientScript.GetWebResourceUrl(this.GetType(),
    "YourProjectName.CssFileName.css") + "\" type=\"text/css\" rel=\"stylesheet\" />";
   
               
    Page.ClientScript.RegisterClientScriptBlock(this.GetType, "cssFile", css, false);
}

And that's it, you can now use the CSS StyleSheet file within your Custom Control.

Wednesday, 2 May 2012

See Text Of Stored Proceure -sp_helptext

 sp_HelpText is used  to see the text of the stored procedure


 sp_helptext StoredProcedurename

 Eg.
 sp_helptext sp_helptext

watermark for Password Text box css and javascript


<style type="text/css">
        .WaterMarkedTextBox
        {
            height: 16px;
            width: 168px;
            padding: 2px 2 2 2px;
            border: 1px solid #BEBEBE;
            background-color: #F0F8FF;
            color: gray;
            font-size: 8pt;
            text-align: center;
        }
        .WaterMarkedTextBoxPSW
        {
            background-position: center;
            height: 16px;
            width: 168px;
            padding: 2px 2 2 2px;
            border: 1px solid #BEBEBE;
            background-color: #F0F8FF;
            color: white;
            vertical-align: middle;
            text-align: right;
            background-image: url(Images/psw_wMark.png);
            background-repeat: no-repeat;
        }
        .NormalTextBox
        {
            height: 16px;
            width: 168px;
        }
    </style>



 <script language="javascript" type="text/javascript">
         function Focus(objname, waterMarkText) {
             obj = document.getElementById(objname);
             if (obj.value == waterMarkText) {
                 obj.value = "";
                 obj.className = "NormalTextBox";
                 if (obj.value == "User ID" || obj.value == "" || obj.value == null) {
                     obj.style.color = "black";
                 }
             }
         }
         function Blur(objname, waterMarkText) {
             obj = document.getElementById(objname);
             if (obj.value == "") {
                 obj.value = waterMarkText;
                 if (objname != "txtPwd") {
                     obj.className = "WaterMarkedTextBox";
                 }
                 else {
                     obj.className = "WaterMarkedTextBoxPSW";
                 }
             }
             else {
                 obj.className = "NormalTextBox";
             }

             if (obj.value == "User ID" || obj.value == "" || obj.value == null) {
                 obj.style.color = "gray";
             }
         }
    </script>


<body>
    <form id="form2" runat="server">
    <div>
        <h3>
            Watermark Textbox using JavaScript and CSS</h3>
    </div>
    <table>
        <tr>
            <td>
                User Id
            </td>
            <td>
                <asp:TextBox ID="txtUserId" runat="server"
              onfocus="Focus(this.id,'User ID')"
                    onblur="Blur(this.id,'User ID')"
                  Width="126px" CssClass="WaterMarkedTextBox">User ID</asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Password
            </td>
            <td>
                <asp:TextBox ID="txtPwd" TextMode="Password"
              runat="server" onfocus="Focus(this.id,'')"
                    onblur="Blur(this.id,'')" Width="126px"
                     CssClass="WaterMarkedTextBoxPSW" />
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td>
                <asp:Button ID="Button1" runat="server" Text="Submit" />
            </td>
        </tr>
    </table>
    </form>
</body>

 Image


 

Check User Stay time in a page using javascript


<script type="text/javascript">
    var time;
    window.onload = function() {
   // Initialize "time" variable to "0".
    time = 0;


    }
    window.setInterval(calcTime, 1000);
    function calcTime() {
      // calculate user-stay time
        time += 1;
    }
    // notify a handler the user-stay time as a query string before the page is unloaded.

    window.onbeforeunload = function() {
    var oRequest = new XMLHttpRequest();
        oRequest.open("get", "/handlers/HandlerTest.ashx?"+time, false);
        oRequest.send(null);
               

    }

Get Page Name in asp.net


  1. string sPagePath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
  2. System.IO.FileInfo oFileInfo = new System.IO.FileInfo(sPagePath);
  3. string sPageName = oInfo.Name;

Controls-collection-cannot-be-modified-because-the-control-contains-code-blocks-ie


Server Error in 'ASP.Net' Application.

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

Cause
While working with one if my applications where I was adding some controls to the head section of my page. Reason, I used the following JavaScript which had the <% %> tags to get the ClientID of a control

<head runat="server">
    <title>Untitled Page</title>
    <script type = "text/javascript">
        function GetValue()
        {
            var txt = document.getElementById("<%=TextBox1.ClientID%>");
            alert(txt.value);
        }
    </script>
</head>

As you can see above I have used ASP.Net Server tags to get the ClientID of textbox hence I cannot add controls dynamically to the head section

Solution
Remove the part which has server tags and place it somewhere else if you want to add dynamic controls from code behind
I removed my JavaScript from the head section of page and added it to the body of the page and got it working