Figer's Technology Consulting | December 2014

dynamic ascx user control button event handler not firing - fix

Remember to assign the control and ID and in the controls , Page_Load re-assign that ID 

 Dim ClinControl As OOClinicianRecord = Page.LoadControl("~/OO/CareTeam/OOClinicianRecord.ascx") ClinControl.ID = "OOClinRecord" newItemPanel.Controls.Add(ClinControl)

then in OOClinicianRecord.ascx.vb

Private Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Me.ID = "OOClinRecord"
End Sub

Creating a JQuery CSS imageless button and calling it from .NET code behind

Add this style to the page 
 ---------------------------------
<style>
.myButton {
-moz-box-shadow:inset 0px 1px 0px 0px #ffffff;
-webkit-box-shadow:inset 0px 1px 0px 0px #ffffff;
box-shadow:inset 0px 1px 0px 0px #ffffff;
background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #ededed), color-stop(1, #dfdfdf) );
background:-moz-linear-gradient( center top, #ededed 5%, #dfdfdf 100% );
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#ededed', endColorstr='#dfdfdf');
background-color:#ededed;
-moz-border-radius:6px;
-webkit-border-radius:6px;
border-radius:6px;
border:1px solid #dcdcdc;
display:inline-block;
color:#777777;
font-family:arial;
font-size:15px;
font-weight:bold;
padding:6px 24px;
text-decoration:none;
text-shadow:1px 1px 0px #ffffff;
}.myButton:hover {
background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #dfdfdf), color-stop(1, #ededed) );
background:-moz-linear-gradient( center top, #dfdfdf 5%, #ededed 100% );
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#dfdfdf', endColorstr='#ededed');
background-color:#dfdfdf;
}.myButton:active {
position:relative;
top:1px;
}
</style>

--------------------------------- 
  Add this as your button to the page 
 --------------------------------- 
 <a href="#" id="testbutton" runat="server" title="Test Button" class="myButton">test button</a> 

 --------------------------------- 
  Add this as your event handler to the code behind (using vb.net in this example) 
 --------------------------------- 
 Private Sub CmdTestButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles testbutton.ServerClick 

Dim i As String = "" 
'This is just test code to add a break to and confirm it gets caught here when you attach the debugger 
End Sub

How to use ASP.NET AjaxControlToolkit

Follow these steps to install the AjaxControlToolkit into your Visual Studio enviroment: http://www.asp.net/ajaxlibrary/act.ashx 

Here is an example of the PopupExtender control Include: 

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

Javascript function to close the pop-up: 

function hideajaxpop() {
var bh = $find("PopEx");
bh.hidePopup();
}

ASP.NET controls to make the pop-up work:

<asp:HyperLink ID="HyperLink1" runat="server" Font-Underline="True">Add Patient</asp:HyperLink>
<asp:Panel ID="pnlPatientSignup" runat="server" Style="display: none">
<div>
<a onclick="hideajaxpop(); return false;" title="Close">X</a>
</div>
<uc3:ClinSignUpPatient ID="ClinSignUpPatient1" runat="server" />
</asp:Panel>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<asp:PopupControlExtender ID="PopEx" runat="server"
TargetControlID="HyperLink1"
PopupControlID="pnlPatientSignup"
Position="Bottom" />

Determine what part of your site is actually used

Why put your development effort / dollars into areas of the site / web application no one is using. Or use this to question areas you are developing and verbally ask clients why they don't use a certain portion of your site, you might find out only a small change makes all the difference in the world to your client. Google analytics is a great tool to determine where users are going and what they're doing on your site. On top of the typical usage on seeing which pages get hit, here is the necessary code to see which buttons get clicked or any other event on your site. 

 ------------------------------------------- 
Place this in the <head> tag of your page (use your correct Account ID, the rest will stay the same)

 
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1111111-1']);
_gaq.push(['_setDomainName', 'none']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
(function () {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<script type="text/javascript"> var _gaq = _gaq || [];            _gaq.push(['_setAccount', 'UA-1145632-1']);            _gaq.push(['_setDomainName', 'none']);            _gaq.push(['_setAllowLinker', true]);            _gaq.push(['_trackPageview']); (function () {                var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;                ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';                var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);            })(); </script> 

------------------------------------------- 
Place this in the hyperlink or button's onclick event (ClickDescHere is what will show up in Google Analytics), if you have multiple subdomains like we have Google even breaks down the click events for you by hostname. onclick="_gaq.push(['_trackEvent', 'ClickDescHere']);"

PHP / MySQL - Upload a file to access later

In this example I am uploading to the file system, but saving the location in mySQL to reference later.

<form action="insert.php" enctype="multipart/form-data" method="post">
<input name="MAX_FILE_SIZE" type="hidden" value="20000000" />
<input name="fileupload" type="file" />
<input alt="Submit Form"/>
</form>
--------------------------------------------------------------

//Database variables are located with-in here
include("dataconnection.php");

$con = mysql_connect($hostname, $username, $password);
if (!$con)
{ die('Could not connect: '); }

$selected = mysql_select_db($dbname);

//Generate Random Number & date to add to file name so they're always unique
$rNumb=rand();
$today = getdate();
$id=$today['mon'].$today['mday'].$today['year']." ".$rNumb;
$id2=str_replace(" ","",$id);

//Move temp file auto created to permanent location
$uploadfile = 'uploadfiles/' . $id2 . basename($_FILES['fileupload']['name']);
if (move_uploaded_file($_FILES['fileupload']['tmp_name'], $uploadfile)) { }
else {}

//Check to see if anything was uploaded, otherwise set variable to nothing
if($uploadfile == 'uploadfiles/' . $id2)
{$uploadfile = "";}

if (!mysql_query($sql,$con))
{ die('There was an issue, please go back and try again, if it still happens please contact technical support'); }

mysql_close($con)
?>

PHP - handling apostrophes

Here is the code you'll need to use to handle apostrophes, this just adds a backslash in front of the apostrophes


$myvariable = str_replace("'","\'",$myvariable);

search engine optimization

Rather than re-write the topic I wanted to pass on this link which is a great beginners guide to SEO.


http://guides.seomoz.org/beginners-guide-to-search-engine-optimization

Emailing from PHP

about 98% of the time your web host will already have the php.ini file set up correctly so literally all it takes to email is this (if that's not the case look here:  http://www.php.net/manual/en/ref.mail.php#77499)

 
//Send Emails
$emailmessage = "Body of your message goes here, I put it in a variable because it's usually very long.";
mail($_POST[email], "Email Subject", $emailmessage , "From: no@no.com" . "\r\n" ."bcc: lucas.baran@figers.com");

SQL findtext - create a stored proc to find text in other stored procs

This stored proc has come in handy many times, great to use it to find text in other stored procedures
Once you've created this stored procedure in your environment of SQL it's as simple as running findText "findtextenteredhere"
-----------------------------------------------------------------
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
CREATE   PROCEDURE [dbo].[findText] (@text VARCHAR(50)) AS
--Declare @text varchar(50)
--set @text  = 'SmIepHeader'
-- Adjust search text to find all contains.
SET @text = '%' + @text + '%'
-- Declare general purpose variables.
DECLARE @line VARCHAR(300)
DECLARE @char CHAR
DECLARE @lineNo INTEGER
DECLARE @counter INTEGER
-- Declare cursor structure.
DECLARE @proc VARCHAR(50),
@usage VARCHAR(4000)
-- Declare cursor of stored procedures.
DECLARE codeCursor CURSOR
FOR
SELECT SUBSTRING(OBJECT_NAME(id),1,50) AS sproc,
text
FROM syscomments
WHERE text LIKE @text
-- Open cursor and fetch first row.
OPEN codeCursor
FETCH NEXT FROM codeCursor
INTO @proc,@usage
-- Check if any stored procedures were found.
IF @@FETCH_STATUS <> 0 BEGIN
PRINT 'Text ''' + SUBSTRING(@text,2,LEN(@text)-2) + ''' not found in stored procedures on database ' + @@SERVERNAME + '.' + DB_NAME()
-- Close and release code cursor.
CLOSE codeCursor
DEALLOCATE codeCursor
RETURN
END
-- Display column titles.
PRINT 'Procedure' + CHAR(39) + 'Line' + CHAR(9) + 'Reference ' + CHAR(13) + CHAR(13)
-- Search each stored procedure within code cursor.
WHILE @@FETCH_STATUS = 0 BEGIN
SET @lineNo = 0
SET @counter = 1
-- Process each line.
WHILE (@counter <> LEN(@usage)) BEGIN
SET @char = SUBSTRING(@usage,@counter,1)
-- Check for line breaks.
IF (@char = CHAR(13)) BEGIN
SET @lineNo = @lineNo + 1
-- Check if we found the specified text.
IF (PATINDEX(@text,@line) <> 0)
PRINT @proc + CHAR(39) + STR(@lineNo) + CHAR(9) + LTRIM(@line)
SET @line = ''
END ELSE
IF (@char <> CHAR(10))
SET @line = @line + @char
SET @counter = @counter + 1
END
FETCH NEXT FROM codeCursor
INTO @proc,@usage
END
-- Close and release cursor.
CLOSE codeCursor
DEALLOCATE codeCursor
RETURN

Dynamically adding controls to a placeholder control in ASP.NET

Loop through your collection of controls (count of i) you would like to add to the ASP.NET placeholder control on your aspx page.

Dim lnkButtonSelfInitiate As LinkButton = New LinkButton()
lnkButtonSelfInitiate.Text = vCellLocalLink.InnerText
AddHandler lnkButtonSelfInitiate.Click, AddressOf btn_Click
Me.plhSelfInitiate.Controls.Add(lnkButtonSelfInitiate)

Protected Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.lblTest.Text = "test"
End Sub