Dynamic DataTable in ASP.NET

Friday, April 5, 2013

private void SetInitialRow()
{
    DataTable dt = new DataTable();
    DataRow dr = null;
    dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
    dt.Columns.Add(new DataColumn("Column1", typeof(string)));
    dt.Columns.Add(new DataColumn("Column2", typeof(string)));
    dt.Columns.Add(new DataColumn("Column3", typeof(string)));
    dr = dt.NewRow();
    dr["RowNumber"] = 1;
    dr["Column1"] = string.Empty;
    dr["Column2"] = string.Empty;
    dr["Column3"] = string.Empty;
    dt.Rows.Add(dr);
    
    //Store the DataTable in ViewState
    ViewState["CurrentTable"] = dt;

    Gridview1.DataSource = dt;
    Gridview1.DataBind();
}

Serialization ~ Short Description

Saturday, March 16, 2013
Serialization is a process by which we can save the state of the object by converting the object in to stream of bytes.These bytes can then be stored in database, files, memory etc.
Below is a simple code of serializing the object.
MyObject objObject = new MyObject();
objObject.Value = 100;       
      
// Serialization using SoapFormatter

SoapFormatter formatter = new SoapFormatter();

Stream objFileStream = new FileStream("c:\\MyFile.xml", FileMode.Create, FileAccess.Write, FileShare.None);

formatter.Serialize(objFileStream, objObject);

objFileStream.Close();

 Below is simple code which shows how to deserialize an object. 
//De-Serialization

Stream objNewFileStream = new FileStream("c:\\MyFile.xml", FileMode.Open, FileAccess.Read, FileShare.Read);

MyObject objObject =(MyObject)formatter.Deserialize(objNewFileStream);

objNewFileStream.Close(); 

Get All checked-Checkbox whose ID ~ start with "email_"

Friday, March 15, 2013
get all checked CheckBox whose id start with "email_"

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script>
    $(document).ready(function () {
        $("button").click(function () {
           $('input[id^="email_"]:checked').each(function () {    
               alert($(this).attr('id'));
           });
        });
    });
</script>


----- This should be in header portion of a page And As you see this is done by the Jquery :)

Simple AJAX Application ~ Codding


<script>
var xmlhttp;
function loadXMLDoc(url,cfunc)
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=cfunc;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
function myFunction()
{
loadXMLDoc("ajax_info.txt",function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  });
}
</script>


----  myDiv is div id under the page.