Tag Archives: ASP.NET

ASP.NET–prevent button from form submit based on condition in javascript

Here is a javascript function that retutns true or false based on wheather a listbox is empty (select element in html does not have any option elements)

function WarnIfCleanersListboxEmpty() {
    if ($('#lboxCleaners').children().length == 0) {
        alert('list is empty');
        return false;
    return true;

in ASP.NET page there is a button that submits the form

 <asp:Button ID="btnGenerate" runat="server" Text="Generuj" OnClientClick="return WarnIfCleanersListboxEmpty()"/>

it’s the return in OnClientClick that I always forget and then wander why the form gets submitted even if my javascript function evaluates to false.

WRONG: OnClientClick=”WarnIfCleanersListboxEmpty()”

RIGHT: OnClientClick=”return WarnIfCleanersListboxEmpty()”


Scott Hanselman’s 2011 Ultimate Developer and Power Users Tool List for Windows – Scott Hanselman

Scott Hanselman's 2011 Ultimate Developer and Power Users Tool List for Windows – Scott Hanselman.

Get indexed collection out of LinqDataSource

I wanted to convert this SQLdatasource

<asp:SqlDataSource ID=”SqlDataSource1″ runat=”server”
ConnectionString=”<%$ ConnectionStrings:PaclikJConnectionString %>”
SelectCommand=”SELECT Row_number() OVER (ORDER BY Vlozeno DESC) as Poradi, ObrazID, Nazev, NazevAj, datepart(yy,DatumVyroby) as ‘RokVyroby’, Popis, PopisAj, cast(RozmerX as varchar(3)) + ‘x’ + cast(RozmerY as varchar(3)) + ‘ cm’ as Rozmer, Cena, NaProdej, Vlozeno, Nazev + ‘, rok: ‘ + cast(datepart(yy,DatumVyroby) as varchar(4)) + ‘, ‘ + Popis + ‘, ‘ + cast(RozmerX as varchar(3)) + ‘x’ + cast(RozmerY as varchar(3)) + ‘ cm’ as AltText, Nazev + ‘, rok: ‘ + cast(datepart(yy,DatumVyroby) as varchar(4)) + ‘, ‘ + PopisAj + ‘, ‘ + cast(RozmerX as varchar(3)) + ‘x’ + cast(RozmerY as varchar(3)) + ‘ cm’ as AltTextAj FROM Obrazy inner join Techniky on Obrazy.Technika = Techniky.TechnikaID ORDER BY Vlozeno DESC”>

to LinqDataSource. The importnat part on the SQLDatasource is of cource the SQL query which indexes the rows. Lower is the solution I came up with to get the same behaviour with LInqToSQL Linqdatasource

Protected Sub LinqDataSource1_Selecting(sender As Object, e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles LinqDataSource1.Selecting
        'handle selecting event if you want to return custom objects ie. dont need all props or need to create custom props

        Dim ctx As New PaclikDataContext

	    'ObjectTrackingEnabled = False 'cos I'm not going to update object, just show them
        ctx.ObjectTrackingEnabled = False
        Dim Obrazy = (ctx.Obrazs _
                     .OrderByDescending(Function(o) o.Vlozeno) _
                     .Select(Function(o) New With { _
                                 .ObrazId = o.ObrazID, _
                                 .Nazev = o.Nazev, _
                                 .NazevAj = o.NazevAj, _
                                 .RokVyroby = o.DatumVyroby.Year, _
                                 .Popis = o.Technika1.Popis, _
                                 .PopisAj = o.Technika1.PopisAj, _
                                 .Rozmer = o.RozmerX.ToString & " x " & o.RozmerY.ToString & " cm"})).ToList

        'I used .ToList 'cos I need the query to return something that implements IEnumerable(Of T)
        ' as AFaIK the overload of Select with the second index param can be used only on IEnumerable(Of T) not on IQueryable

			'notice the creation of Index property .Index = i
			e.Result = Obrazy.Select(Function(o, i) New With {.Index = i, _
                                                          .ObrazId = o.ObrazId, _
                                                          .Nazev = o.Nazev, _
                                                          .NazevAj = o.NazevAj, _
                                                          .RokVyroby = o.RokVyroby, _
                                                          .Popis = o.Popis, _
                                                          .PopisAj = o.PopisAj, _
                                                          .Rozmer = o.Rozmer _

    End Sub
End Class

Tips, tricks and time-savers for the Windows and ConfigMgr administrator

To The Point

Anything about Technology and Business

Brian's Power Windows Blog

Microsoft in the Enterprise. Windows, Hyper-V, Exchange, SQL, and more!

PowerScripting Podcast

Shownotes and links for the PowerScripting Podcast, a podcast to help people learn Windows Powershell

Learn Powershell | Achieve More

What is this Powershell of which you speak?