Implemeting HTTP Modules in ASP.NET
I have found the following article very useful in getting started with creating/using HTTP Modules.
http://www.15seconds.com/issue/020417.htm
Tuesday, December 13, 2005
Friday, December 02, 2005
Accessing from multiple SourceControl locations.
I recently had to access code from "Sourcegear Vault" and at the same time access from "Visual Sourcesafe". This caused a problem to me as we can have only one default sourcecontrol in Visual Studio. Fortunately, one of my friends directed me to this place ,
where I found the an app that changes the default sourcecontrol in Visual Studio in oneclick.
http://weblogs.asp.net/mlafleur/archive/2003/06/17/8812.aspx
I however have to remember to do it, before I access projects from the respective sourcecode locations.
I recently had to access code from "Sourcegear Vault" and at the same time access from "Visual Sourcesafe". This caused a problem to me as we can have only one default sourcecontrol in Visual Studio. Fortunately, one of my friends directed me to this place ,
where I found the an app that changes the default sourcecontrol in Visual Studio in oneclick.
http://weblogs.asp.net/mlafleur/archive/2003/06/17/8812.aspx
I however have to remember to do it, before I access projects from the respective sourcecode locations.
Wednesday, November 30, 2005
Javascript Issues
I am not sure why, but when I use the following function, if there is only one checkbox, it is not working. When I try to use alert and see the contents of attendeelength, it is returning undefined. Note: This happens only when I have one checkbox by name “attendees”, but if I have multiple checkboxes with same name, code works fine.
function CheckAll()
{
var count=0;
var attendeecheckbox = document.all.item("attendees");
var attendeelength = document.all.item("attendees").length;
while (count != attendeelength) {
attendeecheckbox(count).checked=true;
count++;
}
}
So, my solution was to use the following code instead of the above code. This code works with one or many checkboxes.
function CheckAll()
{
var count=0;
var attendeecheckbox = document.getElementsByName("attendees");
var attendeelength = attendeecheckbox.length;
while (count != attendeelength) {
attendeecheckbox(count).checked=true;
count++;
}
}
I am not sure why, but when I use the following function, if there is only one checkbox, it is not working. When I try to use alert and see the contents of attendeelength, it is returning undefined. Note: This happens only when I have one checkbox by name “attendees”, but if I have multiple checkboxes with same name, code works fine.
function CheckAll()
{
var count=0;
var attendeecheckbox = document.all.item("attendees");
var attendeelength = document.all.item("attendees").length;
while (count != attendeelength) {
attendeecheckbox(count).checked=true;
count++;
}
}
So, my solution was to use the following code instead of the above code. This code works with one or many checkboxes.
function CheckAll()
{
var count=0;
var attendeecheckbox = document.getElementsByName("attendees");
var attendeelength = attendeecheckbox.length;
while (count != attendeelength) {
attendeecheckbox(count).checked=true;
count++;
}
}
Thursday, November 10, 2005


Dynamic Vertical DataGrid
The following is an illustration of how to create a dynamic vertical datagrid. The problem I had was to allow users to enter Goals in one column while displaying the Scores in another column and save the data in to database. The scores were already available in the database.
Also, the number of columns in the datagrid had to be dynamic as the bonus objectives would change every year. The other issue was I had to let users score the Goals they put in another column.
To accomplish this, my solution was to create a dynamic vertical datagrid. The final output looks like the above grid
For this I created a DataGrid Template class as shown below, which is the heart of the solution.
Public Class myDataGridTemplate
Implements ITemplate
Dim templateType As ListItemType
Dim columnName As String
Dim columnControlID As String
WithEvents dgrid As DataGrid
Dim dgridid As String
WithEvents lbl As Label
WithEvents lc As Literal
WithEvents tb As TextBox
WithEvents cb As CheckBox
Sub New(ByVal type As ListItemType, ByVal ColName As String)
templateType = type
columnName = ColName
End Sub
Sub New(ByVal type As ListItemType, ByVal ColName As String, ByVal ColControlID As String)
templateType = type
columnName = ColName
columnControlID = ColControlID
End Sub
Sub New(ByVal type As ListItemType, ByVal ColName As String, ByVal mygrid As DataGrid, ByVal gridid As String)
templateType = type
columnName = ColName
dgrid = mygrid
dgridid = gridid
End Sub
Private Sub InstantiateIn(ByVal container As Control) _
Implements ITemplate.InstantiateIn
Dim lc1 As New Literal
lc = New Literal
lbl = New Label
Select Case templateType
Case ListItemType.Header
lc1.Text = "" & columnName & ""
container.Controls.Add(lc1)
Case ListItemType.Item
If columnName = "innergrid" Then
dgrid.ID = dgridid
container.Controls.Add(dgrid)
Else
lbl.ID = columnControlID
lbl.Width = New Unit(40, UnitType.Pixel)
container.Controls.Add(lbl)
AddHandler container.DataBinding, AddressOf BindData
End If
Case ListItemType.EditItem
If columnName = "Achieved" Then
cb = New CheckBox
cb.ID = columnControlID
cb.Width = New Unit(20, UnitType.Pixel)
container.Controls.Add(cb)
AddHandler container.DataBinding, AddressOf BindCheckBoxContents
Else
tb = New TextBox
tb.ID = columnControlID
tb.Width = New Unit(45, UnitType.Pixel)
container.Controls.Add(tb)
AddHandler container.DataBinding, AddressOf BindTextBoxContents
End If
Case ListItemType.Footer
lc1.Text = "Footer"
container.Controls.Add(lc1)
End Select
End Sub
Private Sub BindData(ByVal sender As Object, ByVal e As EventArgs) Handles lbl.DataBinding
Dim container As DataGridItem = CType(lbl.NamingContainer, DataGridItem)
Dim dview As DataRowView = CType(container.DataItem, DataRowView)
Dim str As String = dview.Item(columnName).ToString()
lbl.Text = str
lbl.ForeColor = Color.DarkSlateGray
Dim istargetlinescore As String = dview.Item("IsTargetlineScore").ToString()
If RTrim(istargetlinescore) = "Y" Then
lbl.BackColor = Color.PeachPuff
End If
End Sub
Private Sub BindTextBoxContents(ByVal sender As Object, ByVal e As EventArgs) Handles tb.DataBinding
Dim container As DataGridItem = CType(tb.NamingContainer, DataGridItem)
Dim dview As DataRowView = CType(container.DataItem, DataRowView)
Dim str As String = dview.Item(columnName).ToString()
tb.Text = str
If RTrim(str) = "0" Then
tb.ForeColor = Color.RoyalBlue
Else
tb.ForeColor = Color.Crimson
End If
tb.Font.Bold = True
Dim istargetlinescore As String = dview.Item("IsTargetlineScore").ToString()
If RTrim(istargetlinescore) = "Y" Then
tb.BackColor = Color.PeachPuff
End If
End Sub
Private Sub BindCheckBoxContents(ByVal sender As Object, ByVal e As EventArgs) Handles cb.DataBinding
Dim container As DataGridItem = CType(cb.NamingContainer, DataGridItem)
Dim dview As DataRowView = CType(container.DataItem, DataRowView)
Dim str As String = dview.Item(columnName).ToString()
If RTrim(str) = "T" Then
cb.Checked = True
Else
cb.Checked = False
End If
End Sub
Private Sub dgrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgrid.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim curRadio As MetaBuilders.WebControls.RowSelectorColumn.ParticipantRadioButton
Dim lblScore As Label
curRadio = CType(e.Item.FindControl("RowSelectorColumnSelector"), MetaBuilders.WebControls.RowSelectorColumn.ParticipantRadioButton)
lblScore = CType(e.Item.FindControl("Label2"), Label) 'hardcoded the controlid
If (Not (curRadio Is Nothing)) And (Not (lblScore Is Nothing)) Then 'do this radio checking and color change only if the concerned controls are found in the grid
Dim container As DataGridItem = CType(curRadio.NamingContainer, DataGridItem)
Dim dview As DataRowView = CType(container.DataItem, DataRowView)
Dim str As String = dview.Item("Achieved").ToString()
If RTrim(str) = "T" Then
curRadio.Checked = True
lblScore.ForeColor = Color.Crimson
lblScore.BackColor = Color.Yellow
Else
curRadio.Checked = False
lblScore.ForeColor = Color.RoyalBlue
End If
Dim lblGoal As Label
lblGoal = CType(e.Item.FindControl("Label1"), Label) 'hardcoded the controlid
If RTrim(lblGoal.Text) = "0" Then
curRadio.Disabled = True
Else
curRadio.Disabled = False
End If
End If
End If
End Sub
End Class
The .NET code to build the dynamic DataGrid is as follows.
Public Sub BuildMainGrid()
PlaceHolder1.Controls.Clear()
Dim MainGrid As New DataGrid
MainGrid.ID = "MainGrid1"
Dim dl As BM_Datalogic = New BM_Datalogic
MainGrid.AutoGenerateColumns = False
Dim curempid As String
curempid = “123456789”
Dim iyear As Integer = “2006”
Dim strempid As String = “123456789”
Dim strname As String =”Orlando Quiroz”
Dim strunit As String = “4555”
Dim strjobtitle As String = “CHEF”
Dim gpds As DataSet = dl.FetchBM_GlobalParams(iyear)
Dim mdvDisplayFields As DataView = gpds.Tables(0).DefaultView
Dim i As Integer
Dim dr As DataRow
Dim tc As TemplateColumn
Dim spds As DataSet = New DataSet
'For each row in the global_params resultset...Fetch the subparams for each globalparam ***
For i = 0 To mdvDisplayFields.Count - 1
dr = mdvDisplayFields.Item(i).Row
spds = dl.FetchEmployeeSubParams(iyear, strunit, strempid, strjobtitle, dr.Item("Global_ParamNo"))
Dim mdvSubDisplayFields As DataView = spds.Tables(0).DefaultView
Dim spdr As DataRow
Dim spi As Integer
Dim no_o_subparams As Integer = mdvSubDisplayFields.Count
'For each row in subparams...add a template column and set the header text
For spi = 0 To no_o_subparams - 1
spdr = mdvSubDisplayFields.Item(spi).Row
tc = New TemplateColumn
If no_o_subparams = 1 Then
tc.HeaderText = "
" & CStr(spdr.Item("SubParamDescription")) & " (" & CStr(spdr.Item("Sub_ParamWeightage")) & "%) |
"
Else
If spi = 1 Then
tc.HeaderText = "
" & CStr(dr.Item("Global_ParamDesc")) & "(" & CStr(dr.Item("Global_Param_Weightage")) & "%)" & " |
" & CStr(spdr.Item("SubParamDescription")) & "(" & CStr(spdr.Item("Sub_ParamWeightage")) & "%) |
"
Else
tc.HeaderText = "
" & CStr(spdr.Item("SubParamDescription")) & "(" & CStr(spdr.Item("Sub_ParamWeightage")) & "%) |
"
End If
End If
'For each row in the ***subparams resultset, build a goal,score datagrid and set the same itemtemplate to that grid.
Dim global_paramno As Integer = dr.Item("Global_ParamNo")
Dim sub_paramno As Integer = spdr.Item("Sub_ParamNo")
Dim goalscoregrid As DataGrid = New DataGrid
goalscoregrid = FetchTemplatedInnerGrid(iyear, strunit, Trim(strempid), Trim(strjobtitle), global_paramno, sub_paramno)
tc.ItemTemplate = New ValleyBonusMatrix.myDataGridTemplate(ListItemType.Item, "innergrid", goalscoregrid, "DataGrid1" & global_paramno & sub_paramno)
goalscoregrid.DataBind()
MainGrid.Columns.Add(tc)
Next
Next
MainGrid.DataSource = gpds.Tables(0)
PlaceHolder1.Controls.Add(MainGrid)
MainGrid.DataBind()
dl.closeconnection()
End Sub
Private Function FetchTemplatedInnerGrid(ByVal iyear As Integer, ByVal strunit As String, ByVal strempid As String, ByVal strjobtitle As String, ByVal paramno As Integer, ByVal subparamno As Integer) As DataGrid
Dim DataGrid1 As New DataGrid
DataGrid1.ID = "DataGrid1" & paramno & subparamno **This is important for saving
DataGrid1.AutoGenerateColumns = False
Dim dl As BM_Datalogic = New BM_Datalogic
Dim bpds As DataSet = dl.FetchBM_BonusPotential(iyear, strunit, Trim(strempid), Trim(strjobtitle))
Dim gpds As DataSet = dl.FetchBM_GoalScoreConfig(iyear, strunit, Trim(strempid), Trim(strjobtitle), paramno, subparamno)
dl.closeconnection()
DataGrid1.DataSource = gpds.Tables(0).DefaultView
Dim tc1 As New TemplateColumn
Dim curcolname As String = "Goal"
tc1.HeaderTemplate = New _
ValleyBonusMatrix.myDataGridTemplate(ListItemType.Header, curcolname)
tc1.ItemTemplate = New _
ValleyBonusMatrix.myDataGridTemplate(ListItemType.EditItem, curcolname, "TextBox1")
DataGrid1.Columns.Add(tc1)
Dim tc2 As New TemplateColumn
Dim curcolname1 As String = "Score"
tc2.HeaderTemplate = New _
ValleyBonusMatrix.myDataGridTemplate(ListItemType.Header, curcolname1)
tc2.ItemTemplate = New _
ValleyBonusMatrix.myDataGridTemplate(ListItemType.Item, curcolname1, "Label1")
DataGrid1.Columns.Add(tc2)
If bpds.Tables(0).Rows.Count > 0 Then
lblBonusPotential.Text = bpds.Tables(0).Rows(0).Item("BonusPotential").ToString
Else
lblBonusPotential.Text = 0
End If
Return DataGrid1
End Function
I call the BuildMainGrid function on my page load as follows:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
BuildMainGrid()
End Sub
Note: I have included some of my project specific datalayer calls, which I marked in plum. You will have to replace that with your own logic or information. If saving is important to you, naming the dynamic grids unique is important. I came up with my own unique naming rules. See Red underline in FetchTemplatedInnerGrid function. You may use a different parameter, specific to your project to achieve the same.
Code to save the data from this datagrid is as follows:
Public Sub SaveGridData()
ErrLabel.Text = ""
Dim dl As New BM_Datalogic
Dim vpe As ValleyPE.clsSecurity = New ValleyPE.clsSecurity
Try
Dim iyear As Integer = ddlYear.SelectedItem.Value
Dim strempid As String = ddlEmployee.SelectedItem.Value
Dim strunit As String = txtUnit.Text
Dim struserid As String = vpe.UserName(Request)
Dim strjobtitle As String = ddlJobTitle.SelectedItem.Value
Dim dBonusPotential As Double = Convert.ToDouble(lblBonusPotential.Text)
Dim i As Integer
Dim j As Integer
Dim maing As DataGrid = New DataGrid
maing = CType(PlaceHolder1.FindControl("MainGrid1"), DataGrid)
Dim dgi As DataGridItem
Dim griditemcount As Integer = maing.Items.Count
'the second item in the maingrid holds the inner grids.
dgi = maing.Items(griditemcount - 1)
Dim gpds As DataSet = dl.FetchBM_GlobalParams(iyear)
Dim dr As DataRow
For Each dr In gpds.Tables(0).Rows
Dim paramno As Integer = dr.Item("Global_ParamNo")
Dim spds As DataSet = dl.FetchBM_SubParams(iyear, paramno)
Dim spdr As DataRow
'For each subparam of the current globalparamno
For Each spdr In spds.Tables(0).Rows
Dim subparamno As Integer = spdr.Item("Sub_ParamNo")
Dim innergrid1 As DataGrid = CType(dgi.FindControl("DataGrid1" & paramno & subparamno), DataGrid)
Dim dginneritem As DataGridItem
Dim inneri As Integer
Dim iscore As Double
Dim igoal As Double
Dim lblscore As Label
Dim txtgoal As TextBox
For inneri = 0 To innergrid1.Items.Count - 1 'this contains the actual goal score contents
dginneritem = innergrid1.Items(inneri)
txtgoal = CType(dginneritem.FindControl("TextBox1"), TextBox)
lblscore = CType(dginneritem.FindControl("Label1"), Label)
iscore = Convert.ToDouble(lblscore.Text)
igoal = Convert.ToDouble(txtgoal.Text)
'Update Goal Score Config here
dl.SaveBM_ScoresConfig(iyear, strunit, strempid, strjobtitle, paramno, subparamno, igoal, iscore, struserid)
Next
Next
Next
'Save Goal Submission Information in Submission Table.
dl.SaveGoalsSubmissionInfo(iyear, strunit, strempid, strjobtitle, dBonusPotential, struserid)
BuildMainGrid()
dl.closeconnection()
Catch ex As Exception
dl.closeconnection()
ErrLabel.Text = ex.Message
ErrLabel.Visible = True
Exit Sub
End Try
End Sub
Subscribe to:
Posts (Atom)