I have an asp.net web page which is giving me some problems. I developed the following classes to generate search filters for the page. The search process is not important as it already works. I want to dynamically generate the search filters.
Imports Microsoft.VisualBasic
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports MEI.SPDocuments.Type
Public Class SearchFilter
Private WithEvents _genreDropDown As DropDownList
Private WithEvents _subGenreDropDown As DropDownList
Private WithEvents _txtValue As TextBox
Private _txtBoxAutoCompleteExtender As AjaxControlToolkit.AutoCompleteExtender
Private _filterGenres As Collection(Of String)
Private _programSubGenres() As String = {"Program ID,GetPrograms", "Territory ID,GetTerritories", _
"Rep Name,GetRepNames", "District ID,GetDistricts", "DM Name,GetDMNames", "Region ID,GetRegions", _
"RM Name,GetRMNames", "Speaker Counter,GetSpeaker", "Vedor ID,GetVendors", "Vendor Name,GetVendorName", _
"Date Range,", "City,", "State,", "Pay To,", "Expense Range,", "PIF ID,GetPifs"}
Private _speakerSubGenres() As String = {"Speaker Counter,GetSpeaker", "Speaker Last Name,GetSpeakerLNames", "Speaker First Name,GetSpeakerFNames"}
Private _expenseSubGenres() As String = {"Expense Counter,GetExpenses"}
Private _vendorSubGenres() As String = {"Vendor ID,GetVendors", "Vendor Name,GetVendorName"}
Private _trackSubGenres() As String = {"Track Number,GetTracks", "HCP First Name,GetHCPFName", "HCP Last NameGetHCPLName"}
Public Sub New(ByVal company As CompanyCode, ByVal year As DocumentYearCode)
_filterGenres = New Collection(Of String)
_txtValue = New TextBox
_txtValue.ID = Guid.NewGuid.ToString
_txtBoxAutoCompleteExtender = New AjaxControlToolkit.AutoCompleteExtender
With _txtBoxAutoCompleteExtender
.ID = "AC__" + _txtValue.ID
.MinimumPrefixLength = 1
.EnableCaching = False
.ServicePath = "~/AutoComplete.asmx"
.ServiceMethod = "PlaceHolder"
.TargetControlID = _txtValue.ID
.CompletionListCssClass = "CompletionList"
.CompletionListHighlightedItemCssClass = "ItemHighlighted"
.CompletionListItemCssClass = "ListItem"
.DelimiterCharacters = ""
.Enabled = True
End With
Select Case company
Case CompanyCode.AbbottAnimalHealth, CompanyCode.AbbottDiabetesCare, CompanyCode.AbbottDiagnosticsDivision, CompanyCode.AbbottMedicalOptics, CompanyCode.AbbottMolecular, _
CompanyCode.AbbottPointOfCare, CompanyCode.AbbottVascular, CompanyCode.Corporate, CompanyCode.DivAbbottNutrition, CompanyCode.EstablishedProductsDivision, _
CompanyCode.GlobalPharmaceuticalResearchAndDevelopment, CompanyCode.GlobalStrategicMarketingAndServices, CompanyCode.PharmaseuticalProductsGroup, _
CompanyCode.ProprietaryPharmaceuticalsDivision, CompanyCode.RegulatoryAffairsPPG
_filterGenres.Add("Div Docs")
Case Else
_filterGenres.Add("Program")
_filterGenres.Add("Speaker")
_filterGenres.Add("Expense")
_filterGenres.Add("Vendor")
End Select
_genreDropDown = New DropDownList
_genreDropDown.AutoPostBack = True
_genreDropDown.Attributes.Add("runat", "server")
AddHandler _genreDropDown.SelectedIndexChanged, AddressOf _genreDropDown_ItemChanged
_subGenreDropDown = New DropDownList
_subGenreDropDown.AutoPostBack = True
_subGenreDropDown.Attributes.Add("runat", "server")
AddHandler _subGenreDropDown.SelectedIndexChanged, AddressOf _subGenreDropDown_ItemChanged
PopulateDDls()
End Sub
Private Sub PopulateDDls()
_genreDropDown.Items.Add("")
For Each s As String In _filterGenres
_genreDropDown.Items.Add(s)
Next
End Sub
Private Sub _genreDropDown_ItemChanged(ByVal sender As Object, ByVal e As System.EventArgs)
_subGenreDropDown.Items.Clear()
_subGenreDropDown.Items.Add("")
Select Case _genreDropDown.SelectedItem.ToString
Case "Program"
For Each s As String In _programSubGenres
Dim li As New ListItem(s.Split(CChar(","))(0), s.Split(CChar(","))(1))
_subGenreDropDown.Items.Add(li)
Next
Case "Speaker"
For Each s As String In _speakerSubGenres
Dim li As New ListItem(s.Split(CChar(","))(0), s.Split(CChar(","))(1))
_subGenreDropDown.Items.Add(li)
Next
Case "Expense"
For Each s As String In _expenseSubGenres
Dim li As New ListItem(s.Split(CChar(","))(0), s.Split(CChar(","))(1))
_subGenreDropDown.Items.Add(li)
Next
Case "Vendor"
For Each s As String In _vendorSubGenres
Dim li As New ListItem(s.Split(CChar(","))(0), s.Split(CChar(","))(1))
_subGenreDropDown.Items.Add(li)
Next
Case "Div Docs"
For Each s As String In _trackSubGenres
Dim li As New ListItem(s.Split(CChar(","))(0), s.Split(CChar(","))(1))
_subGenreDropDown.Items.Add(li)
Next
End Select
End Sub
Private Sub _subGenreDropDown_ItemChanged(ByVal Sender As Object, ByVal e As System.EventArgs)
_txtBoxAutoCompleteExtender.ServiceMethod = _subGenreDropDown.SelectedValue.ToString
If _subGenreDropDown.SelectedValue.ToString = String.Empty Then _txtBoxAutoCompleteExtender.ServiceMethod = "PlaceHolder"
End Sub
Public Function createMyTableRow() As HtmlTableRow
Dim myRow As New HtmlTableRow
myRow.Cells.Add(New HtmlTableCell())
myRow.Cells.Add(New HtmlTableCell())
myRow.Cells.Add(New HtmlTableCell())
myRow.Cells(0).Controls.Add(_genreDropDown)
myRow.Cells(1).Controls.Add(_subGenreDropDown)
myRow.Cells(2).Controls.Add(_txtValue)
myRow.Cells(2).Controls.Add(_txtBoxAutoCompleteExtender)
Return myRow
End Function
Private Sub newAutoCompleteExtender(ByVal genre As String)
If _txtValue.Parent.Controls.Count = 2 Then
_txtValue.Parent.Controls.RemoveAt(1)
End If
_txtValue.Parent.Controls.Add(_txtBoxAutoCompleteExtender)
End Sub
End Class
Public Class SearchFilterGroup
Private _searchFilterCollection As Collection(Of SearchFilter)
Private _tableContainer As HtmlTable
Private _company As CompanyCode
Private _year As DocumentYearCode
Public WithEvents _addFilterButton As New Button
Public Sub New(ByVal company As CompanyCode, ByVal year As DocumentYearCode)
_searchFilterCollection = New Collection(Of SearchFilter)
_tableContainer = New HtmlTable
_company = company
_year = year
_addFilterButton.Text = "Add Filter"
_addFilterButton.Attributes.Add("runat", "server")
_addFilterButton.ID = "btnAddFilter"
AddHandler _addFilterButton.Click, AddressOf _addFilterButton_Click
End Sub
Public Sub _addFilterButton_Click(ByVal Sender As Object, ByVal e As System.EventArgs)
_searchFilterCollection.Add(New SearchFilter(_company, _year))
_tableContainer.Rows.Add(_searchFilterCollection(_searchFilterCollection.Count - 1).createMyTableRow)
End Sub
Public Function table() As HtmlTable
_tableContainer.Rows.Add(New HtmlTableRow)
_tableContainer.Rows(0).Cells.Add(New HtmlTableCell)
_tableContainer.Rows(0).Cells(0).ColSpan = 3
_tableContainer.Rows(0).Cells(0).Controls.Add(_addFilterButton)
_addFilterButton_Click(Nothing, Nothing)
_addFilterButton_Click(Nothing, Nothing)
Return _tableContainer
End Function
End Class
I am having problems devising a way to persist the generate controls. Any help would be appreciated.
Basically, for task which you do is better to create custom server control. In ASP.NET exist CompositeControl class which simplifies developing your own server controls which for example should include some other server controls (like panels, texboxes and etc) as children.
A very good link to start with creating your own server controls is Dino Esposito article: A Crash Course on ASP.NET Control Development: Building Composite Controls
Related
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.
I am learning ASP.Net development & trying to create Web application where ListView data should get filter selection done in ComboBox. I have made ListView & bind it with my database table & tried to put ComboBox filter but getting this error
(System.Web.HttpException: Request is not available in this context) in output .
VB Code
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Partial Class VB
Inherits System.Web.UI.Page
Dim query As String
Dim strArea As String = Request.QueryString("Country")
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindListView()
End If
End Sub
Private Sub BindListView()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand()
cmd.CommandText = "SELECT CustomerId, ContactName, Country FROM Customers"
cmd.Connection = con
Using sda As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
sda.Fill(dt)
lvCustomers.DataSource = dt
lvCustomers.DataBind()
End Using
End Using
End Using
End Sub
Protected Sub OnPagePropertiesChanging(sender As Object, e As PagePropertiesChangingEventArgs)
TryCast(lvCustomers.FindControl("DataPager1"), DataPager).SetPageProperties(e.StartRowIndex, e.MaximumRows, False)
Me.BindListView()
End Sub
Protected Sub country_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles country.SelectedIndexChanged
Try
If country.SelectedValue <> "All" Then
query = "Select CutomerId, Contact Name, Country from Customers where Country like '%" + strArea + "%' order by rank;"
End If
Dim cmd As New SqlCommand(query)
Dim da As New SqlDataAdapter(cmd)
Dim table As New DataTable
Catch ex As Exception
End Try
End Sub
End Class
Request read-only property belongs to Page class and inherited class members are available only within the members of the class which inherits it(VB in your case). Change your code like this:-
Partial Class VB
Inherits System.Web.UI.Page
Dim query As String
Dim strArea As String = String.Empty
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindListView()
End If
strArea = Request.QueryString("Country")
End Sub
Also, Please note it will be better to check if any value exists within your query string variable before assigning it.
I came across to this code that creates control dynamically. I tried it. However, whenever I run it, Object reference not set to an instance of an object error pops out and points out to a certain line in the code( i put ---> on the line). I'm a newbie in this programming language. I don't know what to do.
Here's the code I got:
Imports System.Collections.Generic
'Imports System.Data.Odbc
Partial Public Class main
Inherits System.Web.UI.Page
Private controlCounter As Integer = 0
Private myControlList As List(Of String)
Protected Overrides Sub LoadViewState(ByVal savedState As Object)
MyBase.LoadViewState(savedState)
myControlList = DirectCast(ViewState("myControlList"), List(Of String))
For Each ctlID As String In myControlList
controlCounter += 1
Dim hyper As New HyperLink()
hyper.ID = ctlID
Dim lineBreak As New LiteralControl("<br />")
PlaceHolder1.Controls.Add(hyper)
PlaceHolder1.Controls.Add(lineBreak)
Next
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
myControlList = New List(Of String)()
ViewState("myControlList") = myControlList
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
controlCounter += 1
Dim hyper As New HyperLink()
hyper.Text = "a new text Box"
hyper.ID = "hyperlink" + controlCounter.ToString()
Dim lineBreak As New LiteralControl("<br />")
PlaceHolder1.Controls.Add(hyper)
PlaceHolder1.Controls.Add(lineBreak)
--> myControlList.Add(hyper.ID)
ViewState("myControlList") = myControlList
End Sub
End Class
please help me out. Thanks.
A button click initiates a postback, and you only set myControlList to a thing if the request is not a post back, so it's nothing.
If you're wanting to create the list on page load, and then only keep adding to it with subsequent clicks, say, then you'll need to shove myControlList into the Session, or something, after creating it, then on button click retrieve it again, add to it, and re-set it in the Session.
To do this add and getting from the Session stuff, do,
Session("someDistinctKey") = myControlList;
myControlList = CType(Session("someDisctintKey"), List(Of String))
respectively.
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
My page contains: GridView1, GridView2, Button1, Button2, DropDownList1
I bind Gridviews to the table selected in dropdown like this:
Dim results as DataTable
Select Case ddl1.SelectedValue
Case 0
results = dtZero
Case 1
results = dtOne
Case 2
results = dtTwo
Case 3
results = dtThree
End Select
GridView1.DataSource = results
GridView1.DataBind()
Then I have two buttons with the following code:
Protected Sub btn1_Click(sender As Object, e As EventArgs) Handles btn2.Click
Select Case ddl1.SelectedValue
Case 0
RunZero()
Case 1
RunOne()
Case 2
RunTwo()
Case 3
RunThree()
End Select
End Sub
And
Protected Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
Select Case ddl1.SelectedValue
Case 0
RemoveZero()
Case 1
RemoveOne()
Case 2
RemoveTwo()
Case 3
RemoveThree()
End Select
End Sub
For me it looks like a lot of overhead. How can I improve then design and only specify that I'm working on the following record in dropdown list without specifying the Case condition every time? Should I change my design or leave it like it is?
Update:
RunZero, RunOne, RunTwo, RemoveZero, RemoveOne, RemoveTwo, RemoveThree - Execute six different stored procedures.
This was a fun one to solve. Thank you.
Sounds like you should be using actions.
First Create a class that will keep track of all your "operations" represented by your dropdownlist...
EDIT: Using Actions instead to show that you can use parameters for your functions if you want or not.
Class OpMode
Public DropDownKey As String
Public RunFunction As Action(Of Int32) 'This one will support a parameter'
Public RemoveFunction As Action
Public dt As Data.DataTable
Sub New(DropDownKey As String, RunFunction As Action(Of Int32), RemoveFunction As Action, dt As Data.DataTable)
Me.DropDownKey = DropDownKey
Me.RunFunction = RunFunction
Me.RemoveFunction = RemoveFunction
Me.dt = dt
End Sub
End Class
You should already have methods for running your procs.
EDIT: In this example I've allowed a parameter for the "Run" procs...
Private Sub RunOne(MyVariable As Int32)
Response.Write("RunOne")
End Sub
Private Sub RunTwo(MyVariable As Int32)
Response.Write("RunTwo")
End Sub
Private Sub RemoveOne()
Response.Write("RemoveOne")
End Sub
Private Sub RemoveTwo()
Response.Write("RemoveTwo")
End Sub
I assume you have those datatables somewhere...
Private dtOne As New Data.DataTable
Private dtTwo As New Data.DataTable
Create a readonly property to hold all your OpModes you want to use. It's light to recreate so no need to cache it.
Private ReadOnly Property OpModes As List(Of OpMode)
Get
Dim _OpModes As New List(Of OpMode)
_OpModes.Add(New OpMode("1", AddressOf RunOne, AddressOf RemoveOne, dtOne))
_OpModes.Add(New OpMode("2", AddressOf RunTwo, AddressOf RemoveTwo, dtTwo))
Return _OpModes
End Get
End Property
Make a function for searching through your list of opmodes for the one that matches your dropdownlist...
Private Function GetOpByDropDownKey(DropDownKey As String) As OpMode
Return (From x In OpModes Where x.DropDownKey = DropDownKey).First
End Function
Make a click event that works for both buttons and invokes the appropriate method...
EDIT: In this example, the RunFunction has a parameter but the RemoveFunction does not.
Private Sub btn_Click(sender As Object, e As System.EventArgs) Handles btn1.Click, btn2.Click
With GetOpByDropDownKey(dd1.SelectedValue)
If sender Is btn1 Then
.RunFunction(12345)
Else
.RemoveFunction()
End If
End With
End Sub
Now you can bind the correct datatable programmatically...
GridView1.DataSource = GetOpByDropDownKey(dd1.SelectedValue).dt
GridView1.DataBind()
This should be a lot easier to maintain once you get it working. You will have less places where you could have accidently forgotten to include an item in your case statement, since all that definition is defined once in your readonly property.
EDIT: (Removed note about delegates. Actions are all you need.)
I hope this helps you.
EDIT: This is suitable for VB.Net only, not ASP.Net.
A simpler way to acomplish this is to create a class with a ToString function and populate the list with that.
Class OpInfo
Public Sub New(ByVal Text As String, ByVal DataTable As DataTableSelect, ByVal RunMethod As Action, ByVal RemoveMethod As Action)
_Text = Text
_DataTable = DataTable
_RunMethod = RunMethod
_RemoveMethod = RemoveMethod
End Sub
Public ReadOnly Property DataTable As DataTableSelect
Get
Return _DataTable
End Get
End Property
Private _DataTable As DataTableSelect
Public Sub Run()
_RunMethod()
End Sub
Private _RunMethod As Action
Public Sub Remove()
_RemoveMethod()
End Sub
Private _RemoveMethod As Action
Public Function Overrides ToString() As String
Return _Text
End Function
Private _Text As String
End Class
You can populate the list with instances of this class. By type casting the selected item, you have access to all of its members.
Class Form1
Public Sub New()
InitializeComponents()
ddl1.Items.Add(New OpInfo("Zero", dtZero , AddressOf RunZero , AddressOf RemoveZero )
ddl1.Items.Add(New OpInfo("One", dtOne , AddressOf RunOne , AddressOf RemoveOne )
ddl1.Items.Add(New OpInfo("Two", dtTwo , AddressOf RunTwo , AddressOf RemoveTwo )
ddl1.Items.Add(New OpInfo("Three", dtThree, AddressOf RunThree, AddressOf RemoveThree)
End Sub
Protected Sub ddl1_SelectedIndexChanged(sender As Object, e As EventArgs)
If ddl1.SelectedIndex = -1 Then
' Code for when nothing is selected.
Else
GridView1.DataSource = CType(ddl1.SelectedItem, OpInfo).DataTable
GridView1.DataBind()
End If
End Sub
Protected Sub btn1_Click(sender As Object, e As EventArgs) Handles btn2.Click
If ddl1.SelectedIndex <> -1 Then CType(ddl1.SelectedItem, OpInfo).Run
End Sub
Protected Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
If ddl1.SelectedIndex <> -1 Then CType(ddl1.SelectedItem, OpInfo).Remove
End Sub
End Class