G'day,
I have a RadComboBox control inside of a RadGrid that is displayed when the InitInsert action occurs. I'm using Entity Framework as a datasource & the results contained within this are correct. My problem is that when I use findcontrol it returns nothing.
If e.CommandName = "InitInsert" Then
RadGrid1.MasterTableView.InsertItemDisplay = Telerik.Web.UI.GridInsertItemDisplay.Bottom
Dim query = From myTable In dbEntity.myTables Select myTable.Name, myTable.ID
Dim mineCompBox = CType(e.Item.FindControl("mineCompBox"), RadComboBox)
mineCompBox.DataSource = mineCompQuery
mineCompRadBox.DataTextField = "Name"
mineCompRadBox.DataValueField = "Id"
mineCompRadBox.DataBind()</code>
I'm having trouble finding any answers that reference FormTemplate without it being an edit form. What am I missing? :-(
Thanks.
I don't have my computer in front of me to test it but I'm pretty sure that either:
1) You are not looking at the right controls collection
2) or the RadComboBox is not create yet or has been created but the ID doesn't match
so you cannot find it
Again not 100% sure. Maybe this can help you its a complete example: http://beecy.net/post/2009/01/07/telerik-radgrid-formtemplate-codebehind.aspx (maybe check you markup against this one)
My problem was solved by using an ItemCreated command. An example can be found here:
http://www.telerik.com/community/forums/aspnet-ajax/grid/find-controls-when-using-editcommand.aspx
The code for my situation was:
Private Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles RadGrid1.ItemCreated
If TypeOf e.Item Is GridEditFormItem AndAlso e.Item.IsInEditMode Then
Dim dbEntity As WebsiteEntities = New WebsiteEntities
Dim myQuery = From myTable In myTables Select myTable.Name, myTable.ID
Dim EditFormItem As GridEditFormItem = DirectCast(e.Item, GridEditFormItem)
Dim myCombobox As RadComboBox = DirectCast(EditFormItem.FindControl("radDropBox"), RadComboBox)
myCombobox.DataSource = myQuery
myCombobox.DataTextField = "Name"
myCombobox.DataValueField = "ID"
myCombobox.DataBind()
End If
End Sub
Related
I'm having a bit of trouble with ASP Repeaters and trying to sort the data.
So on Page_Load I obtain the datasource as below...
Protected Overloads Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not (Page.IsPostBack) Then
'No need to re-load Occasion because it's done in the PreLoad event
Me.Header.Title = "OCP - " + Occasion.Checklist.name
pnlStatistics.Visible = Not (Occasion.isClosed)
pnlClose.Visible = SecurityHelper.HasRole(SecurityMatchOption.AtLeast, SecurityRole.Manager, _relevantTeam) And Not (Occasion.isClosed)
'initially assume checklist can be closed. any un-signed off task in the item_databound event will disable it.
btnClose.Enabled = True
'Fix Issue 63: rptTask.DataSource = _db.vw_tasklists.Where(Function(o) o.occasionId = Occasion.id).ToList()
rptTask.DataSource = _db.vw_tasklists.Where(Function(o) o.occasionId = Occasion.id).OrderBy(Function(t) t.taskDueDate).ThenBy(Function(t) t.taskDueTime)
However within ItemDataBound we recalculate the task duedate.
Private Sub rptTask_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles rptTask.ItemDataBound
' Get the data relating to this task 'row'
Dim t = CType(e.Item.DataItem, vw_tasklist)
If e.Item.ItemType = ListItemType.Header Then
Dim thDueDate = CType(e.Item.FindControl("thDueDate"), HtmlTableCell)
thDueDate.Visible = Not (Occasion.isClosed)
End If
'securable buttons
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
' Dynamically create a span element named TASKnnnn, which will be referenced from
' 'child page' links back to this page in order to vertically reposition at the selected task
Dim span = New HtmlGenericControl("span")
span.ID = "TASK" & t.taskId
span.ClientIDMode = ClientIDMode.Static ' prevent ASP.NET element ID mangling
e.Item.FindControl("lblTaskName").Parent.Controls.AddAt(0, span) ' hook it into the repeater row
Dim btnSignoff = CType(e.Item.FindControl("btnSignOff"), Button)
Dim btnComment = CType(e.Item.FindControl("btnComment"), Button)
Dim btnAmend = CType(e.Item.FindControl("btnAmend"), Button)
Dim btnView = CType(e.Item.FindControl("btnView"), Button)
Dim lblSoTime = CType(e.Item.FindControl("lblSOTime"), Label)
Dim lblDueDate = CType(e.Item.FindControl("lblDueDate"), Label)
Dim lblTaskId = CType(e.Item.FindControl("lblTaskId"), Label)
Dim lblTaskName = CType(e.Item.FindControl("lblTaskName"), Label)
lblTaskId.Text = CType(t.taskId, String)
lblTaskName.Text = t.taskName
Dim time = (If(t.taskDueTime Is Nothing, New TimeSpan(0, 23, 59, 59), TimeSpan.Parse(t.taskDueTime)))
Dim dueDateTime As DateTime = (Occasion.started.Date + time)
'Setup up due DateTime for Daily Tasks
Select Case DirectCast(t.taskDayTypeId, helpers.Constants.enDayType)
Case helpers.Constants.enDayType.Daily
lblDueDate.Text = dueDateTime.ToString("dd/MM/yyyy HH:mm")
Exit Select
Case Else
'Calculate the actual due date for non-daily tasks
Dim calculator = New Calculator()
Dim calId = t.taskCalendarId
Dim taskMonthDay = "1"
If Not t.taskMonthDayId Is Nothing Then
taskMonthDay = CType(t.taskMonthDayId, String)
End If
Dim monthDay = _db.MonthDays.First(Function(m) m.id = CInt(taskMonthDay))
Dim calendar As Model.Calendar = Nothing
If Not calId is Nothing Then
calendar = _db.Calendars.First(Function(x) calId.Value = x.id)
End If
Dim potDate = calculator.GetActualDueDate(dueDateTime, monthDay.monthDay, t, calendar)
dueDateTime = (potDate.Date + time)
lblDueDate.Text = dueDateTime.ToString("dd/MM/yyyy HH:mm")
Exit Select
End Select
Therefore once the data is displayed in the repeater the sorting is wrong. I need to be able to sort the data after the due date re-calculation. How is this possible?
Thanks
You can to move the ItemDataBound logic up into the original query:
rptTask.DataSource = _db.vw_tasklists.
Where(Function(o) o.occasionId = Occasion.id).
Select(
Function(r)
'Fill in the logic here so the DueProperty has your real Due Date
New With {.Row = r, .DueDate = r.TaskDueDate + r.TaskDueTime}
End Function
OrderBy(Function(t) t.DueDate). ' Now we only need one OrderBy (the time is already included).
Select(Function(r) r.Row) 'But we do want to select back to the original record for the databinding
'And the ToList() was probably NEVER needed or helpful in this situation
Moreover, since this looks like linq-to-sql I might break that up a bit, so we cleanly separate what we expect to execute on the database from what we expect to execute on the web server:
'This runs on the databsae
Dim sqlData = _db.vw_tasklists.
Where(Function(o) o.occasionId = Occasion.id)
'This runs on the web server
rptTask.DataSource = sqlData.
Select(
Function(r)
'Fill in the logic here so the DueProperty has your real Due Date
New With {.Row = r, .DueDate = r.TaskDueDate + r.TaskDueTime}
End Function
OrderBy(Function(t) t.DueDate). ' Now we only need one OrderBy (the time is already included).
Select(Function(r) r.Row) 'But we do want to select back to the original record for the databinding
'And the ToList() was probably NEVER needed or helpful in this situation
Of course, you'll get the best results if you start putting information into the database such that you can effectively sort your data correctly there at the outset.
I would like to add a new row to my HTML table and delete the row dynamically using VBScript. Iam new to this can anyone guide me how to do this.
Note that this will be Internet Explorer specific. Might not work on other browsers/
Sub AddRow()
Dim objTable : Set objTable = window.document.getElementById("tableid")
Dim objRow : Set objRow = objTable.insertRow()
Dim intCount, objCell
For intCount = 0 To 2
Set objCell = objRow.insertCell()
objCell.innerHTML = "Content for cell")
Next
End Sub
For delete use
window.document.getElementById("tableid").deleteRow(oRow.rowIndex);
Trying to use checkbox lists in (calendar booking system). The checkbox should be disabled and red if there are any data in the database against the date and hour. This all work perfectly here is the code. Using vb.net
OK i found a way how to clear the checkboxes
Dim i As Integer
Dim chboxItem As ListItem
For Each chboxItem In CheckBoxListMon.Items
i += 1
If (i Mod 1 = 0) Then
chboxItem.Enabled = True
End If
Next
Protected Sub Page_LoadComplete(sender As Object, e As EventArgs) Handles Me.LoadComplete
Try
strQuery = "SELECT BookingDate, checkBoxItem, BookRegUserID,Booked FROM bookings INNER JOIN checkboxitems where checkBoxItem = BookingTime"
MySQLCmd = New MySqlCommand(strQuery, dbCon)
dbCon.Open()
DR = MySQLCmd.ExecuteReader
While DR.Read
bookDate = DR.Item("BookingDate")
bookTime = DR.Item("checkBoxItem")
bookRegID = DR.Item("BookRegUserID")
booked = DR.Item("Booked")
Dim test As String = bookTime.ToString()
Select Case True
Case bookDate = lblMonday.Text And CheckBoxListMon.Items.FindByValue(test) IsNot Nothing
CheckBoxListMon.Items.FindByValue(bookTime).Enabled = False
CheckBoxListMon.Items.FindByValue(bookTime).Attributes.Add("Style", "color: red;")
End Select
End While
DR.Close()
dbCon.Close()
Catch ex As Exception
End Try
End Sub
When the page load it would not change the ones from the database. But when i reload the page it will actually work perfect.
Where can i put the check just to be sure that they are already in the memory.
Any help will be much appreciated. Thanks all.
Petr
You need to refresh the data when another week is selected because you are using the same controls for each week. You should be able to do this in whatever control you are using to toggle through the weeks.
Page_LoadComplete can only be expedted to fire each time a page has completed loading, that is why your controls work when going to another page and back.
I have a dropdown list on my page (ddlProgram) which is populated via a database query like so:
Using dbContext as IRFEntities = New IRFEntities
Dim getPrograms = (From p in dbContext.IRF_Program _
Order By p.name _
Select p)
ddlProgram.DataSource = getPrograms
ddlProgram.DataTextField = "name"
ddlProgram.DataValueField = "id"
ddl.Program.DataBind()
End Using
So, for example, one might have a DataTextField of "Education" and an ID of "221".
Now, I prepopulate the form with information about the individual visiting the site (if available) - including the dropdown list like so:
If getProspect IsNot Nothing Then
If getProspect.user_id Is Nothing Then
ddlProgram.SelectedValue = getProspect.Program
End If
End If
The Program property contains a number that matches the ID of a Program. So, for example, this individual might have a Program of "221" which would match the "221" of Education mentioned above.
Currently the application successfully sets the SelectedValue to "221" for the DropDownList (ddlProgram), but the SelectedItem of the DDL remains the same (e.g., if it is initially "History" with an ID of "1" after the prepopulation it is "History" with an ID of "221").
What I'm trying to make happen is that the SelectedItem is updated to item which corresponds with the SelectedValue. So, in the end, if the individual has "221" for "Education" selected when the form is prepopulated they would see Education as the selected item and the selected value would be set correctly, whereas right now the form is showing the wrong SelectedItem but has the right SelectedValue behind the scenes.
Here is a more complete idea of the code flow from the Page_Load event:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack = False Then
' If prospect is coming from unique url
Dim prospect_url As String = Page.RouteData.Values("value")
' Save prospect_url into session variable
Session("prospect_url") = prospect_url
Using dbContext As IRFEntities = New IRFEntities
' Prepopulate the programs dropdown.
Dim getPrograms = (From p In dbContext.IRF_Program _
Order By p.name _
Select p)
ddlProgram.DataSource = getPrograms
ddlProgram.DataTextField = "name"
ddlProgram.DataValueField = "id"
ddlProgram.DataBind()
End Using
Using dbContext As IRFEntities = New IRFEntities
' Prepopulate the states dropdown.
Dim getStates = (From p In dbContext.IRF_States _
Order By p.name _
Select p)
ddlState.DataSource = getStates
ddlState.DataTextField = "name"
ddlState.DataValueField = "id"
ddlState.DataBind()
End Using
Using dbContext As IRFEntities = New IRFEntities
' Grab info. about prospect based on unique url.
Dim getProspect = (From p In dbContext.IRF_Prospects _
Where p.url = prospect_url _
Select p).FirstOrDefault
' If they have a record...
If getProspect IsNot Nothing Then
If getProspect.user_id Is Nothing Then
' Prepopulate the form with their information.
' These must have a value, so we need to make sure that no column is null in the database.
ddlProgram.SelectedValue = getProspect.program
txtFirst.Text = getProspect.first_name
txtLast.Text = getProspect.last_name
txtAddress.Text = getProspect.address
txtCity.Text = getProspect.city
ddlState.SelectedValue = getProspect.state
txtZip.Text = getProspect.zip
txtPhone.Text = getProspect.phone
txtEmail.Text = getProspect.email_address
txtYearEnrolling.Text = getProspect.enrolling_in
Else
' Redirect them to login.
Response.Redirect("login.aspx")
End If
End If
End Using
End If
End Sub
What you're doing looks like it should work. If you put a breakpoint after the setting of the value and check the SelectedItem text and value, do they appear as expected or mismatched?
Use the Immediate Window to check:
ddlProgram.SelectedItem.Text
ddlProgram.SelectedItem.Value
If they appear the same then I would presume the binding code is being refired and the list is being regenerated with the first item being selected.
To check this put a break point on the binding code and see if it is fired more than once and correct the order of the methods appropriately.
ADDED:
If it works on your local environment it should work when published, if the code is the same? Looking at your code, I'd start by seperating out some of the databinding code into seperate methods rather than have everything in Page_Load, one becuase it's good practice and two because it will make debugging easier. Further than that I'm not sure what else to suggest.
I am new to ASP.NET. I am trying to display my sql results using a list view. I am using the example for grouping my results by a data field from the 4GuysFromRolla.com website. However, I find the way of grouping the items by a data field to be a bit clumsy. Is there a better way to do it?
Thanks.
Nested ListView - http://mattberseth.com/blog/2008/01/building_a_grouping_grid_with.html
I have never used a ListView, but I did do grouping in a GridView. You can try porting this over to a ListView if you want:
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
Dim tblGrid As Table = Me.GridView1.Controls(0)
Dim strLastCat As String = "#"
Dim row As GridViewRow
For Each row In GridView1.Rows
Dim intRealIndex As Integer = tblGrid.Rows.GetRowIndex(row)
Dim strCat As String = Me.GridView1.DataKeys(row.RowIndex).Value
If strLastCat <> strCat Then
Dim rowHeader As New GridViewRow(intRealIndex, intRealIndex, DataControlRowType.Separator, DataControlRowState.Normal)
Dim newCell As New TableCell
newCell.ColumnSpan = Me.GridView1.Columns.Count
newCell.BackColor = System.Drawing.Color.FromArgb(61, 138, 20)
newCell.ForeColor = System.Drawing.Color.FromArgb(255, 255, 255)
newCell.Font.Bold = True
newCell.Font.Size = New FontUnit(FontSize.Larger)
newCell.Text = strCat
rowHeader.Cells.Add(newCell)
tblGrid.Controls.AddAt(intRealIndex, rowHeader)
strLastCat = strCat
End If
Next
MyBase.Render(writer)
End Sub
The code creates headers each category. The final version can be viewed here: http://www.truedietreviews.com/diet-reviews/