ASP.net Vb.Net Label.Text not updating - asp.net

In my VB.net ASP application, i have some ASPTREEVIEW where, on page load there is a Label, to which i set some value. After clicking on any row, i catch that value & assign it to the label.
But suddenly that label value is not updating by the chosen value. As if i check it via debugging the value, the value changes, but when i use Lable.text = chosen_value... It sets the value, but it is not actually changing on the html page. HTML shows the old value.
What will be the problem, i am not getting it.
Imports DevExpress
Imports DevExpress.Xpo
Imports DevExpress.Data
Imports DevExpress.Data.Filtering
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Public testvar As Integer = 35
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim xpcol As New XPCollection(Of WCSOEE.WcsGCObject)
Dim GCObj1 As WCSOEE.WcsGCObject
Dim Filter1 As New DevExpress.XtraCharts.DataFilter
Dim SelectedGCObject As WCSOEE.WcsGCObject
Filter1.ColumnName = "Oid"
Filter1.Condition = XtraCharts.DataFilterCondition.Equal
Filter1.Value = Label2.Text
If Label2.Text = "" Then
Label2.Text = 10
End If
For Each Siris As DevExpress.XtraCharts.Series In WebChartControl1.Series
' WebChartControl1.Series(Siris.Name).DataFilters.Item(0).Value = CInt(Label2.Text)
Next
Dim masterKeyvalue As DevExpress.Web.ASPxTreeList.TreeListNode = ASPxTreeList1.FocusedNode
If masterKeyvalue IsNot Nothing Then
SelectedGCObject = masterKeyvalue.DataItem
Else
SelectedGCObject = xpcol.Object(31)
End If
' MsgBox(SelectedGCObject.GCName)
MsgBox(Label2.Text)
If IsPostBack = False Then
Label2.Text = calculatehours(SelectedGCObject.Oid)
End If
End Sub
Protected Function calculatehours(Oid As Integer)
xpocolLogAvailability.Session = DevExpress.Xpo.XpoDefault.Session
Dim logavail As New XPCollection(Of WCSOEE.LogAvailability)
Dim gcobjlist As New XPCollection(Of WCSOEE.WcsGCObject)
gcobjlist.Filter = CriteriaOperator.Parse("[Oid]=?", Oid)
Dim filter As CriteriaOperator
Dim ds As New DataTable
Dim Series As New DevExpress.XtraCharts.Series
Dim Filter1 As New DevExpress.XtraCharts.DataFilter
' Label2.Text = Oid
logavail.Filter = CriteriaOperator.Parse("GCObject.Oid=?", Oid)
Filter1.ColumnName = "Oid"
Filter1.Condition = XtraCharts.DataFilterCondition.Equal
Filter1.Value = Oid
Dim arr1(32) As Long
'For i As Int16 = 1 To 32
' arr1(i) = 0
'Next
'For Each gcobj As WCSOEE.LogAvailability In logavail
' arr1(gcobj.Status) = arr1(gcobj.Status) + gcobj.Duration
'Next= ""
'ds.Columns.Add(New DataColumn("Name", System.Type.GetType("System.Int32")))
For Each Siris As DevExpress.XtraCharts.Series In WebChartControl1.Series
WebChartControl1.Series(Siris.Name).DataFilters.Item(0).Value = Oid
Next
WebChartControl1.RefreshData()
'For i As Int16 = 1 To 32
' ds.Rows.Add(i, arr1(i))
'Next
'ASPxListBox1.DataSource = ds
'ASPxListBox1.DataBind()
Return Oid
End Function
Protected Sub ASPxTreeList1_CustomCallback(sender As Object, e As DevExpress.Web.ASPxTreeList.TreeListCustomCallbackEventArgs) Handles ASPxTreeList1.CustomCallback
End Sub
Protected Sub ASPxTreeList1_FocusedNodeChanged(sender As Object, e As System.EventArgs) Handles ASPxTreeList1.FocusedNodeChanged
Dim masterKeyvalue As DevExpress.Web.ASPxTreeList.TreeListNode = ASPxTreeList1.FocusedNode
Dim SelectedGCObject As WCSOEE.WcsGCObject = masterKeyvalue.DataItem
' MsgBox(SelectedGCObject.GCName)
If IsPostBack Then
Label2.Text = calculatehours(SelectedGCObject.Oid)
End If
MsgBox(IsPostBack)
MsgBox(Label2.Text)
End Sub
End Class

Post some code so we can see exactly what your doing, without seeing anything so far ensure you have an If statement in your page load to only apply the label text if the current request is not a postback other wise the the label will be set to this on every request.
If Not IsPostBack() Then
lblSomeLabel.Text = "Page load text"
End If

Related

VB.Net Dropdownlist Value is always 1

I got a Dropdownlist filled from a Database, using the ID from the Database as ValueField but it just doesn't work
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strConnection As String = "DEMOString"
connection = New OleDbConnection(strConnection)
connection.ConnectionString = strConnection
connection.Open()
Dim dtb As DataTable
Dim strSql As String = "SELECT * FROM Personen"
dtb = New DataTable()
Using dad As New OleDbDataAdapter(strSql, connection)
dad.Fill(dtb)
End Using
dtb.Columns.Add("Fullname", GetType(String), "Vorname + ' ' + Nachname")
ddlName.Items.Clear()
ddlName.DataSource = dtb
ddlName.DataTextField = "Fullname"
ddlName.DataValueField = "sozNr"
ddlName.DataBind()
connection.Close()
End Sub
When I try using ddlName.SelectedItem.Value later i get 1 for every Item.
The Using Code
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dateString As String = tbDate.Text
Dim name As String = ddlName.SelectedItem.Text
Dim des As String = tbDescription.Text
MsgBox(ddlName.SelectedItem.Value)
You don't have to DataBind the DropDownList on every postback if viewstate is enabled(default). That will overwrite all changes like the SelectedIndex. Instead put the code in Page_Load in a Not Is PostBack check:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim strConnection As String = "DEMOString"
connection = New OleDbConnection(strConnection)
connection.ConnectionString = strConnection
connection.Open()
Dim strSql As String = "SELECT * FROM Personen"
Dim dtb As New DataTable()
Using dad As New OleDbDataAdapter(strSql, connection)
dad.Fill(dtb)
End Using
dtb.Columns.Add("Fullname", GetType(String), "Vorname + ' ' + Nachname")
ddlName.DataSource = dtb
ddlName.DataTextField = "Fullname"
ddlName.DataValueField = "sozNr"
ddlName.DataBind()
connection.Close()
End If
End Sub
Side-Note: you should also not reuse the connection. Instead create, initialize and close it in the method where you use it, best by using the Using-statement which also ensures that it get's disposed/closed in case of an error.

ASP VB Stuck on Dynamic Controls, Viewstate, and Postback. Could really use some help to get back on track

I've been reading posts and articles and just getting a little confused and consequently burning through time I don't have at the moment
Can someone look at my code and tell me where I've gone wrong?
Partial Class PayerContacts
Inherits System.Web.UI.Page
Dim connStrDRContacts As String = ConfigurationManager.ConnectionStrings("DRContacts_SQL").ConnectionString
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
navBuild()
End Sub
Protected Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
If IsPostBack Then
LoadContacts(ViewState("objSender"))
End If
End Sub
Private Function navBuild() As Integer
Dim SQLstrDRs As String = "SELECT * FROM DRList"
Dim DbConnDRs As SqlConnection = New SqlConnection(connStrDRContacts)
DbConnDRs.Open()
Dim dtDRsTemp As New DataTable
Dim SQLAdapterDRs As New SqlDataAdapter(SQLstrDRs, DbConnDRs)
SQLAdapterDRs.Fill(dtDRsTemp)
'Loop through each row of the DataView to create HTML table data
Dim NewTableRow As New TableRow
For Each row As DataRow In dtDRsTemp.Rows
'CREATE table with button to display contacts related to client (one to many)
Dim NewTableButton As LinkButton = New LinkButton
NewTableButton.ID = "btnDRName" & NewTableText
NewTableButton.ViewStateMode = UI.ViewStateMode.Enabled
AddHandler NewTableButton.Click, AddressOf LoadContacts
Next
Return 0
End Function
Protected Sub LoadContacts(sender As Object, e As EventArgs)
Dim LoopCount As Integer = 0
Dim SQLstrLoadTable As String = "SELECT * FROM ContactList WHERE DRVendor = '" & sender.Text.ToString & "'"
and so on....
SQLAdapterLoadTable.Fill(dtLoadTableTemp)
Dim NewTableRow As New TableRow
For Each row As DataRow In dtLoadTableTemp.Rows
'CREATE Accordion to display data
NewAccordion.ID = "ContactAccordion" & LoopCount
NewAccordion.Visible = True
blah, blah...
'SET Pane
NewAccordionPane.HeaderContainer.ID = "PaneHeader" & LoopCount
NewAccordionPane.ContentContainer.ID = "PaneContent" & LoopCount
'CREATE button to open ModalPopup to EDIT each record
Dim imgGear As New ImageButton
imgGear.ID = "btnGear" & row!ID.ToString
imgGear.ViewStateMode = UI.ViewStateMode.Enabled
AddHandler imgGear.Click, AddressOf EditRecord
'LOAD Pane
NewAccordionPane.HeaderContainer.Controls.Add(NewHeaderTable)
NewAccordionPane.ContentContainer.Controls.Add(New LiteralControl(NewTableText))
ViewState("objSender") = sender
End Sub
Protected Sub EditRecord(ByVal sender As Object, ByVal e As EventArgs)
'Open ModalPopup to edit record
popup.Show()
pnlAddEdit.Visible = True
End Sub
End Class
The Infinities Loop articles on ViewState and Dynamic Controls should really be read by every Webforms developer: -
http://mocha.mojoskins.com/SharedFiles/Download.aspx?pageid=566&mid=786&fileid=38
http://weblogs.asp.net/infinitiesloop/TRULY-Understanding-Dynamic-Controls-_2800_Part-1_2900_
The examples are in C# but you should be able to figure out what's going on, it's the same base class library after all.

How to transfer CSV file data from a URL address to a gridview in asp.net?

I am new to ASP.net web programming and I am trying to develop a web application that will transfer CSV file data to gridview in asp.net. For example I have below url:
http://sam.sample.com/samp/DATA.CSV
Above url must be transferred in gridview using asp whenever the file has been uploaded,so meaning the date and time of uploading must be displayed also.
I have tried below codes but got an error:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Dim cls As Class1 = New Class1()
Dim webrequest As System.Net.HttpWebRequest
Dim URL As String = "http://sam.sample.com/samp/DATA.CSV"
Dim csvuri As New Uri(URL)
webrequest = DirectCast(System.Net.HttpWebRequest.Create(csvuri), System.Net.HttpWebRequest)
webrequest.UseDefaultCredentials = True
webrequest.PreAuthenticate = True
webrequest.Credentials = New Net.NetworkCredential("ABC", "A123")
If (webrequest.GetResponse().ContentLength > 0) Then
Dim strReader As New System.IO.StreamReader(webrequest.GetResponse().GetResponseStream())
cls.CreateCSVTable(strReader.ReadLine())
While (InlineAssignHelper(SingleLine, strReader.ReadLine())) IsNot Nothing
cls.AddRowCSVTable(SingleLine)
End While
GridView1.DataSource = cls.CSVTable
GridView1.DataBind()
If strReader IsNot Nothing Then
strReader.Close()
End If
End If
Catch ex As System.Net.WebException
MsgBox(ex.ToString)
End Try
End Sub
Public Sub CreateCSVTable(ByVal TableColumnsList As String)
CSVTable = New DataTable("CSVTable")
Dim myDataColumn As DataColumn
Dim ColumnName As String() = TableColumnsList.Split(",")
For i As Integer = 0 To ColumnName.Length - 2
myDataColumn = New DataColumn()
myDataColumn.DataType = Type.GetType("System.String")
myDataColumn.ColumnName = ColumnName(i)
CSVTable.Columns.Add(myDataColumn)
Next
End Sub
Public Sub AddRowCSVTable(ByVal RowValueList As String)
Dim RowValue As String() = RowValueList.Split(","c)
Dim myDataRow As DataRow
myDataRow = CSVTable.NewRow()
For i As Integer = 0 To RowValue.Length - 2
myDataRow(i) = RowValue(i)
Next
CSVTable.Rows.Add(myDataRow)
End Sub
The error was "Cannot find column 0" which points on:
myDataRow(i) = RowValue(i)
How can I resolve this error. Thanks in advance.
I have created a sample solution here: Sample Solution
Hope this helps you.

Handling events of dynamically created controls in asp.net

I have a button which is not created dynamically. When I click on that button the number of lines in a textbox are counted. I store that in the variable called count. Depending on value of count I create buttons in panel.
Up to now it is working properly.
Now the click event of those buttons is not firing.
I have tried to google my question. But everywhere I got the same answer. Create the controls in Page_Init event. But how is it possible? I am getting the value of count from a textfile's lines, and that value of count decides how many button will be created in the panel.
Dim btnAllow As New Button
btnAllow.Text = "Allow"
btnAllow.ID = "btA" & x
AddHandler btnAllow.Click, AddressOf btnAllow_Click
btnAllowList.Add(btnAllow)
tdAllow.Controls.Add(btnAllow)
The code for btnAllow_Click
Protected Sub btnAllow_Click(ByVal sender As Object, ByVal e As System.EventArgs)
For x As Integer = 0 To btnAllowList.Count - 1
If sender.ID.SubString(3) = btnAllowList(x).ID.Substring(3) Then
Dim lineCount = IO.File.ReadAllLines(PendingRequestsPath).Length
Dim reader As New System.IO.StreamReader(DocumentViewersPath)
Dim allLines As New List(Of String)
Do While Not reader.EndOfStream
allLines.Add(reader.ReadLine())
Loop
reader.Close()
Dim DocumentViewerAlreadyExists As Boolean = False
For i As Integer = 0 To lineCount - 1
If ReadLine(i, allLines) = lblRequestorsList(x).Text Then
DocumentViewerAlreadyExists = True
Exit For
End If
Next
If DocumentViewerAlreadyExists = False Then
Dim Writer As System.IO.StreamWriter = IO.File.AppendText(DocumentViewersPath)
Writer.WriteLine(lblRequestorsList(x).Text)
End If
Dim line As String = ""
Dim r As IO.StreamReader = IO.File.OpenText(PendingRequestsPath)
Dim strFile As New ArrayList
While r.Peek <> -1 ' Loop through the file
line = r.ReadLine 'Read the next available line
' Check to see if the line contains what you're looking for.
' If not, add it to an ArrayList
If Not line.Contains(lblRequestorsList(x).Text) Then
strFile.Add(line)
End If
r.Close()
' Because we want to replace the content of the file, we first
' need to delete it.
If IO.File.Exists(PendingRequestsPath) Then
IO.File.Delete(PendingRequestsPath)
End If
' Create a StreamWriter object with the same filename
Dim objWriter As New IO.StreamWriter(PendingRequestsPath, True)
' Iterate through the ArrayList
For Each item As ArrayList In strFile
objWriter.WriteLine(item) ' Write the current item in the ArrayList to the file.
Next
objWriter.Flush()
objWriter.Close()
End While
End If
Next
End Sub
This worked for me:
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Dim NumberOfControls As Integer = 10
Session("CreateControls") = NumberOfControls
End Sub
Protected Sub btnAllow_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'This will be executed when clicking on the newly created buttons.
End Sub
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Session("CreateControls") IsNot Nothing Then
For x As Integer = 0 To Convert.ToInt32(Session("CreateControls"))
Dim btnAllow As New Button
btnAllow.Text = "Allow"
btnAllow.ID = "btA" & x
AddHandler btnAllow.Click, AddressOf btnAllow_Click
Panel1.Controls.Add(btnAllow)
Next
End If
End Sub

ASP.NET Control added to Placeholder lost values right after adding

Working over 5 hours on the following problem:
Private Sub ModulEdit_PreInit(sender As Object, e As EventArgs) Handles Me.PreInit
Dim modulid As Integer = 1
loadeditors(modulid)
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Public Sub loadeditors(modulID As Integer)
PlaceHolder1.Controls.Clear()
Using dbContext As New EntitiesModel()
Dim mps As List(Of ef.Modulparameter) = dbContext.Modulparameters.Where(Function(c) c.ModulID = modulID).ToList
Dim mmid As Int16
If EditMode.Checked = True Then
mmid = RadComboBox3.SelectedValue
End If
Dim mp As ef.Modulparameter
For Each mp In mps
Dim lbl As New Label
lbl.Text = "<BR>" & mp.Name & "<BR>"
PlaceHolder1.Controls.Add(lbl)
Select Case mp.Editor.Name
Case "textbox1line"
Dim con As New TextBox
con.ID = mp.ID
If EditMode.Checked = True Then
Using dbContext2 As New EntitiesModel
Try
Dim mpa As ef.Menu_modul_paramvalue = dbContext2.Menu_modul_paramvalues.Where(Function(c) c.ModulparameterID = mp.ID And c.Menu_modulID = mmid).First
con.Text = mpa.Valuestring
Catch ex As Exception
con.Text = "AAAA"
End Try
End Using
End If
PlaceHolder1.Controls.Add(con)
'RadAjaxManagerProxy1.AjaxSettings.AddAjaxSetting(Panel1, con, Nothing)
'RadAjaxManagerProxy1.AjaxSettings.AddAjaxSetting(con, con, Nothing)
Case "radeditor"
Dim con As New RadEditor
con.ID = mp.ID
con.ToolsFile = "\admin\controls\ToolsFile.xml"
'con.CssFiles.Add("\Content\frenzy\css\frenzy-orange.css")
If EditMode.Checked = True Then
Using dbContext2 As New EntitiesModel
Try
Dim mpa As ef.Menu_modul_paramvalue = dbContext2.Menu_modul_paramvalues.Where(Function(c) c.ModulparameterID = mp.ID And c.Menu_modulID = mmid).First
con.Content = mpa.Valuestring
Catch ex As Exception
con.Content = "BBBB"
End Try
End Using
End If
PlaceHolder1.Controls.Add(con)
'RadAjaxManagerProxy1.AjaxSettings.AddAjaxSetting(Panel1, con, Nothing)
'RadAjaxManagerProxy1.AjaxSettings.AddAjaxSetting(con, con, Nothing)
End Select
Next
End Using
End Sub
I add the control dynamicly, calling the codepart above in pre_init (tryed in load and init too with same result)
The value (text) for the control is there until that line PlaceHolder1.Controls.Add(con)
After the con.text is empty.
The control is added after, but with no value.
Strange, that in the same proc i add another control (label), where the text value is on the page after.
Adding additional info:
the control value (text or content), when debugging the LoadEditors), is allways correctly set. But then on the page both (textbox and radeditor) are empty
The routing is called from pre init, as described in a lot of related posts.
You are calling loadeditors in ModulEdit_Init. Shouldn't this be LoadControls ?
I fixed it myself:
Adding "con.ViewStateMode = System.Web.UI.ViewStateMode.Disabled" before adding control to placeholder
Calling "loadeditors()" in RadComboBox3 too
much probably the problem was, that i loaded editors in page-load or init, which got the correct values, but then the RadComboBox3.SelectedIndexChanged event was called, which overwrote the values somehow
So my answer is not a real answer, but it works now (I hate such: it works, but i dont know why) ;)

Resources