Thanks in advance for looking at my post. I'm new to cookies. I figured they'd be easier to work with if I create a property for each key of the collection. I created a set of private properties to deal with getting/setting cookies so I keep control state between pages. Here's what I have:
Private Property CK_Settings(pKey As String) As String
Set(value As String)
If Request.Cookies("Settings") Is Nothing Then
Response.Cookies.Add(New HttpCookie("Settings"))
End If
Response.Cookies("Settings").Item(pKey) = value
End Set
Get
If Request.Cookies("Settings") Is Nothing Then
Response.Cookies.Add(New HttpCookie("Settings"))
End If
Return Request.Cookies("Settings").Item(pKey)
End Get
End Property
Private Property CK_rb1 As String
Set(value As String)
CK_Settings("rb1") = value
End Set
Get
If CK_Settings("rb1") IsNot Nothing Then
Return CK_Settings("rb1")
Else
Return Nothing
End If
End Get
End Property
Private Property CK_Jobs As String
Set(value As String)
CK_Settings("Jobs") = value
End Set
Get
If CK_Settings("Jobs") IsNot Nothing Then
Return CK_Settings("Jobs")
Else
Return Nothing
End If
End Get
End Property
Private Property CK_rb2 As String
Set(value As String)
CK_Settings("rb2") = value
End Set
Get
If CK_Settings("rb2") IsNot Nothing Then
Return CK_Settings("rb2")
Else
Return Nothing
End If
End Get
End Property
Private Sub SetCookies()
Me.CK_rb1 = Me.rb1.SelectedIndex.ToString
Me.CK_Jobs = Me.ddlJobs.SelectedIndex.ToString
Me.CK_rb2 = Me.rb2.SelectedIndex.ToString
End Sub
Private Sub GetCookies()
If Me.CK_rb1 IsNot Nothing Then
Me.rb1.SelectedIndex = Me.CK_rb1.ToInteger
End If
If Me.CK_rb2 IsNot Nothing Then
Me.rb2.SelectedIndex = Me.CK_rb2.ToInteger
End If
Me.ddlJobs.SelectedIndex = Me.CK_Jobs.ToInteger
End Sub
I get no compiler/runtime errors, but the cookies aren't being set at all. I've also added 2 subs for easier setting/getting. I found this when researching online, but I don't think it's relevant. ANY help at all is GREATLY appreciated!
UPDATE
I was able to get the radiobuttonlist (rb) to retain state. I'm having issues with the dropdownlist (ddl) retaining state. It's worth noting that the dropdownlist is a custom control. Also, I changed the names to something less of consequence.
The dropdownlist implemented overridden method that I was accounting for. As soon as I figured this out, I was able to get the ddl to retain state.
Related
Private Sub GetGeneralMemberInformation(Member As WebServices.MemberInfoService.Member)
Try
Dim error_msg As String = "The following Member is not Eligible to display the data: {0} "
SubscriberId = Member.SubscriberId
If Member.Eligibility(0) Is Nothing Then
Throw New Exception(String.Format(error_msg, SubscriberId))
End If
Catch ex As Exception
Throw ex
End Try
End Sub
I'm unable to check do null check on the Eligibility object .while checking itself i'm getting the error like index is outside the bounds of the array . Can any body suggest me how to do null check and i want display mu custom error message instead of that one and here eligibility object don't have any data.
As others have noted, Eligibility may be empty. Since you clarified that Eligibility is a List(Of Object), I updated the code sample below to reflect that.
Imports System
Imports System.Collections.Generic
Public Module Module1
Public Sub Main()
Dim member1 = New Member()
member1.Eligibilities = New List(Of MemberEligibility)
' REMARKS: List is empty
If member1.Eligibilities.Count > 0 Then
If member1.Eligibilities(0) Is Nothing Then
Console.WriteLine("First element is NULL")
End If
End If
' REMARKS: Add one element to list
member1.Eligibilities.Add(New MemberEligibility())
' REMARKS: List contains one element
If member1.Eligibilities.Count > 0 Then
If member1.Eligibilities(0) IsNot Nothing Then
Console.WriteLine("First element is initialized")
End If
End If
End Sub
End Module
Public Class Member
Private _eligibility As MemberEligibility
Private _eligibilities As List(Of MemberEligibility)
Public Sub New()
End Sub
Public Property Eligibility As MemberEligibility
Get
Return Me._eligibility
End Get
Set
Me._eligibility = Value
End Set
End Property
Public Property Eligibilities As List(Of MemberEligibility)
Get
Return Me._eligibilities
End Get
Set
Me._eligibilities = Value
End Set
End Property
End Class
Public Class MemberEligibility
Public Sub New()
End Sub
End Class
Here is a .NET Fiddle showing the above code in action.
I am delving into territory unfamiliar to me. I'm creating a User Control that I'd like to use across multiple forms. (thus reason for User Control). I am only familiar with VB.NET language. I've googled and I think I'm close to understanding, but I just can't figure this out. I've got multiple buttons on my User Control. When the "upload" button is pressed on my user control, I'd like it to run a Sub on my parent page called UploadFile(), which has one string parameter. I will have the UserControl on my page mulitple times like this:
<uc1:UploadFile ID="UploadFile1" runat="server" />
<uc1:UploadFile ID="UploadFile2" runat="server" />
In my code behind of the parent page, I'd like to execute this function on the Click event of the upload button within the user control. Where the unique parameters will then tell me what folders I will be uploading my file to. So that when user uploads a file within my first User Control on the page, it will run my Upload Sub on my parent page, take the parameter "a" and run my Sub that uploads my document and saves to database for files specific to "a" type. I will then tell the user control to change it's label to show that File A has been uploaded
Sub something somethingUploadFile1_Click
UploadFile("a")
End Sub
Sub something somethingUploadFile2_Click
UploadFile("b")
End Sub
Sub UploadFile(ByVal myString as string)
Select Case myString
Case "a"
If UploadFile1.FileUpload.HasFile Then
'run code to upload file
'display label
UploadFile1.lblReplaceMsg.Text = "File " & myString & " has been uploaded."
End If
Case "b"
If UploadFile2.FileUpload.HasFile Then
'run code to upload file
'display label
UploadFile2.lblReplaceMsg.Text = "File " & myString & " has been uploaded."
End If
End Select
End Sub
I know that in above code I'm supposed to be somehow referencing the button on my UserControl, but I'm not sure how to do it. I also know that Raising Events and Event Delegation are two major components of this, but again, I'm just not figuring it out. Can you please show me how to complete my code using the existing examples I provided?
Here is the complete code behind of my existing UserControl:
Public Class UploadFile
Inherits System.Web.UI.UserControl
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub btnUpload_Click(sender As Object, e As EventArgs) Handles btnUpload.Click
End Sub
Protected Sub btnDelete_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles btnDelete.Click
End Sub
Public Property lblViewFile() As Label
Get
Return _lblViewFile
End Get
Set(ByVal value As Label)
_lblViewFile = value
End Set
End Property
Public Property btnDelete() As ImageButton
Get
Return _btnDelete
End Get
Set(ByVal value As ImageButton)
_btnDelete = value
End Set
End Property
Public Property pnlUpload() As Panel
Get
Return _pnlUpload
End Get
Set(ByVal value As Panel)
_pnlUpload = value
End Set
End Property
Public Property FileUpload() As FileUpload
Get
Return _FileUpload
End Get
Set(ByVal value As FileUpload)
_FileUpload = value
End Set
End Property
Public Property btnUpload() As Button
Get
Return _btnUpload
End Get
Set(ByVal value As Button)
_btnUpload = value
End Set
End Property
Public Property lblReplaceMsg() As Label
Get
Return _lblReplaceMsg
End Get
Set(ByVal value As Label)
_lblReplaceMsg = value
End Set
End Property
Private _lblViewFile As Label
Private _btnDelete As ImageButton
Private _pnlUpload As Panel
Private _FileUpload As FileUpload
Private _btnUpload As Button
Private _lblReplaceMsg As Label
End Class
asp.net vb user control raising an event on the calling page
derive the arguments class you need from EventArgs according to your structure and data and use that.
In some ASP.Net code-behind, I call the SelectRow method of a custom grid control that inherits from System.Web.UI.WebControls.GridView.
Making the call:
If (ProgressGrid.Rows.Count > 0) Then
ProgressGrid.SelectRow(0)
End If
As expected, this generates a SelectedIndexChanged event, which is picked up by the handler:
Protected Sub ProgressGrid_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ProgressGrid.SelectedIndexChanged
Using db As New DataContext
Dim course = (From c In db.CourseResults
Where c.MemberID = MemberID AndAlso c.ResultID = CInt(ProgressGrid.SelectedDataKey.Value)
Select c).Single
' more code here
End Using
End Sub
My problem is that ProgressGrid.SelectedDataKey is nothing inside my event handler, causing a null-reference error. While debugging with Visual Studio 2010, I can see from the call stack that the ProgressGrid.SelectRow(0) was hit and that the ProgressGrid.Rows.Count is greater than zero. So why are all the "Selected..." properties on the ProgressGrid object set to nothing or -1? Any idea what I am doing wrong?
The custom Grid class contains this property which overrides the default GridView behavior:
Public Overrides Property SelectedIndex() As Integer
Get
If AutoPostback Or AllowSelect = False Then
Return MyBase.SelectedIndex
Else
If HttpContext.Current Is Nothing Then
Return Nothing
Exit Property 'Exit if in design mode
End If
Dim index As String = Page.Request(Me.ClientID + "_SelectedRow")
If (String.IsNullOrEmpty(index)) Then
If (ViewState("SelectedIndex") Is Nothing) Then
Return -1
Else
Return ViewState("SelectedIndex")
End If
Else
ViewState.Add("SelectedIndex", index)
Return CType(index, Integer)
End If
End If
End Get
Set(ByVal value As Integer)
MyBase.SelectedIndex = value
End Set
End Property
The debugger is unable to display the details for MyBase and the first calls to MyBase.SelectedIndex = value have the debugger's quick watch window return a null reference error. Once I reach the event handler, break points in the above property indicate that MyBase.SelectedIndex is nothing, despite attempting to set it to zero.
I discovered that setting the ViewState in the custom grid's SelectedIndex property fixed my problem. This allowed ViewState to hold the new index value and return it when the Get method was called on the property.
Set(ByVal value As Integer)
MyBase.SelectedIndex = value
ViewState("SelectedIndex") = value
End Set
On a page we have submit button, on clicking it we are getting error due to m_strPageDefinition has null value. Following is the code of it for more insight. Only sometimes and only in production we are getting value of m_strPageDefinition as null, which is causing problems. Does anyone has idea why m_strPageDefinition is coming null.
Private m_strPageDefinition As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
If (Not Context.Items("MyXmlString") = Nothing) And (Not Context.Items("mFormID") = Nothing) Then
MyXMLString = Context.Items("MyXmlString")
MyHiddenXMLString.Value = MyXMLString
End If
Else
m_strPageDefinition = MyHiddenXMLString.Value
End If
End Sub
Private Property MyXMLString()
Get
Return m_strPageDefinition
End Get
Set(ByVal value)
m_strPageDefinition = value
End Set
End Property
You should consider being more consistent how you address m_strPageDefinition. Why are you accessing is a private variable instead of always using the Property setter? e.g.
Private m_strPageDefinition As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
If (Not Context.Items("MyXmlString") = Nothing) And (Not Context.Items("mFormID") = Nothing) Then
MyXMLString = Context.Items("MyXmlString")
MyHiddenXMLString.Value = MyXMLString
Else
MyXMLString = MyHiddenXMLString.Value
End If
Else
MyXMLString = MyHiddenXMLString.Value
End If
End Sub
Private Property MyXMLString()
Get
Return m_strPageDefinition
End Get
Set(ByVal value)
m_strPageDefinition = value
End Set
End Property
I believe your issue is coming from the missing "Else" I have included here in the Not IsPostBack statement. Forgive me for VB.NET is not a language I code in, so the format/structure might be off a bit.
I think the problem is here:
m_strPageDefinition = MyHiddenXMLString.Value
and more specifically: MyHiddenXMLString.Value is probably null.
I couldn't figure out where you declared MyHiddenXMLString, but how about making it a hidden variable (input type="hidden" .. ) on the aspx side and setting it's value when the page loads the first time.
Then you know the value will always be there and easy to access.
hth
I am pulling my hair out with this one.
I have an Employee dropdownlist usercontrol (uxEmployee) and I have exposed the SelectedValue method as a property(SelectedValue).
Within my page I am trying to either set the value based on my data or add the value to the list if it is not currently on there.
No matter what I do it is adding the Set value to the list. Even when the value is already on the list; it is still adding the value. Basically FindByValue is always returning "Nothing" no matter what I do. Any help would be appreciated.
Public Property SelectedValue() As String
Get
Return uxEmployee.SelectedValue
End Get
Set(ByVal value As String)
If Not uxEmployee.Items.FindByValue(value) Is Nothing Then
uxEmployee.SelectedValue = value
Else
uxEmployee.Items.Insert(0, New ListItem(value, value))
uxEmployee.AppendDataBoundItems = True
uxEmployee.DataBind()
End If
End Set
End Property
Public WriteOnly Property Department() As String
Set(ByVal value As String)
_department = value
End Set
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not (IsPostBack) Then
Dim emp As New Employee()
With uxEmployee
If _department <> String.Empty Then
.DataSource = emp.GetByDept(_department)
Else
.DataSource = emp.GetList()
End If
.DataTextField = "FullName"
.DataValueField = "UserName"
.DataBind()
End With
End If
End Sub
my calling page uses the following in page load.. uxSalesleadv is an instance of uxemployee
Dim objPrj As ServiceProject = New ServiceProject()
objPrj = objPrj.GetItem(prjID)
With objPrj
uxSalesLead.SelectedValue = .SalesLead
End With
The method performs an exact match and is case-sensitive and culture-insensitive. Have you checked that the values are identical in the debugger?
Try using FindByName(). If it works, then the values are not being set properly when you initially set the data source.
After much trial and error and debugging I have figured out the problem.
I was able to get the control to work correctly when i moved my data assignment from the page_load to the pre_render event. This was not ideal though.. I eventually found that the usercontrol was setting the value before the actual databinding was taking place.
I have now moved the logic out of my Set property and now added logic to my controls page_load databinding to check if the previous SET value is in the databind list. If it isnt then it adds the item. This will allow me to use the usercontrol throughout my apps without any additional page code and worrying about item not in list errors.