AJAX enabled ASP.NET components for rapid web application development
|
||||||||||||||||
|
||||||||||||||||
<link rel="stylesheet" type="text/css" href="dbnetsuite.css.ashx" /> <script language="JavaScript" src="dbnetsuite.js.ashx"></script>
<script>
jQuery(document).ready( init )
////////////////////////////////////////////////////////////
function init()
////////////////////////////////////////////////////////////
{
var dbnetgrid1 = new DbNetGrid("dbnetgrid1");
with (dbnetgrid1)
{
connectionString = "SamplesDatabase"
fromPart = "Products"
fixedFilterSql = "discontinued <> @discontinued"
fixedFilterParams["discontinued"] = 0;
setColumnProperty("supplierid","lookup","select supplierid, companyname from suppliers")
setColumnProperty("categoryid","lookup","select categoryid, categoryname from categories")
setColumnProperty("supplierid","label","Supplier")
setColumnProperty("categoryid","label","Category")
bind('onInitialized', addButtons);
initialize()
}
}
////////////////////////////////////////////////////////////
function addButtons(control)
////////////////////////////////////////////////////////////
{
var btn = jQuery(control.addToolbarButton(-1));
btn.html( jQuery(control.toolbarElement("save")).html() );
btn.find(".tool-button-text").html(" Custom CSV").css('width', '105px').css('text-align', 'right');
btn.click( createCSV );
btn = jQuery(control.addToolbarButton(-1));
btn.text( "Re-Instate" );
btn.addClass("my-update-button")
btn.prop("disabled",true);
btn.attr("title","Clear discontinued flag on selected product records");
btn.click( updateRecords );
}
////////////////////////////////////////////////////////////
function createCSV()
////////////////////////////////////////////////////////////
{
var data = {}
data.Filter = DbNetLink.components["dbnetgrid1"].fixedFilterSql;
data.Params = DbNetLink.components["dbnetgrid1"].fixedFilterParams;
DbNetLink.Util.webMethod("CreateCSV", window.createCSVCallback, data);
}
////////////////////////////////////////////////////////////
function createCSVCallback(response)
////////////////////////////////////////////////////////////
{
jQuery(".my-update-button").attr("disabled","");
jQuery(".csv-text").val(response.csv);
}
////////////////////////////////////////////////////////////
function updateRecords()
////////////////////////////////////////////////////////////
{
var data = {}
data.Filter = DbNetLink.components["dbnetgrid1"].fixedFilterSql;
data.Params = DbNetLink.components["dbnetgrid1"].fixedFilterParams;
DbNetLink.Util.webMethod("UpdateProductRecords", window.updateRecordsCallback, data);
}
////////////////////////////////////////////////////////////
function updateRecordsCallback(response)
////////////////////////////////////////////////////////////
{
DbNetLink.components["dbnetgrid1"].loadData();
}
</script>
<table> <tr> <td> <fieldset> <legend>Discontinued Products</legend> <div id="dbnetgrid1"></div> </fieldset> </td> </tr> <tr> <td> <fieldset> <legend>CSV Text</legend> <textarea class="csv-text" style="width:100%;height:200px;"></textarea> </fieldset> </td> </tr> </table>
[WebMethod(CacheDuration=0)] // Set the CacheDuration to 0 while debugging to prevent caching of proxy methods
///////////////////////////////////////////////////////////////
public static ListDictionary CreateCSV(string Filter, Dictionary<string,object> Params)
///////////////////////////////////////////////////////////////
{
QueryCommandConfig Query = new QueryCommandConfig();
Query.Sql = "select * from products where " + Filter;
foreach (string Key in Params.Keys)
Query.Params[Key] = Params[Key];
DbNetData Db = new DbNetData("SamplesDatabase");
Db.Open();
Db.ExecuteQuery(Query);
StringBuilder CSV = new StringBuilder();
while (Db.Reader.Read()) {
List<string> Values = new List<string>();
Values.Add( Db.ReaderString("ProductName") );
Values.Add( Db.ReaderString("QuantityPerUnit") );
Values.Add( Db.ReaderString("UnitPrice") );
Values.Add( Db.ReaderString("UnitsInStock") );
Values.Add( Db.ReaderString("UnitsOnOrder") );
Values.Add( Db.ReaderString("ReorderLevel") );
CSV.Append( String.Join(",",Values.ToArray()));
CSV.Append( Environment.NewLine );
}
Db.Close();
ListDictionary Response = new ListDictionary();
Response["csv"] = CSV.ToString();
return Response;
}
[WebMethod(CacheDuration=0)] // Set the CacheDuration to 0 while debugging to prevent caching of proxy methods
///////////////////////////////////////////////////////////////
public static void UpdateProductRecords(string Filter, Dictionary<string,object> Params)
///////////////////////////////////////////////////////////////
{
UpdateCommandConfig Update = new UpdateCommandConfig();
Update.Sql = "update products set discontinued = 1 where " + Filter;
foreach (string Key in Params.Keys)
Update.FilterParams[Key] = Params[Key];
DbNetData Db = new DbNetData("SamplesDatabase");
Db.Open();
Db.ExecuteUpdate(Update);
Db.Close();
}