Posts

Showing posts from 2008

Easy Nested Datagrid

Nested Grid Example ASPX Page <asp:datagrid id ="dgBids" onItemCommand ="commandDGBid" headerStyle-CssClass ="tdheaders" cellpadding ="5" runat ="server" autogeneratecolumns ="false"> < columns > < asp : BoundColumn dataField ="company" headertext ="Company"></ asp : BoundColumn > < asp : BoundColumn dataField ="bidID" visible ="false"></ asp : BoundColumn > < asp : ButtonColumn buttonType ="LinkButton" dataTextField ="bidid" CommandName ="bid" headertext ="Bid"></ asp : ButtonColumn > < asp : BoundColumn dataField ="seasondesc" headertext ="Season"></ asp : BoundColumn > < asp : BoundColumn dataField ="style" headertext ="Style"></ asp : BoundColumn > < asp : BoundColumn dataField ="styledescription"

No Query Strings Needed

.NET allows you to reference most anything from one page to any page. An example: Page1.aspx Public Shared MyVariableThatIWant_ToPass as string = "secret information" OnAnyOtherPage. aspx Dim TheSecretFromPage1 as string TheSecretFromPage1 = MyProject.Page1. MyVariableThatIW_antToPass

Raising Events

In Default.aspx I have a user control ( < WI : editBOM runat ="server" id ="ctlEdit" />)  I want to refresh  the ctlEdit dropdownlist when another control fires (BOM.ascx)   So in DEFAULT.aspx I add this sub Private Sub ctlNew_drpBOM_Change() Handles ctlNew.drpBOM_Change ctlEdit.drpbomChange() End Sub In the EDITBOM.ASCX file I add this subroutine essentially rebinding the dropdownlist Public Sub drpbomChange() drpBOM.DataSource = VendorBid_Data.itemAttributesvb.getValidBOMStyles() drpBOM.DataBind() drpBOM.Items.Insert(0, "Choose Style" ) End Sub In the BOM.ASCX file I declare the event and raise the event: Public Event drpBOM_Change() I raise the event at the point I want to fire it.... RaiseEvent drpBOM_Change() Thus we have a page that holds two controls - when one dropdownlist or event fires then the other control dropdownlist is refreshed.  This enables me to load up my controls on the page but if circumstances change

DorpDownList in DL set by value in Database

Sub dlComponent_databind( ByVal sender As Object , ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Dim drpMarket As DropDownList = CType (e.Item.FindControl( "drpStatus" ), DropDownList) drpMarket.Items.Insert(0, "No Selection Specified" ) drpMarket.SelectedValue = Trim(e.Item.DataItem.componentStatus) Dim drpCo As DropDownList = CType (e.Item.FindControl( "drpCompany" ), DropDownList) drpCo.Items.Insert(0, "No Selection Specified" ) drpCo.SelectedValue = Trim(e.Item.DataItem.company) End Sub

Keeping Dynamic Controls Dynamic

Part of the issue with including User Controls in a page by adding them in the top of the page like this: <% @ Register TagPrefix ="HMD" TagName ="Assortment" Src ="~/Controls/assortments.ascx" %> is that if the content is something like say a shopping cart or dropdownlists generated from a database or something where the state and the refreshed load is terribly important  - a solution is to load the control dynamically based on an event.  Dim the control and add it to the page via a literal or panel or table cell or some other object that could house it.  This assures that the control is loaded fresh everytime it needs to be. Dim ctlStoreSku As Control = LoadControl( "controls/skuweekly.ascx" ) myPanel.Controls.Add(ctlStoreSku) myPanel.Visible = True That's  it....

Fixed Datagrid Headers

Quick and dirty way to make Datagrid Headers stay at the top of the page while your viewers are scrolling down the page can be accomplished with a little CSS   Add this style:       .DataGridFixedHeader { POSITION : relative ;  TOP : expression(this.offsetParent.scrollTop) ; BACKGROUND-COLOR : white } Add this to your Datagrid properties:       HeaderStyle-CssClass =" DataGridFixedHeader " And Voila ... you have a header that stays in place.

Errors Errors Errors

We would always prefer to avoid all errors but if one happens we certainly want to know about it. Adding an error handler to the Global.asax file is slick - easy and works beautifully..... here is what you need: Sub Application_Error( ByVal sender As Object , ByVal e As EventArgs) Try Dim strFrom As String = System.Configuration.ConfigurationManager.AppSettings( "EmailFrom" ) Dim strTo As String = System.Configuration.ConfigurationManager.AppSettings( "EmailTo" ) Dim msg As System.Net.Mail.MailMessage Dim smtp As New System.Net.Mail.SmtpClient( " mail.yourmailserver.com " ) msg = New System.Net.Mail.MailMessage(strFrom, strTo, "Page Error My Application Name" , BuildMessage()) msg.IsBodyHtml = True smtp.Credentials = New System.Net.NetworkCredential( "Username" , "password" , " mail.myserver.com " ) smtp.Send(msg) Catch ex As Exception End Try End Sub The buildMessage Function in the global.asax

Loop through Generic List

Generic Lists were such a big deal for me.  I love the ability to have all my db items in one place ... in classes in separate data layer and returning neatly to my page when I need them.  Binding Generic Lists to data controls was easy but I had to figure looping through it when the occasion came that I was not directly binding but using the values in other ways.   Example below to Loop through a Generic List '''Class is maindata '''Function that builds my list is getLCOpen - I am passing it three variables For Each x As LC_data.maindata In LC_data.maindata.getLCOpen("", "", 0)           'do something here with data Next  

CLEAR FORM ELEMENTS/loop through page controls

Handy routine for clearing out all of your textbuttons or making all of your panels visible   Sub resetBtn_Click(Sender As Object, E As EventArgs) Dim myForm As Control = Page.FindControl("myform") << put in name of your form on your page Dim ctl As Control For Each ctl In myForm.Controls 'Clears Textbox Types   If ctl.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox") Then              CType(ctl, TextBox).Text = ""   End If   Next ctl End Sub

Generic List Single Item

Working with Generic Lists in a data class has been quite rewarding.  I still occasionally return Datasets but typically everything I need to do can be done using the List items.   It is a breeze to return entire list, datasets or in this case just an integer .... the project is program_data, class is pdMain and public function is getProgramInfo with 2 parameters.  I just want the Fiscal Week value  - function typically returns an entire list of data but I just want the first record and one item - I would do it something like this:   'GENERIC LIST - SINGLE ITEM Dim temp As String = program_Data.pdMain.getProgramInfo("HMD", "")(0).FiscalWeek lblFiscalWeek.Text = temp

Code Flunkie - Everything .Net

New Blog .... just for dotnet snippets. I set it up to put all those little snippets I like to drop in here and there - figured I would share. The pieces of code that you rarely use but never can find when you need it ..... Some of my snippets are old and I will try to revise as I add them.