I need to populate 12 asp:dropdownlist 's with the same 0-9 options. (ASP.NET VB)
Now I could just populate them manually, but I wandered if there is an easier method... Can i populate an Array and use that to populate them all?
I thought I could write a small function that you pass the DDL's name into, but how then could I use that function input string as the dropdownlist name?
I know this is simple stuff but its something Ive not needed to do before, and cant see a simple method to do it.
Try this
Protected Sub Page_Load(ByVal sender as Object, ByVal e As EventArgs)
If (Not Page.IsPostBack) Then
BindDropdown(list1)
BindDropdown(list2)
BindDropdown(list3)
End If
End Sub
Private Sub BindDropdown(ByVal list As DrodownList)
Dim items As String() = {"0","1","2","3","4","5","6","7","8","9" }
list.DataSource = items
list.DataBind()
End Sub
Add this to cache and make a common helper function to bind the dropdown
Related
I have a datasource which is specified on Page_Load :
// EmpDetailsList query to get employee data
EmpGridView.DataSource = EmpDetailsList
EmpGridView.DataBind()
I am trying to filter the datasource when the user clicks a button;
Protected Sub Search_btn_Click(sender As Object, e As EventArgs) Handles Search_btn.Click
FilteredList = EmpDetailsList.Where(EmpDetailsList.FirstName= "John")
EmpGridView.DataSource = FilteredList
EmpGridView.DataBind()
End Sub
I am having a lot of trouble doing this without using EmpDetailsList as a global variable.
I also do not want to violate the DRY principle and call the data source a second time when the search button is clicked. I also tried passing EmpDetailsList as a parameter to another Sub but I don't understand how to use it with a click event handler.
Any suggestions will be helpful, I've looked for a couple of hours but I haven't found anything that doesn't use global variables.
The Where function expects a predicate. Add a .ToList at the end so your have a List(Of T) instead of an IEnumerable.
Protected Sub Search_btn_Click(sender As Object, e As EventArgs) Handles Search_btn.Click
Dim FilteredList = (EmpDetailsList.Where(Function(e) e.FirstName = "John")).ToList
EmpGridView.DataSource = FilteredList
EmpGridView.DataBind()
End Sub
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.
This is one of those problems which seems like it should have a simple solution but I can't work out what it is!
How can I pass a control from one sub to another if the first sub doesn't actually call the second? For example, where btnChangeText is in a panel that has a ModalPopupExtender called mpExample, and therefore isn't usually visible:
Protected Sub btnChangeText_Click(sender as object, e as EventArgs) Handles btnChangeText.Click
<SpecifiedTextBox>.Text = "Hello"
End Sub
And then on the main page, visible at all times, is a button associated with each textbox. In this example, it's textbox15:
Protected Sub btnChangeTextBox15_Click(sender as object, e as EventArgs) Handles btnChangeTextBox15.Click
<Set TextBox15 as variable>
mpExample.Show()
End Sub
I know it's a silly example - believe me when I say that the real application I want to make of this actually makes sense! But the point is that I want to somehow store the name of the control to be updated by the first sub when the second sub is run.
If I was calling the first sub from the second it'd be easy, I'd just pass it as an argument, but I'm not. The first sub is called from a button click and is an independent action from the running of the second sub.
I don't seem to be able to use a session variable (my first thought) because I can't find any way to store the control name as a string and then convert it back to an actual control when the first sub runs. That'd be the easiest answer if somebody could tell me how to do it.
One approach would be to store the control's ID as a string in a Session variable, and then use the FindControl method to grab the control in your 2nd Click event.
Protected Sub btnChangeTextBox15_Click(sender as object, e as EventArgs) Handles btnChangeTextBox15.Click
Session("currentTextBox") = TextBox15.ID
mpExample.Show()
End Sub
Protected Sub btnChangeText_Click(sender as object, e as EventArgs) Handles btnChangeText.Click
Dim currentTextBox As TextBox
currentTextBox = CType(Page.FindControl(Session("currentTextBox")),TextBox)
currentTextBox.Text = "Hello"
End Sub
Note that if your TextBox15 control is inside some kind of container (a Panel or something), you'll need to use that container's FindControl method, rather than Page.FindControl.
Another approach is to store the TextBox itself in a Session variable, and then pull that out to set the text in your other method. Note that this only works if the methods are both called in the same request (which doesn't sound like it would work for your use-case). Here's what that would look like:
Protected Sub btnChangeTextBox15_Click(sender as object, e as EventArgs) Handles btnChangeTextBox15.Click
Session("currentTextBox") = TextBox15
mpExample.Show()
End Sub
Protected Sub btnChangeText_Click(sender as object, e as EventArgs) Handles btnChangeText.Click
Dim currentTextBox As TextBox
currentTextBox = CType(Session("currentTextBox"), TextBox)
currentTextBox.Text = "Hello"
End Sub
I am trying to put a - what seems - very simple web user control
basically i want it to render as a dropdownlist/checkboxlist or radiolist based on a property
but also want to be able to work out what is selected
i was trying the following - but cant seem to work out how to attach to the selectedindexchanged of the listcontrol so that i can set the selectd value(s)
its not helping that my VB is not up to much but am forced to use it in this instance
its not even giving me the intellisense for the event..
Public Options As List(Of Options)
Public ControlRenderType As ControlRenderType
Public IncludeFreeOption As Boolean
Public SelectedOptions As List(Of Options)
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
Dim c As ListControl
Select Case (ControlRenderType)
Case STGLib.ControlRenderType.CheckBoxList
c = New CheckBoxList()
Case STGLib.ControlRenderType.DropdownList
c = New DropDownList()
Case STGLib.ControlRenderType.RadioButtonList
c = New RadioButtonList()
Case Else
Throw New Exception("No Render Type Specified")
End Select
For Each opt In Options
Dim li = New ListItem(opt.Description, opt.ID)
c.Items.Add(li)
Next
c.SelectedIndexChanged += ..?? or something
Page.Controls.Add(c)
End Sub
can anyone explain please - it is of course quite possible that I am going about this in completely the wrong way..
thanks
First create a Sub or a Function to handle the IndexChange of the object that you have created dynamically and make sure that the signature of the Sub is something like this
Sub myOwnSub(ByVal sender As Object, ByVal e As EventArgs)
...
... Handle your event here
...
End Sub
Then after you create your object add the following code
Dim obj as ListBox
AddHandler obj.SelectedIndexChanged, AddressOf myOwnSub
I find myself needing to preform the same actions on both HtmlControls and WebControls. I am a firm believer in DRY and find the fact that there is only the Control class to use if I want to consolidate the functions on both types. The problem that I have with using Control is that there certain properties that both HtmlControl and WebControl expose that Control does not. In the current case, the Attributes property is the problem. Does anyone have any suggestions on how to avoid the duplication of code in this type of instance?
Both HtmlControl and WebControl implement the interface IAttributeAccessor (explicitly). Use the IAttributeAccessor.SetAttribute instead. I'm not a vb.net coder, so I leave the task of writing the code to the reader. ;)
In the past I've duplicated code to set the attributes for HtmlControls and WebControls. However, here's another idea:
Private Sub SetAttribute(ByRef ctrl As Control, ByVal key As String, ByVal value As String)
If TypeOf ctrl Is HtmlControl Then
DirectCast(ctrl, HtmlControl).Attributes(key) = value
ElseIf TypeOf ctrl Is WebControl Then
DirectCast(ctrl, WebControl).Attributes(key) = value
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For Each ctrl In Me.Controls
SetAttribute(ctrl, "class", "classname")
Next
End Sub
I know what you mean. Theoretically, you could do one of the following:
Use reflection to assign some of those common settings.
Create a wrapper class that can take either a webcontrol or html control reference, and assign the values. (if control is webcontrol) assign value else if (html is htmlcontrol) assign value, something like that.
Create another logical class to store the common settings, then another component to copy those settings and apply them to the class.
Ultimately, there isn't any common bridge (no common base class or interface). What kind of assignments are we talking about?
Simon's answer works:
Private Sub SetAttribute(ByRef ctrl As IAttributeAccessor, ByVal key As String, ByVal value As String)
ctrl.SetAttribute(key, value)
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For Each ctrl In Me.Controls.OfType(Of IAttributeAccessor)()
SetAttribute(ctrl, "class", "classname")
Next
End Sub