This is default featured post 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured post 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured post 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured post 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured post 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Showing posts with label AJAX. Show all posts
Showing posts with label AJAX. Show all posts

Using jQuery - Add row to table

During this days some people ask me about creating dynamic data entry ui . I try to explain it with a real Example step by step .

First Draw an HTML Table with two rows , header and a row with text boxes for data Entry .


Remember that last textbox in the second row must have a lastElement css class.
The simplest way for add a new row to a table is .append() method , but in this approach you should know the new row structure .Using .clone() method make it more easy and flexible. Now the only thing to do is getting the last row of table and call .clone() method on it.

The clone method gets an optional Boolean parameter to clone selected DOM elements with all event handlers or not .

OK , you got cloned element and now you should insert it at the end of the table using .insertAfter() method.



put this line of code to a function and call it on press enter on last text box of each row.
you should handle keypress event of last input that has lastElement class.



The condition "e.which == 13" checks if pressed key is Enter or not.
Test your code ... yes it works , but a little problem , if you enter some data in text boxes clone() method copy text boxes with their values and its not good . select the new added row (now is a last row in table) and select all inputs and clear their values .


Another point is focus must be set to first text box of new row , do it ...



I try to keep this post simple , but if you know how to do ajax save , you can add a method to save last row before add new row in UI .

and here is the completed code ...

Check If User Name Exists Using AJAX And C#

You could make sure the user name entered during the registration process of your website is unique before the submit button is clicked using AJAX and C#. To accomplish this create two aspx pages. The first (default.aspx in this example) is the registration page, and it contains a TextBox control with an onkeyup element that calls the updateOutput JavaScript function and a span tag to display the HTML returned by the XMLHttpRequest.send function. The second page (doajaxstuff.aspx) should only contain following tags:

<@Page Language="C#" AutoEventWireup="true" CodeFile="doajaxstuff.aspx.cs" Inherits="VUsername_doajaxstuff" >


<asp:literal runat="server" id="literal1"/>



The html, title and body tags should be removed because the page contents will be added to the default.aspx page, which already has these tags. The C# code that checks if the user name exists is in the Page_Load event of doajaxstuff.aspx. It gets the user name from the query string and displays the result in a Literal control. To keep the example simple I returned "User name exists" or "OK", but this could be changed to return an img tag to show a red cross image or a green tick respectively.

view plaincopy to clipboardprint?

string[] usernames = { "admin", "administrator", "user1","user2",
"guest1", "guest2"};
protected void Page_Load(object sender, EventArgs e)
{
string newUsername = Request.QueryString["q"];

if(usernames.Contains(newUsername))
{
literal1.Text = "User name exists";
}
else
{
literal1.Text = "OK";
}
}

string[] usernames = { "admin", "administrator", "user1","user2",
"guest1", "guest2"};
protected void Page_Load(object sender, EventArgs e)
{
string newUsername = Request.QueryString["q"];

if(usernames.Contains(newUsername))
{
literal1.Text = "User name exists";
}
else
{
literal1.Text = "OK";
}
}

The JavaScript code below does the AJAX work. The updateOutput function checks first if the TextBox is empty, and exits if true. Then it creates an object called xmlHttp which is an XMLHttpRequest object or an ActiveXObject, depending on the user's browser. XMLHttpRequest is supported in browsers from Internet Explorer 6, Opera 7.60, Mozilla 1.0, Netscape 7, Safari 1.2 and Konqueror 3.2. If the user has Internet Explorer 5 an ActiveXObject will be created, which basically does the same job. If the xmlHttp object is created the variable url will be assigned a string containing the page to call plus the query string, which is the string entered by the user in usernameTextBox.

The onreadystatechange event handler is assigned the name of the function StateChanged. This event occurs five times: when the request is initialized, set up, sent, being processed and completed. You could check which stage the request is in using the readyState variable (0, 1, 2, 3 and 4 respectively.) The if statement checks for 4 which is set when the HTML is downloaded to xmlHttp.responseText. The if statement also makes sure that xmlHttp.status is 200, which means no error occurred. If both these conditions are true the contents of xmlHttp.responseText are displayed in the span tag.

The open method sets the parameters of the request and contains three parameters; the request method, the URL of the request and a Boolean parameter that specifies the request should be executed asynchronously or not. The first parameter could be "GET", "POST", "HEAD" or "PUT". The second parameter is the relative or full URL of the page to request. And the third parameter should be true so the page could be called asynchronously.

The send method executes the request. Because the open method's first parameter is "GET", send's parameter is null. If open's first parameter was "POST" it would be a string containing the request parameters.
 

Uploading Files using FileUpload control in Update Panel in ASP.Net AJAX

By default, FileUpload control will not work inside an UpdatePanel control for uploading files using Asynchronous postback. This is because, the file uploading and file manipulations is restricted by default in client side for security reasons. Hence it is not possible to upload files using asynchronous postback in UpdatePanel.

 

To upload files inside UpdatePanel control we need to rely upon a standard postback i.e. we need to set the button that is uploading the file to be PostBack trigger instead of AsyncPostBack trigger. This will initiate a normal postback whenever we click the upload button and it is possible to upload the file.

 

Refer the below code for clear understanding,

 

<asp:UpdatePanel ID="UpdatePanel1" runat="server">

            <ContentTemplate>              

                <asp:FileUpload ID="fuUpload" runat="server" />

                <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" />

            </ContentTemplate>

            <Triggers>

                <asp:PostBackTrigger ControlID="btnUpload" />               

            </Triggers>

        </asp:UpdatePanel>

 

 

 protected void btnUpload_Click(object sender, EventArgs e)

    {

        string filename = System.IO.Path.GetFileName(fuUpload.FileName);

        fuUpload.SaveAs("C:\temp" + filename);

    }

 

To simulate an AJAX file upload we can use iframes. In this approach, the page that is contained in the iframe will contain the FileUpload control and it will be posted with a normal postback to the server and hence provides a feeling like AJAX request. We will see about this in future code snippets.

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites