Wednesday, March 28, 2012

Converting to XML Format Variable

Suppose there exists a lot of Checkboxes, for saving all we can convert values to an XML format and pass to DB

<div id="divTest">
<table>
<tr>
<td>
<input type="checkbox" value="1"/>One
</td>
<td>
<input type="checkbox" value="2"/>Two
</td>
</tr>
<tr>
...................
...................
</tr>

</table>
</div>

--------------------------------

<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js" type="text/javascript"></script>

<script type="text/javascript" language="javascript">
$(document).ready(function () {

$("btnSave").click(function(){
                 var TestXML = "<STHome>";
                $("#divTest").find("input:checkbox").each(function () {
                    if ($(this).attr("checked")) {
                        TestXML = TestXML + "<Home>";
                        TestXML = TestXML + "<ID>" + $(this).val() + "</ID>";                                               
                        TestXML = TestXML + "<TestValue>" + "" + "</TestValue>";                  
                        TestXML = TestXML + "</Home>";
                    }
                });
                TestXML = STHomeXML + "</STHome>";
        $.ajax({
                     type: "POST",
                      url: "TestPage.aspx/InsertDetails",
                      dataType: "json",
                      data: "{'TestID':'" + TestID + "','TestXML':'" + TestXML + "'}",
                      contentType: "application/json; charset=utf-8",
                      success: function (result) {
                alert("save successfully");
            }
        });
  });//Button Click end

});//document.ready end
</script>


------------

and pass this "TestXML" variable to aspx.cs
Go to sareeshmg.blogspot.com
post name : XML Insert

No comments:

Post a Comment