HTML 5 offers some great new features for designers. No more using background images for your gradient backgrounds.
Category: 3DW DevTools
Compare old and new record for logging c#
You have an old record, and the new one to be saved to database. Generate a log of the changes made using this snipped.
string logmessage = "The Doc Repo Category " + oldDocCatTitle + " has been edited.";
logmessage += (oldDocCatTitle != DocCatTitle) ? "The title changed from " + oldDocCatTitle + " to " + DocCatTitle + "." : "";
logmessage += (oldDocCatDesc != DocCatDesc) ? "The description changed from " + oldDocCatDesc + " to " + DocCatDesc + "." : "";
logmessage += (oldDocCatShared != DocCatShared) ? "The shared changed from " + oldDocCatShared.ToString() + " to " + DocCatShared.ToString() + "." : "";
logmessage += (oldDocCatShowOnHome != DocCatShowOnHome) ? "The description changed from " + oldDocCatShowOnHome.ToString() + " to " + DocCatShowOnHome.ToString() + "." : "";
if (oldDocCatTitle != DocCatTitle || oldDocCatDesc != DocCatDesc || oldDocCatShared != DocCatShared || oldDocCatShowOnHome != DocCatShowOnHome || oldDocCatLink != DocCatLink)
{
bool addLog = dataProcessing.addLog(Session["adminSID"].ToString(), "", logmessage, out string logMessage);
}
Return Post Variables from c# api as JSON array
Call to c# api using ajax and recieve an array of variables in JSON format.
javascript call to api: In aspx page, you can access a posted in variable such as an id…
const NewsID = ‘<%=Request.Form[“NewsID”]%>’;
… then call to api and get record from that id…
$.ajax({
type: 'POST',
url: 'update.aspx/loadRecord',
data: JSON.stringify({ NewsID: NewsID }),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (msg) {
var obj = jQuery.parseJSON(msg.d)
console.log("I have successfully called to the api.");
// set the heading value
$("#Heading").val(obj.Heading);
// set the body value
$("#Body").val(obj.Body);
}
});
The C# api receives the record id and returns a record…
... call to database and get record ...
var message = new {Heading=Heading, Body= Body};
return JsonConvert.SerializeObject(message);
Add javascript via c# code behind in .net webforms project
You can call a javascript function in the codebehind of a webforms project. Its important not to forget the semi-colon after the function name.
Page.ClientScript.RegisterStartupScript(this.GetType(), "Call My Javascript Function", "myJavaScriptFunction();", true);
Append options to a select element using javascript
//dynamically add 20 years from this year to drop-down
     var select = document.getElementById(“ccexp”);
     var startYear = new Date().getFullYear();
     var option, currentYear, currentYearString, currentYearTwoDigit;
     for (var i = 0; i <= 20; i++) {
         currentYear = startYear + i;
         currentYearString = currentYear.toString();
         currentYearTwoDigit = currentYearString.substring(2);
         option = document.createElement(“option”);
         option.setAttribute(“value”, currentYearTwoDigit);
         option.appendChild(document.createTextNode(currentYear));
         select.appendChild(option);
     }
Javascript to append options to a select element
select multiple elements by class, and loop through them with javascript
I have a table with elements in it. I need to target them and change the css class.
<div class="dwtooltip"><img src="note_icon.png" /><span class="dwtooltiptext">my tooltip text here.</span></div>
const items = document.querySelectorAll(‘.dwtooltip’);
if(items){}
for (var i=0; i < items.length; i++) {
console.log(i);
items.forEach(function(el){
el.classList.add(“notooltip”);
el.classList.remove(“dwtooltip”);
});
}
Get the value from between parenthesis c#
Many times you have data in a format that needs to be parsed. One thing I have come across lately is needing to get a value from between parenthesis.
Data: Here is my description and it has a code.(1234)
I need to get the code of 1234 from the data.
string value = "Here is my description and it has a code.(1234)";
int startIndex = value.IndexOf('(');
int endIndex = value.IndexOf(')');
int length = endIndex - startIndex;
if (startIndex > -1 && endIndex > -1)
{
return value.Substring(startIndex + 1, length-1);
}
else  return "There are no parenthesis in the value.";
Local email delivery : Dump email from an app into a local directory for testing
While devving an app on localhost, the email scripts error out and you cant test what the message would have looked like. With this handy snippet, you can dump any email from your app into a local directory. Slap this into your dev web config file.
<mailSettings> Â Â Â <smtp deliveryMethod="SpecifiedPickupDirectory"> Â Â Â Â Â <specifiedPickupDirectory pickupDirectoryLocation="C:\MailDump\" /> Â Â Â Â Â <network host="localhost"/> Â Â Â </smtp> Â </mailSettings>
Add an ID to the rows of a GridView table
It can be handy to have each row have an ID for use with client side scripting. By default the asp gridview does not come with ids for the rows. Use the row databound event to add them
protected void GridViewFullView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow row = e.Row;
row.Attributes["id"] = GridViewFullView.DataKeys[e.Row.RowIndex].Value.ToString();
}
}
Check if string contains only letters and/or numbers
Its super easy to validate if a string does not have any special characters in it.
Bool isValid(string inputText)
{
return inputText.All(Char.IsLetterOrDigit);
}