Introduction:
This article will explain how to update datagrid values to database using AJAX without a page refresh.
Main:
Inserting Data to databases through front-end is a common task for all Web Developers.We Can do that without Page refresh through AJAX with the following simple steps.
1.Forming Custom XML using front end Data’s.
2.Posting the XML to ours Webapge.
3.Reading the Incoming XML using RequestStream()
4.Splitting the XML data and updating into database.
In aspx page,
<asp:datagrid id=SampleGrid runat=server AutoGenerateColumns=false>
<Columns>
<asp:TemplateColumn>
<HeaderStyle width=10%></HeaderStyle>
<ItemStyle width=10%></ItemStyle>
<HeaderTemplate> StudentID </HeaderTemplate>
<ItemTemplate>
<span id="spnStudentID" style="Hand:Cursor;width:10;">
<%#DataBinder.Eval(Container,"DataItem.StudentID") %> </span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderStyle width=10%></HeaderStyle>
<ItemStyle width=10%></ItemStyle>
<HeaderTemplate> Name </HeaderTemplate>
<ItemTemplate>
<span id="spnName" style="Hand:Cursor;width:10;">
<%#DataBinder.Eval(Container,"DataItem.Name") %> </span>
<asp:TextBox id=txtName runat=server style="Display:none">
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderStyle width=10%></HeaderStyle>
<ItemStyle width=10%></ItemStyle>
<HeaderTemplate> Address </HeaderTemplate>
<ItemTemplate>
<span id="spnAddress" style="Hand:Cursor;width:10;">
<%#DataBinder.Eval(Container,"DataItem.Address") %> </span>
<asp:TextBox id=txtAddress runat=server style="Display:none">
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderStyle width=10%></HeaderStyle>
<ItemStyle width=10%></ItemStyle>
<HeaderTemplate> City </HeaderTemplate>
<ItemTemplate>
<span id="spnCity" style="Hand:Cursor;width:10;">
<%#DataBinder.Eval(Container,"DataItem.City") %> </span>
<asp:TextBox id=txtCity runat=server style="Display:none">
<asp:button id=btnupdate runat=server style="Display:block" onclick="UpdateRow(this);">
<asp:button id=btnSave runat=server style="Display:none" onclick="CallAjax(this);">
</ItemTemplate>
</asp:TemplateColumn>
<asp:datagrid id=SampleGrid runat=server AutoGenerateColumns=false> <Columns> <asp:TemplateColumn> <HeaderStyle width=10%></HeaderStyle> <ItemStyle width=10%></ItemStyle> <HeaderTemplate> StudentID </HeaderTemplate> <ItemTemplate> <span id="spnStudentID" style="Hand:Cursor;width:10;"> <%#DataBinder.Eval(Container,"DataItem.StudentID") %> </span> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn> <HeaderStyle width=10%></HeaderStyle> <ItemStyle width=10%></ItemStyle> <HeaderTemplate> Name </HeaderTemplate> <ItemTemplate> <span id="spnName" style="Hand:Cursor;width:10;"> <%#DataBinder.Eval(Container,"DataItem.Name") %> </span> <asp:TextBox id=txtName runat=server style="Display:none"> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn> <HeaderStyle width=10%></HeaderStyle> <ItemStyle width=10%></ItemStyle> <HeaderTemplate> Address </HeaderTemplate> <ItemTemplate> <span id="spnAddress" style="Hand:Cursor;width:10;"> <%#DataBinder.Eval(Container,"DataItem.Address") %> </span> <asp:TextBox id=txtAddress runat=server style="Display:none"> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn> <HeaderStyle width=10%></HeaderStyle> <ItemStyle width=10%></ItemStyle> <HeaderTemplate> City </HeaderTemplate> <ItemTemplate> <span id="spnCity" style="Hand:Cursor;width:10;"> <%#DataBinder.Eval(Container,"DataItem.City") %> </span> <asp:TextBox id=txtCity runat=server style="Display:none"> <asp:button id=btnupdate runat=server style="Display:block" onclick="UpdateRow(this);"> <asp:button id=btnSave runat=server style="Display:none" onclick="CallAjax(this);"> </ItemTemplate> </asp:TemplateColumn> |
In Javascript,
function UpdateRow(objbtn)
{
var row=objbtn.parentElement.
var lblname = row.cells[1].cildNodes[0];
var txtname = row.cells[1].cildNodes[1];
var lblAddress = row.cells[2].cildNodes[0];
var txtAddress = row.cells[2].cildNodes[1];
var lblCity = row.cells[3].cildNodes[0];
var txtCity = row.cells[3].cildNodes[1];
var btnSave = row.cells[3].cildNodes[3];
lblname.Style.display='none';
lblAddress.Style.display='none';
lblCity.Style.display = 'none';
txtname.Style.display='block';
txtAddress.Style.display='block';
txtCity.Style.display='block';
btnSave.Style.display='block';
objbtn.Style.display='none';
}
function CallAjax(this)
{
var row=objbtn.parentElement.
var lblStudentID = row.cells[0].cildNodes[0];
var lblname = row.cells[1].cildNodes[0];
var txtname = row.cells[1].cildNodes[1];
var lblAddress = row.cells[2].cildNodes[0];
var txtAddress = row.cells[2].cildNodes[1];
var lblCity = row.cells[3].cildNodes[0];
var txtCity = row.cells[3].cildNodes[1];
var btnUpdate = row.cells[3].cildNodes[2];
PassValues(lblStudentID.innerText,txtname.value,txtAddress.value,txtCity.value);
lblname.Style.display='block';
lblAddress.Style.display='block';
lblCity.Style.display = 'block';
txtname.Style.display='none';
txtAddress.Style.display='none';
txtCity.Style.display='none';
btnUpdate.Style.display='block';
objbtn.Style.display='none';
}
function PassValues(ID,name,Address,City)
{
var xml = '<?xml version="1.0"?><Body><Details><ID> '+ ID +' </ID><name>'+ name +' </name><Address> '+ Address +'</Address><City>'+ City +'</City></Details></Body>';
var XMLHttpRequestObject = false;
if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject) {
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP")
}
XMLHttpRequestObject.open('POST",sampleUpdate.aspx);
var response=XMLHttpRequestObject.send( xml);
if(response.childNodes[0] != null)
{
alert(response);
}
}
function UpdateRow(objbtn) { var row=objbtn.parentElement. var lblname = row.cells[1].cildNodes[0]; var txtname = row.cells[1].cildNodes[1]; var lblAddress = row.cells[2].cildNodes[0]; var txtAddress = row.cells[2].cildNodes[1]; var lblCity = row.cells[3].cildNodes[0]; var txtCity = row.cells[3].cildNodes[1]; var btnSave = row.cells[3].cildNodes[3]; lblname.Style.display='none'; lblAddress.Style.display='none'; lblCity.Style.display = 'none'; txtname.Style.display='block'; txtAddress.Style.display='block'; txtCity.Style.display='block'; btnSave.Style.display='block'; objbtn.Style.display='none'; } function CallAjax(this) { var row=objbtn.parentElement. var lblStudentID = row.cells[0].cildNodes[0]; var lblname = row.cells[1].cildNodes[0]; var txtname = row.cells[1].cildNodes[1]; var lblAddress = row.cells[2].cildNodes[0]; var txtAddress = row.cells[2].cildNodes[1]; var lblCity = row.cells[3].cildNodes[0]; var txtCity = row.cells[3].cildNodes[1]; var btnUpdate = row.cells[3].cildNodes[2]; PassValues(lblStudentID.innerText,txtname.value,txtAddress.value,txtCity.value); lblname.Style.display='block'; lblAddress.Style.display='block'; lblCity.Style.display = 'block'; txtname.Style.display='none'; txtAddress.Style.display='none'; txtCity.Style.display='none'; btnUpdate.Style.display='block'; objbtn.Style.display='none'; } function PassValues(ID,name,Address,City) { var xml = '<?xml version="1.0"?><Body><Details><ID> '+ ID +' </ID><name>'+ name +' </name><Address> '+ Address +'</Address><City>'+ City +'</City></Details></Body>'; var XMLHttpRequestObject = false; if (window.XMLHttpRequest) { XMLHttpRequestObject = new XMLHttpRequest(); } else if (window.ActiveXObject) { XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP") } XMLHttpRequestObject.open('POST",sampleUpdate.aspx); var response=XMLHttpRequestObject.send( xml); if(response.childNodes[0] != null) { alert(response); } } |
In CodeBehind,
if(Request.InputStream != null)
{
XmlDocument doc = new XmlDocument
doc.Load(Request.InputStream)
string ID = doc.GetElementsByTagName("ID").InnerText;
string name = doc.GetElementsByTagName("name").InnerText;
string Addess = doc.GetElementsByTagName("Addess").InnerText;
string City = doc.GetElementsByTagName("City").InnerText;
str sql = "update Student_table set name = '" + name +"',Address = '" + Address +"' ,City ='" + City +"' where StudentsID ='" + ID "";
try
{
using (SqlConnection connection = new SqlConnection(
connectionString))
{
SqlCommand command = new SqlCommand(sql, connection);
command.Connection.Open();
command.ExecuteNonQuery();
}
}
catch (param ex)
{
Response.Write (ex.ToString();
}
if(Request.InputStream != null) { XmlDocument doc = new XmlDocument doc.Load(Request.InputStream) string ID = doc.GetElementsByTagName("ID").InnerText; string name = doc.GetElementsByTagName("name").InnerText; string Addess = doc.GetElementsByTagName("Addess").InnerText; string City = doc.GetElementsByTagName("City").InnerText; str sql = "update Student_table set name = '" + name +"',Address = '" + Address +"' ,City ='" + City +"' where StudentsID ='" + ID ""; try { using (SqlConnection connection = new SqlConnection( connectionString)) { SqlCommand command = new SqlCommand(sql, connection); command.Connection.Open(); command.ExecuteNonQuery(); } } catch (param ex) { Response.Write (ex.ToString(); } |
Conclusion:
Hope this helps,
Happy Coding.
I cannot pass my webpage,i think i need to find current session id and need to add before the webpage,anybody help.
Very Nice Article,and very useful
Its converted my app into a rich user experience.Thanks a lot.
This is a very exciting post, I was looking for this knowledge. Just so you know I located your web site when I was doing research for blogs like mine, so please check out my site sometime and leave me a comment to let me know what you think.
I really liked your blog! super
I don’t agree with everything in this piece, but you do make some very good points. Im very interested in this matter and I myself do alot of research as well. Either way it was a well thoughtout and nice read so I figured I would leave you a comment. Feel free to check out my website sometime and let me know what you think.
Exceptional article, this is very similar to a site that I have. Please check it out sometime and feel free to leave me a comenet on it and tell me what you think. Im always looking for feedback.
This is a superb write-up, I found your site looking around yahoo for a related theme and arrived to this. I couldnt come across to much different material on this piece, so it was great to discover this one. I likely will end up being returning to check out some other posts that you have another time.
This is a wonderful posting, I located your blog page doing research yahoo for a similar content and arrived to this. I couldnt come across to much additional information and facts on this posting, so it was wonderful to discover this one. I will probably end up being returning to check out some other articles that you have another time.
This is a good summary, I was wondering if I could use this piece of writing on my website, I will link it back to your website though. If this is a problem please let me know and I will take it down right away.
Great piece of writing, this is very similar to a site that I have. Please check it out sometime and feel free to leave me a comenet on it and tell me what you think. I’m always looking for feedback.
This is a good post, I was wondering if I could use this piece of content on my website, I will link it back to your website though. If this is a problem please let me know and I will take it down right away.
I have read a few of the articles on your website now, and I really like your style of blogging. I added it to my favorites site list and will be checking back soon. Please check out my site as well and let me know what you think.
Very informative post. Thanks for taking the time to share your view with us.
I’ve been visiting your blog for a while now and I always find a gem in your new posts. Thanks for sharing.
This is a really good piece of content, I found your webpage looking around aol for a similar subject and came to this. I couldnt find to much other material on this post, so it was nice to find this one. I will likely end up being back again to look at some other articles that you have another time.
thanks!You made some good points there. I did a search on the topic and found most people will agree with your blog
Nice blog you have, the articles here are very helpful. Subscribed to RSS feed. Thanks!
This is a very exciting post, I was looking for this info. Just so you know I located your blog when I was browsing for blogs like mine, so please check out my site sometime and leave me a comment to let me know what you think.
Nice site and great text.
This is a superb post, but I was wondering how do I suscribe to the RSS feed?
Super text, I will add this blog to my favorites.
I thought it was going to be some boring old site, but I’m glad I visited. I will post a link to this site on my blog. I am sure my visitors will find that very useful.
Apple now has Rhapsody as an app, which is a great start, but it is currently hampered by the inability to store locally on your iPod, and has a dismal 64kbps bit rate. If this changes, then it will somewhat negate this advantage for the Zune, but the 10 songs per month will still be a big plus in Zune Pass’ favor.