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.
Related
Using vb.net 4.5 and Telerik 2017.2.711.45 (Q2)
I am trying to get radgrid filter expressions and a public string variable to persist across postbacks.
With EnableViewState=FALSE, radgrid filter expressions do not persist through postback, however a public string variable (stringVar) does persist.
When I set EnableViewState=TRUE the filter expressions in radgrid do persist, however it causes stringVar to not persist.
From my understanding of ViewState, it makes no sense that setting EnableViewState=TRUE would cause stringVar to not persist across postbacks. I would love to know why this is occurring and what I could do to resolve this.
EDIT:
The highlighted Line is where an error would be thrown because ReportTitle no longer has a value.
Partial Class displayxslgrid
Public ReportTitle As String
Public ReportsDB As reportDataBase
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Page.EnableViewState = True
Reports = New reportDataBase.Global_Functions(System.Web.HttpContext.Current)
End Sub
Protected Sub RadGrid1_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
Call BindRadGrid1()
End Sub
Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.ItemCommand
Dim strReportTitle As String
Select Case e.CommandName
Case RadGrid.ExportToExcelCommandName, RadGrid.ExportToWordCommandName, RadGrid.ExportToCsvCommandName
strReportTitle = ReportTitle.Trim
End Select
End Sub
Public Sub BindRadGrid1()
Dim strReportTitle As String
Dim dt As DataTable = Nothing
ReportTitle = dt.Rows(0).Item("ReportTitle")
strReportTitle = dt.Rows(0).Item("ReportTitle").ToString
'RadGrid1 Data source gets set here along with other stuff
End Sub
End Class
Using view state is normal, and Telerik controls need it to preserve their values across post-backs. A public string property on your page class should not persist, and should be set/calculated every time. If you absolutely need something like that to persist, save the value in a hidden server control, or have it in the QueryString of the URL.
So it turns out, that that variable was not truly persisting. It was getting its value from the bindradgrid1. When EnableViewState=True the need data source event is not fired, therefore the bindradgrid1 is not called and the variable does not get a value. Simple fix was adding a bindradgrid1() in the item command sub so that even with EnableViewState=True, bindradgrid1() will still get called. Thanks for all who helped.
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.
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
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim lab As Label = Me.GridView1.FindControl("Label1")
If TextBox2.Text = "7" Then
GridView1.SelectedRow.Cells(2).Text = "500"
Else
GridView1.SelectedRow.Cells(2).Text = "950"
End If
End Sub
The following error occurs : Object reference not set to an instance of an object.
You've got this code in your Page Load event, so it will run when the page is first loaded, and on every single postback. This probably isn't what you want.
I imagine that on the very first load, there isn't a selected row in your GridView, so GridView1.SelectedRow is going to be null. If this isn't null, then Cells or Cells(2) definitely will be. Trying to access a property on null is going to throw a NullReferenceException - "Object reference not set to an instance of an object".
As this MSDN example shows, you're probably better off accessing the SelectedRow property in an event handler for the SelectedIndexChanged event of the GridView.
Dim lab As Label = Me.GridView1.FindControl("Label1")
It doesn't look like you're doing anything with this label. Put a breakpoint on that line and see if it finds it. If it doesn't and you don't even use it, take the line out.
Also, check if textbox2 is valid whilst debugging.