Custom Search

ASP.net / Vb.net

I missed coding in Visual Studio.

For two months now, I’ve been digging my way into the open source arena, php and mysql to be specific. I’ve been a full time asp.net csharp developer for more than two years already but now I am into magento and code igniter using LAMP servers. Even though coding in asp.net is a lot easier compare to developing in php (because of visual stdudio.) I still find being in open source now a days is a lot more practical if you and your clients are in the SME business front. For example, magento. I can’t create a system as powerful as magento for a short period of time even if I use asp.net. Yes there are also e-commerce platforms that uses asp.net like aspdotnetstorefront but it is very expensive and is too hard to customize. I used it to one of my clients before, And it was a pain to my ass tweaking its engine to meet the clients needs. Compared to magento, even a php beginner can customize it. I am not saying that I am leaving asp.net now, I can somehow use my experience of both sides to be more effective in my future projects.

How to Hide the Root Node in asp.net Menu Control

Set ShowStartingNode=”false” in the SitemapDataSource control.

<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="false" />
       <asp:Menu ID="MenuControl" runat="server" Orientation="Horizontal"
          DataSourceID="SiteMapDataSource1" StaticDisplayLevels="2">
        </asp:Menu>

Casting Controls in Vb.net

Dim lblProdCode As New Label
lblProdCode = DirectCast(e.Row.FindControl("lblProdCode"), Label)
lblProdCode.Text = ProductsGrid.DataKeys(e.Row.RowIndex).Values("ProdCode").ToString()

Dim lblProdName As New Label
lblProdName = DirectCast(e.Row.FindControl("lblProdName"), Label)
lblProdName.Text = ProductsGrid.DataKeys(e.Row.RowIndex).Values("ProdName").ToString()

Dim lblTotalItemSold As New Label
lblTotalItemSold = DirectCast(e.Row.FindControl("lblTotalItemSold"), Label)
lblTotalItemSold.Text = "999"

lblProdCode.Dispose()
lblProdName.Dispose()
lblTotalItemSold.Dispose()

Displaying (Generating) Index Number (Row Number) on your Gridview

Put this in your Gridview…

<asp:TemplateField>
             <HeaderTemplate>
                 <asp:Label ID="Label4" runat="server" Text="Index #"></asp:Label>
             </HeaderTemplate>
             <ItemTemplate>
                 <asp:Label ID="Label1" runat="server" Text=‘<%# Container.DataItemIndex + 1 %>‘></asp:Label>
             </ItemTemplate>
             <HeaderStyle HorizontalAlign="Left" />
              <ItemStyle HorizontalAlign="Left" />
          </asp:TemplateField>

The Code that generates the row index is

<%# Container.DataItemIndex + 1 %>

Handling Paging in Gridview in codebehind

Protected Sub OrderGrid_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles OrderGrid.PageIndexChanging
        OrderGrid.PageIndex = e.NewPageIndex
        OrderGrid.DataBind()
    End Sub

Populating Gridview using Rowdatabound Event

The Html Code

<asp:GridView ID="OrderGrid" runat="server" DataKeyNames="OrderTotal,OrderDate" AutoGenerateColumns="false">
       <Columns>
         <asp:TemplateField>
           <HeaderTemplate>
               <asp:Label ID="Label2" runat="server" Text="Order Total"></asp:Label>
           </HeaderTemplate>
           <ItemTemplate>
               <asp:Label ID="OrderTotalTxt" runat="server" Text="Label"></asp:Label>
           </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField>
           <HeaderTemplate>
               <asp:Label ID="Label3" runat="server" Text="Order Date"></asp:Label>
           </HeaderTemplate>
           <ItemTemplate>
               <asp:Label ID="OrderDateTxt" runat="server" Text="Label"></asp:Label>
           </ItemTemplate>
         </asp:TemplateField>
       </Columns>
    </asp:GridView>

the code behinde vb.net
Loading the Grid

Protected Sub LoadOrderGrid()
        OrderGrid.DataSource = ObjectOrder.GetOrdersAdminAll()
        OrderGrid.DataBind()
    End Sub

Populating during Rowdatabound

Protected Sub OrderGrid_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)  Handles OrderGrid.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then
             Dim OrderTotalTxt As New Label
             OrderTotalTxt = DirectCast(e.Row.FindControl("OrderTotalTxt"), Label)
             OrderTotalTxt.Text = OrderGrid.DataKeys(e.Row.RowIndex).Values("OrderTotal").ToString()
             TotalAmount += Convert.ToDecimal(OrderTotalTxt.Text)
             Dim OrderDateTxt As New Label
                OrderDateTxt = DirectCast(e.Row.FindControl("OrderDateTxt"), Label)
                OrderDateTxt.Text = OrderGrid.DataKeys(e.Row.RowIndex).Values("OrderDate").ToString()

                OrderDateTxt.Dispose()
                OrderTotalTxt.Dispose()

        ElseIf e.Row.RowType = DataControlRowType.Footer Then

        NumberOfOrders.Text = OrderGrid.Rows.Count()
        TotalAmountofOrders.Text = "$ " & TotalAmount
        End If

    End Sub

SOAP vs REST

Displaying Data in Gridview with Download to .xls Button, in vb.net

Aspx

<form id="form1" runat="server">
        <asp:Button ID="dataDownload" runat="server" Text="Download" />
        <asp:GridView ID="customers" runat="server" >        
        </asp:GridView>      
 </form>

Code Behind:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        customers.DataSource = ObjOrders.GetOrderCustomer(Me.Page)
        customers.DataBind()

    End Sub

    Public Overloads Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
    End Sub

    Protected Sub dataDownload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles dataDownload.Click
        Dim attachment As String = "attachment; filename=Contacts.xls"  ‘name of the output .xls file
       Response.ClearContent()
        Response.AddHeader("content-disposition", attachment)
        Response.ContentType = "application/ms-excel"
        Dim sw As New StringWriter()
        Dim htw As New HtmlTextWriter(sw)

        customers.RenderControl(htw)
        Response.Write(sw.ToString())
        Response.[End]()
    End Sub

Issues encountered in opening Asp.net MVC in different versions of Visual Studio.

I have created a basic asp.net mvc structure using visual studio 2008 express edition, now when I open it using visual studio 2008 professional can’t open the the directory where my main MVC files are located, maybe its because the visual studio 2008 professional edition does not have MVC template installed in it?.. the pursuit continues…

Application Design Using Asp.net n-tier architecture techniques.

Although Asp.net MVC (Model-View-Controller) is a promising framework many Asp.net Developer’s find it hard to transition and move. blahh .. blahhh.. blahhh….