Saturday, May 23, 2009

Uppercase textbox in asp.net

In vb.net you have a property on the TextBox itself while on asp.net you should use a javascript function or a maskeditextender from ajax....
but the css style can do your work with 1 line of code.
Simple,fast,clear :





and your css :

.UpperCase{text-transform:uppercase;}

Saturday, May 16, 2009

How to Add Dynamic Buttons,Images in asp.net ?

Today i had the necessity to add some dynamic buttons to a page based on a SQL Server 2005 table. Basically i've a Dataset that return to me Functions based on the user logged in.
This code can be easily changed to do different things (like menu\treeview).

So my Goal was to have a page that build up n buttons based on some info that returns from the DB, and each buttons has an handler so to do\call different pages or do different things when pressing each button.

First of all i created a new webusercontrol called ctButtonPanel and overrided the Render method as follow :


Public Overrides Sub RenderControl(ByVal writer As System.Web.UI.HtmlTextWriter)
For Each objControl As Web.UI.WebControls.Button In Me.Controls
writer.AddStyleAttribute("padding", "10px") 'Spacing buttons
writer.RenderBeginTag(HtmlTextWriterTag.Div) 'Put buttons inside DIV
objControl.RenderControl(writer) 'Write the control (button in this case)
writer.RenderEndTag() 'close the DIV
Next
End Sub


You can do whatever there. I wanted the button to appears vertically and spaced.
Once created the webusercontrol just put a reference in the page that should load your items





Then on the server side of the page we just iterate trough our Dataset with function infos and add a new button(or whatever webcontrol) to the CommandPanel as follow :

'Important - clear the webusercontrol each time.
Me.CommandPanel.Controls.Clear()
'Instanciate the control (in my case a button is enough)
Dim mybutton As Button
For Each row As DataRow In value.Tables(0).Rows 'my Dataset
mybutton = New Button
mybutton.Text = row("sCodice") 'my Function Text
mybutton.ID = row("nIDSceltaLavorazioneOperatore") 'my Function ID
mybutton.Height = Unit.Pixel(30)
mybutton.Width = Unit.Pixel(200)
mybutton.CssClass = "BTN_Submit_Lavorazione"
AddHandler mybutton.Click, AddressOf BottoneClick
Me.CommandPanel.Controls.Add(mybutton) 'Add the just created button
Next


And add the Handler that will be called by the created buttons once clicked as follow

Private Sub BottoneClick(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myBtn As Button = sender
'Here i used the button ID to do different things. ID are distinct functionID in my 'case so was enough.
'You can add Arguments when creating the button and retryve the arguments from here

select case myBtn.id
case 1
case 2
case 3
case 4
end select

end sub


And here is a quick sample of what i got.


Hope it can helps.
Ciao

Wednesday, May 13, 2009

How to execute DTSX from Stored Procedures/Asp.Net ?

Let's imagine a common scenary where we have a server with our SQL Server 2005 and another server with our aps.net application running.
Our goal is to launch a dtsx from our web application and get back a @returncode that will inform us about the execution state.

  1. Let's build our DTSX package, test it and publish on a server reachable from our asp.net application (can be the same server)
  2. Let's create a Stored Procedure that will execute the DTSX passing to it some variable.
  3. Let's create a function inside our web.application that will execute the Stored Procedure and get back the result.
Let's GO..

1. The Stored Procedure will execute the DTSX , passed as parameter from our web.application, trough the shell (xp_cmdshell), with the command DTExec. The Proceudre i built for this example is very flexible, so all the command line that the xp_cmdshell will execute is built in from the web.application

CREATE procedure [dbo].[spExecuteDTSX]

@cmd varchar(2000)

as

SET NOCOUNT ON

declare @returncode int
exec @returncode = master..xp_cmdshell @cmd
select @returncode Errore


Be sure to have enabled the xp_cmdshell (Sql Server Surface Attack Configuration)

2. Server side create an enumerator (it's not manditory, but will help us to have a cleaner code)

Public Enum DTExecRet
package_OK = 0
package_FAIL = 1
package_ABORT = 3
package_NOTFOUND = 4
package_NOTLOAD = 5
package_SNTXERROR = 6
End Enum


In the function\method that will execute your Stored Procedure (i imagine a button click), let's instanziate 3 variables now.
One for the dtsx server path, one for the dtsx name and another for the final string with the execute command.
In this example the server path is taken form the web.config and i pass to the DTSX also 2 variables (you have to configure the DTSX to accept 2 variables inbound).

Dim strPath As String = System.Configuration.ConfigurationManager.AppSettings("DTS").ToString
Dim strFile As String = "Package.dtsx"
Dim cmd As String = "DTExec /F """ & strPath & strFile & """ /SET \Package.Variables[sNome].Properties[Value];" & Me.txtNome.Text & " /SET \Package.Variables[nIDCliente].Properties[Value];" & Me.cbbCliente.SelectedValue


So now we have all ready and we can call our Stored Procedure in the way we are used to do passing the 3 parameters we created and get back a @returncode.
In my case the Procedure will return a Datasat.

objImportazione.cmd = cmd
Dim res As DTExecRet = objImportazione.RunDTSX


Once we have our @returncode we can easily check how went the whole process

strRes = String.Empty
Select Case res
Case CLSImportazione.DTExecRet.package_OK
strRes="ok"
Case CLSImportazione.DTExecRet.package_ABORT
strRes="abort"
Case CLSImportazione.DTExecRet.package_FAIL
strRes="fail"
Case CLSImportazione.DTExecRet.package_NOTFOUND
strRes="package not found"
Case CLSImportazione.DTExecRet.package_NOTLOAD
strRes="error loading package"
Case CLSImportazione.DTExecRet.package_SNTXERROR
strRes="sintax error"
End Select

CLSUtility.AlertScript(me,strRes)


for the msgbox chek this CLSUtility.AlertScript(me,"Your Message Here")

This is all guys, i hope it's quite clear and once again sorry if my english is not perfect.

Wednesday, May 6, 2009

ToolBar.Net - Easy to use Toolbar

Here is my first version of an easy to use and implement toolbar all customizable (cssClass) and perfectly working also within ajax updatepanels.

You can add to it buttons(ToolButton) or imagebuttons(ToolImage) both from client side and server side.
If on server side remember to add a Click handler for each button you will add (or just one handler passing a different commandArgument on each button)

At the end of this post you will find the dll. Just add a reference to it in your project and instanziate it on your pages like


<%@ Register Assembly="ToolBarNet" Namespace="ToolBarNet.ToolBarAsp" TagPrefix="cc1" %>


then add the following code (adding or removing buttons\imagebuttons) wherever you wat your ToolBar









I'm finishing a second version that implements also a chekFunction class that will automatically chek functions allowed to certain group\users and enable\disable automatically elements on the toolbar

please direct link this post if you want to share the toolbar
Thanks

Tuesday, May 5, 2009

Aspnet MsgBox

We all love the MsgBox. It's usefull and is a quick way to alert users for something.
Unfortunally aspnet hasn't a builtin control and we have to play a bit to achieve our goal.
Today i'll explain a simple way to use it, tested on framework 2.0 and 3.5 (insde updatepanels) (3.5 has a builtin msgBox but it has some problems once we publish the application, but i'll discuss this later).

A good way to go is to build up an Utility Class and make shared
most of the methods we construct inside it. Doing this we dont have to initialize the class everytime to call a method.
Here we go:


Public Shared Sub AlertScript(ByVal myPage As System.Web.UI.Page, ByVal sMessage As String)
Dim chiave As String = "alert('" + JavaEncode(sMessage) + "')"
Dim MyKey As String = "chiave"
System.Web.UI.ScriptManager.RegisterClientScriptBlock(myPage, myPage.GetType, MyKey, chiave, True)
End Sub

and let's assume we put this inside our Utility Class called CLSUtility
Now from our page we just have to call the method AlertScript
in this way :

CLSUtility.AlertScript(me,"Your Message Here")


JAVAENCODE FUNCTION

Function JavaEncode(ByVal strResponse As String) As String
Dim strVal As String
'
If Not strResponse Is Nothing Then
strVal = strResponse
' Carattere \
strVal = Replace(strVal, "\", "\\")
' Carattere '
strVal = Replace(strVal, "'", "\'")
' Carattere &
strVal = Replace(strVal, "&", "\&")
' Carattere %
strVal = Replace(strVal, "%", "\%")
' Carattere "
strVal = Replace(strVal, """", "\""")
' Carattere +
strVal = Replace(strVal, "+", "\+")
' Carattere <
strVal = Replace(strVal, "<", "\<")
' Carattere >
strVal = Replace(strVal, ">", "\>")
' Carattere
strVal = Replace(strVal, Chr(10), "\r")
strVal = Replace(strVal, Chr(13), "")
strVal = Replace(strVal, """", "'")
Return strVal
Else
Return Nothing
End If
''
End Function


Here you go, hoping that this little tip may help some of you.
This works also if you put your button or whatever inside an
Ajax UpdatePanel

Later i'll post a great control that uses the AjaxToolkit to show up MessageBox with different colors according the priority of the message\error
.
Stay tuned.