dex express controls how to get ID value from ComboBox - asp.net

I have ID Number and name. I am using a datacontrol and this "Select Number, Name from My Table"
I can see both in the combobox, but if I pick one I can't get any data from it. what do I do?
I have used native .NET and get the ID value using this>
Protected Sub lstWorkList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstWorkList.SelectedIndexChanged
Try
Session("gblWorkerNumber") = Me.lstWorkList.SelectedValue.ToString
but in dev express there is no "post back" and I can't get their example to work either.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If (Not IsCallback) Then
CmbCountry.Value = "Mexico" ' I want to get my Number into a session var
FillCityCombo("Mexico")
End If
End Sub
seems to me like their example just hardcodes "Mexico" in ??? how does that work anyway?
Their Example is on this page:
http://demos.devexpress.com/aspxeditorsdemos/ASPxComboBox/ClientAPI.aspx

this is what I was after, the .Value
Protected Sub cbxWorkerName_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbxWorkerName.SelectedIndexChanged
Session("WorkerNumber") = Me.cbxWorkerName.Value
' in native .NET it is either .selectedindex or .selectedValue one gives the name and the other gives the Number
End Sub

Related

creating event control in vb.net. Multiple controls with the same ID '1' were found

Im trying to create buttons on page load and have event controls to those. Im able to create the buttons but the event doesnt seem to be triggered when the button is clicked instead it throws an error stating multiple controls with id found.I think this has something to do with postback and unique ID creation for the buttons. can some one point me as to what to be added along with this?
Sub createbutton()
Dim but As New Button
but.Text = "save"
but.ID = "but"
AddHandler but.Click, AddressOf Button_Click
Me.form1.Controls.Add(but)
End Sub
The event control for this is as below.
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
' Handle your Button clicks here
MsgBox("done")
End Sub
Im getting the error
Multiple controls with the same ID '1' were found
The subroutine createbutton works on page load as follows.
Public Class Default3
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ' to gen page on load ;)
createbutton()
End Sub
Help is appreciated , Thanks :)
i think you are a guy who worked a lot with VB 6.0
Control array feature is available in VB 6.0
in VB.net everything is depend upon control ID.
if you don't have specific requirement like ID should be same then i suggest pls append prefix and incremental ID would resolve your issue.
Let me know if you have some specific requirements
Thanks
Asif
Corrected Code, Instead of ID it's name which allow uniqueness
Public Class Form1
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
' Handle your Button clicks here
MsgBox("done")
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
createbutton()
End Sub
Sub createbutton()
Dim but As New Button
but.Text = "save"
but.Name = "but"
AddHandler but.Click, AddressOf Button_Click
Me.Controls.Add(but)
End Sub
End Class

Filtering SQL Query using predefined filters

I've pasted my code below. What I'm trying to do is filter a set of data using the day. The 'day' field is stored as the abbreviated day name, so "thu" for Thursday etc. This works perfectly fine, except once you've changed the date filter to something else, the option for the current day doesn't work.
For example, today is Sunday. When the page loads, it filters out all listings that are not for Sunday, as it should. On a small group of LinkButton controls, if you click any other day of the week, it will change and show the option for the selected day, again, as it should do. However, when you try and set the filter to Sunday again, or click the "today" button, it won't change.
Based on my code, can anyone see what is wrong? If it is of any help, my data is on a remote SQL server.
VB Code:
Dim DateVal As Date = DateTime.UtcNow()
Dim DateName As String = DateVal.ToString("ddd")
Protected Sub btnMon_Click(sender As Object, e As EventArgs) Handles btnMon.Click
SqlDataSource1.SelectParameters("FiltDay").DefaultValue = "Mon"
End Sub
Protected Sub btnTue_Click(sender As Object, e As EventArgs) Handles btnTue.Click
SqlDataSource1.SelectParameters("FiltDay").DefaultValue = "Tue"
End Sub
Protected Sub btnWed_Click(sender As Object, e As EventArgs) Handles btnWed.Click
SqlDataSource1.SelectParameters("FiltDay").DefaultValue = "Wed"
End Sub
Protected Sub btnThu_Click(sender As Object, e As EventArgs) Handles btnThu.Click
SqlDataSource1.SelectParameters("FiltDay").DefaultValue = "Thu"
End Sub
Protected Sub btnFri_Click(sender As Object, e As EventArgs) Handles btnFri.Click
SqlDataSource1.SelectParameters("FiltDay").DefaultValue = "Fri"
End Sub
Protected Sub btnSat_Click(sender As Object, e As EventArgs) Handles btnSat.Click
SqlDataSource1.SelectParameters("FiltDay").DefaultValue = "Sat"
End Sub
Protected Sub btnSun_Click(sender As Object, e As EventArgs) Handles btnSun.Click
SqlDataSource1.SelectParameters("FiltDay").DefaultValue = "Sun"
End Sub
Protected Sub btnAll_Click(sender As Object, e As EventArgs) Handles btnToday.Click
SqlDataSource1.SelectParameters("FiltDay").DefaultValue = DateName
End Sub
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
SqlDataSource1.SelectParameters("FiltDay").DefaultValue = DateName
End Sub
TSQL (If it helps)
SELECT Schedule.SlotID, RIGHT('00' + CONVERT(VARCHAR(2), Schedule.StartHr), 2) + ':' + RIGHT('00' + CONVERT(VARCHAR(2), Schedule.StartMin), 2) AS StartTime,
Schedule.ProgrammeID, Schedule.Day, RIGHT('00' + CONVERT(VARCHAR(2), Schedule.EndHr), 2) + ':' + RIGHT('00' + CONVERT(VARCHAR(2), Schedule.EndMin), 2)
AS EndTime, Programmes.ProgrammeName, Programmes.Description, Programmes.PresenterID, Presenters.PresenterName, Presenters.PhotoURL
FROM Presenters CROSS JOIN
Programmes CROSS JOIN
Schedule
WHERE (Schedule.Day = #FiltDay)
maybe it is not 100% related to your issue but you should not handle the datasource initialization on the event of a different control.
your need is to fill the sql parameter so you should handle the Selecting event of the datasource.
on click of your buttons set the proper value for your variable DateName and on selecting of the datasource fill the sql parameter using your variable.
acting this way you can easily spot the actual value fed into the call and identify the issue.

Web Controls Not Talking to Each other in ASP.NET using VB.NET

I have an asp.net page that loads two controls, Control A and Control B. Control A has some generic form submit and clear buttons that trigger click events in its' own code behind which use reflection to call the update function in Control B which has a few input fields. I have debugged this and everything seems to be in order, however; when the update function in control B is called the input fields are not returning a value when using inputname.text or me.inputname.text. Does anyone have any ideas why this is not working? Any guidance would be appreciated.
This is the code in Control A's codebehind which calls the update method in Control B's code behind
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
Try
Dim lctlControl = Session("SelectedQstnCtl")
Dim methodObj = lctlControl.GetType().GetMethod("UpdateGenInfo", BindingFlags.NonPublic Or BindingFlags.Instance)
' Execute UpdateGenInfo method to update the data
methodObj.Invoke(lctlControl, Nothing)
Catch ex As Exception
'TODO: check for concurrency error here
End Try
End Sub
This is the update function in Control B that is being called. The session values are being passed, but the form fields are not.
Protected Sub UpdateGenInfo()
Dim lclUtil As New clUtility
Dim genInfo As New clGenInfo
Try
Dim dt As Integer
'Update Data for 1-2
dt = genInfo.UpdateGenInfo_E1_01_02(Session("ConnStrEP"), Me.varLastUpdate, Session("AppNo"), Session("RevNo"), _
Me.txtPrName.Text, Me.txtPrAddr1.Text, Me.txtPrAddr2.Text, _
Me.txtPrCity.Text, Me.txtPrState.Text, Me.txtPrZip.Text)
Catch ex As Exception
'Display error
lclUtil.DisplayMsg(Me.lblErrMsg, String.Format("Error Location: Sub LoadGenInfo (ctlE1_01_02) {0}", ex.Message))
End Try
End Sub
The most likely cause is that the control instance stored in the session is not the control instance on the current page. For example, if you're storing the control instance in the session when the page is first loaded, and retrieving it on post-back, it will be a different instance.
If you can't give Control A a direct reference to Control B, then change your code to store the reference in the Page.Items collection:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Page.Items("SelectedQstnCtl") = TheSelectedQstnCtl
End Sub
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
Dim lctlControl = DirectCast(Page.Items("SelectedQstnCtl"), YourControlClass)
lctlControl.UpdateGenInfo()
End Sub
I see you are using reflection which might be an overkill for that task.Try referencing the method in the control directly.Make then method UpdateGenInfo public and then reference it like this.
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
Try
Dim lctlControl = CType(Session("SelectedQstnCtl"),YourControlClass)
lctlControl.UpdateGenInfo()
Catch ex As Exception
End Sub
Public Function UpdateGenInfo()
'your code here
Catch ex As Exception
End Try
End Function
This way you can easily trace where your values are getting lost.Let me know how it goes.
Try yet another simple approach working demo here
In control a
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim testb1 = CType(Me.NamingContainer.FindControl("testb1"), testb)
testb1.UpdateGenInfo()
End Sub
In control b
Public Function UpdateGenInfo()
Try
Dim a = Me.TextBox1.Text
Catch ex As Exception
End Try
End Function
Aspx Parent Page
<uc1:testa ID="testa1" runat="server" />
<uc2:testb ID="testb1" runat="server" />
The controls in testb are in an update panel.Try this and let me know if this works.

VB.net/Asp.net loading a control programatically with a public property to be set

I have a vb.net asp application where I'm loading a control (from another control on a page). I want to set a variable between the two controls when it loads.
This is where I load the control:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim AuditControl As Control = LoadControl("~/controls/auditcompanies.ascx")
phCompanies.Controls.Add(AuditControl)
End Sub
On the control that's being loaded i've exposed the item i want to change as a property
Public Property resultType() As String
Get
Return m_resultType
End Get
Set(ByVal value As String)
m_resultType = value
End Set
End Property
Basically all it is doing is setting a parameter for my table adaptor
Public Sub Load_Data()
dtblog = New dsDECorp.ClientInfoDataTable
dtblog = tablog.GetAudits(m_resultType)
For Each rClient As dsDECorp.ClientInfoRow In dtblog
CID = rClient.ClientID
ClientName = rClient.CompanyName
Next
dtblog.Dispose()
End Sub
How do I pass the parameter through the property from the first control to the second when it loads?
Thanks
Tom
I'm a C# guy so forgive me if I make any syntax errors but the following should work for you:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim AuditControl As Control = LoadControl("~/controls/auditcompanies.ascx")
Dim AuditControlType As Type = AuditControl.GetType()
Dim AuditField As FieldInfo = AuditControlType.GetField("resultType")
AuditField.SetValue(AuditControlType, "Your Value")
phCompanies.Controls.Add(AuditControl)
End Sub

ASP.Net page methods execution time monitoring

could anybody advice me, are there any approaches to calculate the methods execution time in asp.net page?
ASP.Net Trace doesn't suit me since it ignores ajax partial postbacks though just they are the tight place.
I have the page event which for some unknown reason executes slowly. Since the logic and event execution chain is rather complicated, I still unable to merely determine the culprit in debugger.
I'd like to measure the execution time of each method throughout the page, but I still see no way but insert redundant code lines (like timeBegin = DateTime.Now.... timeEnd=DateTime.Now = timeBegin etc) in the beginning and ending of each method. It seems to be ugly approach, does anybody know how to do it automatically? Are there tools which measure the methods execution time available?
I am not 100% sure the below works in all cases. So far it does though. Note that Context.Session.Item("CURRENT_WEB_USER") is where I store the current user, and is therefore not readily available as part of the asp.net session variables.
The code hereunder is in replacement of Global.aspx that allows both full page timing and timing of developer code.
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_AcquireRequestState(sender As Object, e As System.EventArgs)
If HttpContext.Current Is Nothing Then Return
If HttpContext.Current.Session Is Nothing Then Return
If TypeOf HttpContext.Current.CurrentHandler Is Page Then
If Not Context.Items.Contains("Request_Start_Time") Then
Dim MyPage As Page = HttpContext.Current.CurrentHandler
AddHandler MyPage.Init, AddressOf PageInitEncountered
AddHandler MyPage.Unload, AddressOf PageUnloadEncountered
Context.Items.Add("Request_Start_Time", DateTime.Now)
Dim User As String = Context.Session.Item("CURRENT_WEB_USER")
Context.Items.Add("Request_User", User )
End If
End If
End Sub
Sub Application_ReleaseRequestState(sender As Object, e As System.EventArgs)
If HttpContext.Current Is Nothing Then Return
If TypeOf Context.CurrentHandler Is Page Then
Dim Page As Page = Context.CurrentHandler
Dim StartDate As Date = Context.Items("Request_Start_Time") : If StartDate = Nothing Then Return
Dim EndDate As Date = Now
Dim StartProc As Date = Context.Items("Page_Init")
Dim EndProc As Date = Context.Items("Page_Unload")
Dim User As String = Context.Items("Request_User")
LogEntry(StartDate, EndDate, StartProc, EndProc, User, Context.Request.Url.ToString)
End If
End Sub
Public Sub PageInitEncountered(sender As Object, e As System.EventArgs)
Context.Items.Add("Page_Init", DateTime.Now)
End Sub
Public Sub PageUnloadEncountered(sender As Object, e As System.EventArgs)
Context.Items.Add("Page_Unload", DateTime.Now)
End Sub
Public Sub LogEntry(StartDate As Date, EndDate As Date, StartProc As Date, EndProc As Date, User As String, PageURL As String)
System.Diagnostics.Debug.WriteLine(" (" & LogEntry.RequestMs & " - " & LogEntry.DurationMs & ") (" & User & ") " & PageURL)
End Sub
This may not be what you are looking for, but RedGate has a product called Performance Profiler that does exactly that. They do have a 15 day trial so you can even download it and try it out on your current issue. IMHO, it's worth every penny.
You can use asp.net performance counters and other things also. But that will not measure a page speed but they will measure a over all application performance.

Resources