Tuesday, 31 December 2013

Is viewstate of the control is maintained when its visible property is false in C# ASP.NET

Yes, When we set visible ='false' for any control, the control is not rendered into the form but its state is maintained with the help of viewstate.
If we set EnableViewState = 'false' for any control it means its state will not be maintained. 

For Example :-

aspx page
<form id="myForm" runat="server">
    <div>
 
        <asp:TextBox ID="txtFirstValue" runat="server">     </asp:TextBox>
        <asp:Button ID="btnClick"
            runat="server" Text="Button" onclick="btnClick_Click />
 
        <asp:TextBox ID="txtSecondValue" runat="server" Visible="false">1</asp:TextBox>
 
    </div>
    </form>


aspx.cs page
public partial class Index : System.Web.UI.Page
{
    int number;
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void btnClick_Click(object sender, EventArgs e)
    {
        txtFirstValue.Text = txtSecondValue.Text;
        number = Convert.ToInt32(txtSecondValue.Text);
        number++; //To increment by 1
        txtSecondValue.Text = number.ToString();
    }
}

Output of this program is : 1,2,3,4,5,6 etc. It means every time viewstate is maintained even visible property is false.
If we want that viewstate should not be maintained for a control we will do something like this

<asp:textbox id="txtSecondValue" runat="server" visible="false"  EnableViewState="false">1</asp:textbox>

Now every time output will be 1 because no viewstate was maintained.


That’s it!!…..Happy Programming...

How to Create a Facebook Like Chat Panel Interface in C# ASP.NET using CSS

Create a Panel Like this :-

  <asp:Panel runat="server" ID="pnlPopUp" CssClass="chatPanel">
        <div class="chatName flip">
            Chat Person Name
  <asp:ImageButton ID="imgbtnClose" runat="server" ImageUrl="~/images/LightBox/close.png" OnClick="imgbtnClose_Click" CssClass="closeChat" ToolTip="Close" />
        </div>
        <div class="slidetogle">
            <div class="messagehistory">
                Message History will show here.
            </div>
            <div class="newmessage">
                Enter your message here...
            </div>
        </div>
    </asp:Panel>


Css Classes For this Panel :-

.messagehistory {
            background-color: #F0F9FF;
            height: 230px;
            color: Gray;
            border-bottom: 1px solid gray;
            overflow: auto;
        }


        .newmessage {
            background-color: #F0F6FF;
            height: 40px;
            color: Gray;
        }


        .chatPanel {
            width: 280px;
            max-height: 300px;
            bottom: 0px;
            right: 35%;
            position: fixed;
            z-index: 20;
            border: 1px solid gray;
            display: inline-block;
        }


        .chatName {
            width: 100%;
            border: 1px solid gray;
            height: 30px;
            text-align: center;
            background-color: rgb(37,152,196);
            font-size: medium;
            color: White;
        }


        .closeChat {
            margin-left: 30%;
            height: 60%;
            padding-top: 2px;
        }




That’s it!!…..Happy Programming...

Sunday, 29 December 2013

How to know difference between value type parameter and reference type parameter in C#

Sometimes a user search with these kind of questions :-

what are the basics differences between value type and reference type in C#
reference type vs value type in C#
value type and reference type in C#
Solution :- Definition : Value types are those which hold both data and memory on the same location. Reference types are those which has a pointer which points to the memory location. Something like this...

using System;

namespace demo
{
    class Program
    {
        public static void valueType(int number)
        {
            number++; // or number=number+1
        }

        public static void referenceType(ref int number)
        {
            number++; // or number=number+1
        }

        static void Main(string[] args)
        {
            int number;
            Console.Write("Enter any number : ");
            number = Convert.ToInt32(Console.ReadLine()); // read user input number

            Console.WriteLine("\nValue Type");
            Console.Write("\nPrevious Value : {0}", number);
            Program.valueType(number); // call static method of Program class
            Console.Write("\nCurrent Value : {0}", number);
            Console.WriteLine("\nReference Type");
            Console.Write("\nPrevious Value : {0}", number);
            Program.referenceType(ref number); // call static method of Program class
            Console.Write("\nCurrent Value : {0}", number);
            Console.ReadLine();
        }
    }
}



That’s it!!…..Happy Programming...

Saturday, 28 December 2013

Multiple Logics to Swap Two Numbers in any Programming Language

This Question is sometimes used to see the analytic ability of a person and how dynamic thinking they have. The logic is same for all programming language, so if you have the logic right then you can answer it for any particular language such as PHP, JAVA, C++, C, C# etc. 

With using a third variable :-

Logic No.1 : temp=a; a=b; b=temp;


Without using a third variable :-

Logic No.2 : a=a+b; b=a-b; a=a-b;
Logic No.3 : a=a/b; b=a*b; a=(1/a)*b;
Logic No.4 : a=a*b; b=a/b; a=a/b;
Logic No.5 : a=(a+b)-(b=a);


That’s it!!…..Happy Programming...

Friday, 27 December 2013

How to check User Input Number is Even or Odd without using conditional statement C#


using System;

class Program
{
    public static void Main()
    {
        try
        {
            Console.WriteLine("Enter any number : ");
            int number = Convert.ToInt32(Console.ReadLine());

            int remainder = number % 2;
            int checkNumber = number / remainder;

// if remainder is not zero than this statement will execute other wise program control goes to catch block bcoz divide by zero exception would be occured
            Console.WriteLine("Number is Odd");
            Console.ReadKey();
        }

        catch (Exception ex)
        {
            Console.WriteLine("Number is even");
            Console.ReadKey();
        }
    }
}
To implement same concept in Java Click Here


That’s it!!…..Happy Programming...

How to pass values from one page to another page in C# ASP.NET?


You can use ViewState to maintain the state of controls during page postback.

For setting Viewstate :-
ViewState["myName"] = "xyz";

For retrieving Viewstate on postback:
string myName = ViewState["myName"].ToString();


You can use Cookies to save information on client side.

For setting Cookies :-

HttpCookie cName = new HttpCookie("Name");
cName.Value = txtName.Text;
Response.Cookies.Add(cName);
Response.Redirect("newPage.aspx");


You can use the Context to Transfer data to the one page to another.

For setting Context :-

Page1.aspx.cs
this.Context.Items["fName"] = "xyz";

Page2.aspx.cs
string fName = this.Context.Items["fName"].ToString();


You can use the Session Variable to preserve the common data which is required almost for every page or across the application for the specific user.

For setting Session:
Session["fName"] = "xyz";

For retrieving Session from any page :
string fName = Session["fName"].ToString();//default session timeout is 20 minutes
That’s it!!…..Happy Programming...