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
Related
I am trying to use the code below to store the items from the list into a session. For some reason when I debug the code the count is returning 0 even though there are multiple items in the list box? Any ideas what I am doing wrong here?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
NameTextBox_AutoCompleteExtender.OnClientItemSelected = "getSelected"
End Sub
Protected Sub cmdNext_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles cmdNext.Click
Dim n As Integer = NPListbox.Items.Count
Dim arr As String() = New String(n - 1) {}
For i As Integer = 0 To arr.Length - 1
arr(i) = NPListbox.Items(i).ToString()
Next
Session("arr") = arr
Response.Redirect("~/frmDescription.aspx")
End Sub
<script language="javascript" type="text/javascript">
function getSelected(source, eventArgs) {
var s = $get("<%=NameTextBox.ClientID %>").value;
var opt = document.createElement("option");
opt.text = s.substring(s.length - 10);
opt.value = s.substring(s.length - 10);
document.getElementById('<%= NPListbox.ClientID %>').options.add(opt);
}
I am going to guess that you do not have any logic in your Page_Load to populate the listbox, based upon what it had when you finished the autocomplete extender logic. Since, you do not, then when the click event fires after the Page_Load your values are gone.
Put the logic that executes on selection of the autocomplete extender in a method and have your Page_Load call that, like this:
Protected Sub Page_Load(sender As Object, e As EventArgs)
' Put call here to populate the listbox results from autocomplete extender selection
PopulateListBox()
End Sub
Private Sub PopulateListBox()
' Go to whatever resource you are using to get the values for the list box
End Sub
UPDATE:
Since you are depending upon using a client-side function to grab the values from the autocomplete extender and populating the listbox that way, you need to mimic that logic in your Page_Load on the server-side, because it will be too late if you try to use the client-side one, since you need the data server-side and all of the server-side events happen before the client-side logic in a server post back.
You need to do something like this:
Protected Sub Page_Load(sender As Object, e As EventArgs)
' Only do this when page has posted back to the server, not the first load of the page
If IsPostBack Then
' Put call here to populate the listbox results from autocomplete extender selection
PopulateListBox()
End If
End Sub
Private Sub PopulateListBox()
' Get value from text box
Dim textBoxValue As String = Me.NameTextBox.Text
' Create new item to add to list box
Dim newItem As New ListItem(textBoxValue)
' Add item to list box and set selected index
NPListbox.Items.Add(newItem)
NPListbox.SelectedIndex = NPListbox.Items.Count - 1
End Sub
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 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
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
Note: There was not any question with this kind of problem here or anywhere...
Ok, so I made my listview, and it's delete and edit events are working properly, now I want to implement a possibility for user to mark an element as "default".
D, E and Def are buttons
Reference
----------------------------------------------------------------
- ref1 - somevalue - somevalue - somevalue - [D] - [E] - [Def]
----------------------------------------------------------------
so that would be a row from a table, I made delete and edit work by handling events from listview,
Private Sub lvMain_ItemDelete(ByVal sender As Object, ByVal e As ListViewDeleteEventArgs) Handles lvMain.ItemDeleting
Dim refFac As new ReferenceFactory
refFac.Delete(e.Keys(0))
EndSub
similar for Editing. But now when I try to get values from Default button, the button wont even do anything...
This is the code:
<asp:ImageButton ID="ibtDefault" runat="server" ImageUrl="~/Images/Default16.png" CommandName="Default" />
and for my logic:
Public Sub ibtDefault_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs )
SelectedRef.Name = "Test"
End Sub
I just wanted to test it whether it will run or not by changing the value of my global string that will show which Reference is made default. But it wont even do that...
Then I tried with Commands.
Private Sub lvMain_ItemCommand(ByVal sender As Object, ByVal e As ListViewCommandEventArgs) Handles lvMain.ItemCommand
If e.CommandName = "Default" Then
'Dim refID As New Integer
'Dim refer As ListViewItem
'refer = e.Item
SelectedRef.Name = "Test"
End If
End Sub
But this wont run either... What am I doing wrong here :S
Basically what I want to is that on click i save Reference Name and ID in two global variables i prepared.
Thanks
Default is a reserved word and may be messing with your code. When you run through your ItemCommand event, you can rebind the data at the end of it and it should refresh your page.