3 April 2014

Setting focus to textbox control using JavaScript

   <script language="javascript">
        document.getElementById("TextboxControlName").focus();
    </script>

What is the Role of IIS ?



Visual studio having It own ASP.NET Engine which is capable enough to run Asp.net web application from visual studio. So we just click on Run button to start the application.
Now this is the scenarios of local environment. But If we want to host it on server from where all user can access the sites then IIS comes into the picture.

IIS provides a redesigned WWW architecture that can help you achieve better performance, reliability, scalability, and security for our Web sites. IIS can support following Protocol HTTP/HTTPS, FTP, FTPS, SMTP Etc. We need to host the site on IIS, when request comes from client it first hits the IIS Server, then the server passed it to ASP.NET worker process to execute. Then the response also passes to client via IIS itself.
Note only Hosting of Site we can create our FTP Server, SMTP Server using IIS itself.
There are different version of IIS available like 5.1, 6.0, 7.0 etc.

get value from dropdown (select) control in java script



alert(document.getElementById('dropdown1').value);

get value from a textbox in java script

 alert(document.getElementById('txtbox1').value);

RowDatabound event tips and tricks in Gridview control

Introduction


Gridview control is most common control in all asp.net applications to display the data and it’s very powerful control and lot of built in features. In this article, i will explore some tips and tricks in rowdatabound event in gridvew control, Actually RowDatabound event will occur when data row is bound to the control in the gridview control.

Find control in RowDataBound event


Using this event we can able to find the specific controls such as textbox, dropdown, checkbox and get values from control which is used inside gridview control.
protected void GVSample_RowDataBound(object sender, GridViewRowEventArgs e)
        {   //Get data row view
            DataRowView drview = e.Row.DataItem as DataRowView;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //Find dropdown control & get values
                DropDownList dpEmpdept = (DropDownList)e.Row.FindControl("DrpDwnEmpDept");
                string value = dpEmpdept.SelectedItem.Value;
                //Find textbox control
                TextBox txtname = (TextBox)e.Row.FindControl("txtName");
                string Name = txtname.Text;
                //Find checkbox and checked/Unchecked based on values
                CheckBox chkb = (CheckBox)e.Row.FindControl("ChckBxActive");
                if (drview[5].ToString() == "True")
                { 
                    chkb.Checked = true; 
                }
                else 
                { 
                    chkb.Checked = false; 
                }
            }
        }

 

Format specific row


We can able to do formatting a specific row in a gridview based on some conditions, say for example if price would be more then 100 then display, those rows with background as brown, fore color as black and bold letters.
 protected void GVSample_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblPrice = (Label)e.Row.FindControl("lblPrice");
                if (Convert.ToInt32(lblPrice.Text) > 100)
                {
                    e.Row.ForeColor = System.Drawing.Color.Black;
                    e.Row.Font.Bold = true;
                    e.Row.BackColor = System.Drawing.Color.Brown;  
                }
            }
        }

 

Gridview row color change

If you want to change the gridview row color change on mouse over and mouse out then write the below code in rowdatabound event.
protected void GVSample_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "self.MouseOverOldColor=this.style.backgroundColor;this.style.backgroundColor='#C0C0C0'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=self.MouseOverOldColor");
            }
        }

 

Bind dropdown list in footer

Sometime we use footer row to add/insert records, so in this case if you use dropdown list in footer row to add new record then we need to load the data in dropdown list when bind the data with gridview control and see the below code how can we load dropdown list in footer row while rowdatabound event occurs.
protected void GVSample_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                DropDownList dp = (DropDownList)e.Row.FindControl("DrpDwnAddEmpDept");
                dp.DataSource = GetEmpDept();
                dp.DataTextField = "DepName";
                dp.DataValueField = "DepName";
                dp.DataBind();
            }
        }
        private DataTable GetEmpDept()
        {
            //Get Employee department 
            DataTable dt = new DataTable();
            dt.Columns.Add("DepName");
            DataRow rw1 = dt.NewRow();
            rw1[0] = "IT";
            dt.Rows.Add(rw1);
            DataRow rw2 = dt.NewRow();
            rw2[0] = "Finance";
            dt.Rows.Add(rw2);
            DataRow rw3 = dt.NewRow();
            rw3[0] = "Security";
            dt.Rows.Add(rw3);
            return dt;
        }

 

HyperLink and redirect


In general we use hyperlink to redirect other page or open new page to view specific details based on some conditions, below example redirecting to other page if user is eligible to view.
<asp:GridView ID="GVSample" runat="server" AutoGenerateColumns="false" OnRowDataBound="GVSample_RowDataBound">
            <Columns>
                <asp:TemplateField HeaderText="Userprofile">
                    <ItemTemplate>
                        <asp:HyperLink ID="hyprlnkdetails" runat="server" />
                        <asp:Label ID="lblText" runat="server" Text=""></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
protected void GVSample_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            DataRowView drview = e.Row.DataItem as DataRowView;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                HyperLink HLdetails = e.Row.FindControl("hyprlnkdetails") as HyperLink;
                Label lblText       = e.Row.FindControl("lblText") as Label;
                if (drview[5].ToString()=="True")
                {
                    HLdetails.Text          = "Click here to view";
                    HLdetails.NavigateUrl   = String.Format("~/details.aspx?UserID={0}", drview[8].ToString());
                }
                else
                {
                    HLdetails.Visible   = false;
                    lblText.Text        = "View Restricted";
                }
            }
        }

 

Get DataKeyValues in RowDatabound

Generally we set the primary key values to the datakeyName property in gridview control and below code will help to get that date key values associated with a row in rowdatabound event.
protected void GVSample_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                    string userID = GVSample.DataKeys[e.Row.RowIndex].Values[0].ToString();
            }
        }
Above list of tips on rowdatabound event in gridview control will help you when work with this event, thanks for reading this article and provide your suggestions and feedback about this article.
 

Disadvantage of Ajax



  1. Back functionality cannot work because the dynamic pages don’t register themselves to the browsers history engine. Hence the need of Iframe becomes essential.
  2.  The page cannot be bookmarked if implemented using Ajax.
  3.  If java script is disabled , Ajax will not work
  4.  Because different components of the pages are loaded at different times, response time may be slow.
  5.  Because different components of the pages are loaded at different times it may create confusion for the user.

Dispose in C#

Dispose() method releases the memory from the variable,
we define the dispose when the variable is in not use,
this variable can be reused again.

Example:

func()
{
sqlcommand cmd=new sqlcommand()
datareader dr;

cmd=new sqlcommand("select* from emp",con)
dr=cmd.executreader;
cmd.dispose();
dr.close();

cmd=new sqlcommand("select* from dept",con)
dr=cmd.executreader;
cmd.dispose();
dr.close();

cmd=new sqlcommand("select* from salgrade",con)
dr=cmd.executreader;
cmd.dispose();
dr.close();

}
cmd.executereader();
cmd.dispose();

What are the differences between scripting languages and programming languages?

The primary difference between a "programming language" (C, C++, VB etc.) and a "scripting language" (PHP, ASP, JSP, JavaScript, VBScript)is that code written in a programming language needs to be compiled before it is run. Once it is compiled, it can be run any number of times. 

Scripting languages, on the other hand, are interpreted at run-time. This means that every time you want to run the program, a separate program needs to read the code, interpret it, and then follow the instructions in the code. Compiled code has already been interpreted into machine language, so it is will typically execute faster because the conversion into machine language has already been done. 

Markup languages (HTML, XML) are somewhat different from both of the others. A markup language is simply a set of tags that are used to "mark up" text documents so that sections of text can be logically arranged and labeled. These documents can be viewed as plain text, or, more commonly, are viewed through a browser. The browser parses the document, looking for markup tags, and it then arranges the text and/or formats it according to the values in the tags.