Monday, January 27, 2014

How to Set the ASP.NET Server Control Textbox value using JavaScript or Jquery

We can not access the server control by directly using it's id. You need to append and prepend the <%= and %> and also the ClientID. Because when rendering in browser the server control id will be change. If you are using JQuery to set the textbox value means you need to use # symbol. Check out the sample code clearly.
To get the server control value check my another post Click Here
HTML Code:
<asp:TextBox ID="textbox1" runat="server"></asp:TextBox>

JavaScript Code:
document.getElementById("<%= textbox1.ClientID %>").value = "values to set using JavaScript";

JQuery Code:
$("#<%= textbox1.ClientID %>").val("values to set using JQuery");

How to Get the ASP.NET Server Control Textbox value using JavaScript or Jquery

We can not access the server control value by directly using it's id. You need to append and prepend the <%= and %> and also the ClientID. Because when rendering in browser the server control id will be change. If you are using JQuery to get the textbox value means you need to use # symbol. Check out the sample code clearly.
To Set a value to server control check my another post Click Here
HTML Code:
<asp:TextBox ID="textbox1" runat="server"></asp:TextBox>

JavaScript Code:
var val1= document.getElementById("<%= textbox1.ClientID %>").value;

JQuery Code:
var val2= $("#<%= textbox1.ClientID %>").val();

Project Euler Solution using C#: Problem 7: 10001st Prime

Problem:

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10001st prime number?
My Solution:

static void Main(string[] args)
{
    bool isFound = false;
    bool isPrime = false;
    int limit = 10001;
    int count = 0;
    int val = 1;
    while (isFound == false)
    {
        if (val == 2)
        {
            count++;
        }
        else
        {
            for (int i = 2; i < val; i++)
            {
                if (val % i != 0 && val != i)
                {
                    isPrime = true;
                }
                else
                {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime)
            {
                count++;
                if (count == limit)
                {
                    Console.WriteLine("The" + limit + " th prime number is : " + val);
                }
            }
        }
        val++;
    }
    Console.ReadLine();
}
Note: You can simplifies the coding :)