Determining which control fires an event in a listview - asp.net

I asked this on the telerik forums, but sometimes responses can be slow there. I was wondering if anyone here know how to go about this.
I am in a situation where a user will have a variable number of items and beside each item I want a RadNumericTextBox. I was thinking of using the RadListView and setting the template to be the item name and a RadNumericTextBox associated with it. I want to ignore the edit, create, and the more advanced features of the RadListView. I just want a list of items with the input boxes that will auto post back when a user has changed the value.
The problem I am facing is when a user changes a number in the text box, how do I know which text box this is? I was looking to see if there was a attribute on RadNumericTextBox that could hold an arbitrary value such as my item key so I would know which number they changed. However, I don't see such an attribute.
Is there some way I can make a determination which text box they edited when I auto post back?
In case anyone asks, I do not want to force my user to click a button to make the row go into edit mode, change the number, then click a button to save the row.

You could do this with a Repeater control.
Include a RadNumericTextBox in the repeater's item template, and then write a server-side event handler for it. The client ID of the text box can be accessed through the event handler's sender object, but if that isn't enough information you can rely on the repeater's data source to relate whatever data you need with each text box.
The simplest way might be to use the Label attribute of the text box. Here's an example:
ASPX:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<br />
<telerik:RadNumericTextBox ID="radNTB" runat="server" AutoPostBack="true" OnTextChanged="radNTB_TextChanged" ClientIDMode="Predictable"></telerik:RadNumericTextBox>
</ItemTemplate>
</asp:Repeater>
VB:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim dt As New DataTable
dt.Columns.Add("Column1", GetType(String))
For i As Integer = 1 To 5
Dim row = dt.NewRow
row.Item("Column1") = "TextBox" & i.ToString
dt.Rows.Add(row)
dt.AcceptChanges()
Next
Repeater1.DataSource = dt
Repeater1.DataBind()
End If
End Sub
Private Sub Repeater1_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
Dim tb As RadNumericTextBox = e.Item.FindControl("radNTB")
tb.Label = DataBinder.Eval(e.Item.DataItem, "Column1").ToString()
End If
End Sub
Public Sub radNTB_TextChanged(sender As Object, e As EventArgs)
Dim ntb As RadNumericTextBox = sender
Response.Write(ntb.Label)
End Sub

What you can do is by the item your binding the listview with the data source,
make the id of the RadNumericTextBox equals to your item key that you want to pass.
In the RadNumericTextBox TextChanged event cast the sender object to RadNumericTextBox type. in this
case you will get the unique item key you are looking for.
example :
<asp:FormView ID="frmViewPicture" runat="server">
<EditItemTemplate>
<telerik:RadNumericTextBox ID='Eval("ItemKey")'
OnTextChanged="radTxtNewPrice_TextChanged" AutoPostBack="true">
</telerik:RadNumericTextBox>
</EditItemTemplate>
</asp:FormView>
Make sure the item key is unique and available in your data source, other wise you will get an exception.
protected void radTxtNewPrice_TextChanged(object sender, EventArgs e)
{
Telerik.Web.UI.RadNumericTextBox txtRadNumericTextBox= (Telerik.Web.UI.RadNumericTextBox)sender;
var itemKey = txtRadNumericTextBox.ID;
// Do Your Logic Here
}
Hope this is helpful.

Related

How do I assign OnRowCommand to multiple IDs ASP.net and VB.net Backend

I have 2 GridViews with separate IDs
I need the backend code to update the one being viewed when a button is clicked.
` Protected Sub savestatus(sender As Object, e As EventArgs)
Dim btn As Button = TryCast(sender, Button)
Dim row As GridViewRow = CType(((CType(sender, Button)).NamingContainer), GridViewRow)
Dim rowindex As Integer = row.RowIndex
Dim code As String = GridView1.DataKeys(row.RowIndex).Values(0).ToString()
Dim type As Int32 = GridView1.DataKeys(row.RowIndex).Values(1)
Dim statusid As Integer
Dim checkLocked, checkerror As CheckBox
' For Each row As GridViewRow In GridView1.Rows
checkLocked = CType(GridView1.Rows(rowindex).FindControl("lock"), CheckBox)
checkerror = CType(GridView1.Rows(rowindex).FindControl("error"), CheckBox)
If checkerror.Checked Then ' error
statusid = 2
End If
If checkLocked.Checked Then
statusid = 3
End If`
How do I make the GridView1 a variable depending on which grid view the button is pressed in.
Ok, it would have helped a lot to at least show the button and a few rows of the gridview markup.
There are about 10 ways to do this. (really !!!).
However, in your case, two check boxes, and you need actions to occur when a check box is changed – AND say change the other one!!
Now I am using two check boxes – but it could be a text box or whatever I change.
So, say I have this grid markup
Some columns + TWO un-bound check boxes.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="HotelName" HeaderText="HotelName" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Province" HeaderText="Province" />
<asp:TemplateField HeaderText="Good">
<ItemTemplate>
<asp:CheckBox ID="chkGood" runat="server"
AutoPostBack="true"
OnCheckedChanged="chkGood_CheckedChanged"
MyRowID ='<%# Container.DataItemIndex %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Bad">
<ItemTemplate>
<asp:CheckBox ID="chkBad" runat="server"
AutoPostBack="true"
OnCheckedChanged="chkBad_CheckedChanged"
MyRowID ='<%# Container.DataItemIndex %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Ok, and now the code to load the grid:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack = False Then
Using cmdSQL As New SqlCommand("SELECT ID, HotelName, City, Province from tblHotels",
New SqlConnection(My.Settings.Test3))
cmdSQL.Connection.Open()
GridView1.DataSource = cmdSQL.ExecuteReader
GridView1.DataBind()
End Using
End If
End Sub
And thus we have this:
Ok, so far - very simple.
Now note CLOSE at the markup for the two check boxes.
And while dropping a button or whatever on a normal form - you can then double click to JUMP to the code behind event/stub?
Well, for buttons or whatever you drop INSIDE of a grid, you can't double click on the control to create + jump to the code behind stub.
But, WHILE in the markup, you can start typing the event, and you get this:
Note VERY careful how the intel-sense pops up a option to create the event. So click on that option. Nothing seems to happen, but NOW we get a code stub behind.
So, we have this code stub for the chkOk event:
Protected Sub chkGood_CheckedChanged(sender As Object, e As EventArgs)
Dim ckBox As CheckBox = sender
Dim RowID As Integer = ckBox.Attributes.Item("MyRowID")
Dim gvRow As GridViewRow = GridView1.Rows(RowID)
If ckBox.Checked = True Then
' do whatever if true - say un-check the "bad" check box
Dim ckBoxBad As CheckBox = gvRow.FindControl("chkBad")
ckBoxBad.Checked = False
Else
' code here if the user just un-checked the "good" check box
End If
End Sub
Note a few things:
We pick up the button click - then shove it into a checkbox control. This is just a lot easier to get the check box value, and our CUSTOM MyRowID
(and this works if it was a button for example).
We then get the custom made up Attribute we added called "MyRowID"
MyRowID ='<%# Container.DataItemIndex %>'
You can see the expression in the Markup - it passes the current row id. Sometimes, I'll pass other values from the row and you can do this like this:
<asp:CheckBox ID="chkBad" runat="server"
AutoPostBack="true"
OnCheckedChanged="chkBad_CheckedChanged"
MyRowID ='<%# Container.DataItemIndex %>'
MyPKID = '<%# Eval("ID") %>' />
So in above, I pass both RowID and a custom MyPKID (so the Eval() expression can be used to pass any valid data row avaialble at binding time. Its often handy then having to grab and mess with a data row - you JUST grab the button from sender - and you don't care about gridview or anything else to get a few extra values. (just a FYI tip). So for example, I REALLY don't want the PK row id as the first row. So I could remove it and STILL use the above idea to PASS the pk row id - all columns can be used - even if a control is NOT in the grid - as long as the column exists during the data binding process - you can grab it.
So, now we pick up the current GridRow - and we are free to modify whatever we want on that row.
In my simple example, we pick up the OTHER check box - and un-check if it was checked. But we could say update other things on that row.
And I did the same for the chkBad check box. And I have really the same as the first chkBox code stub. Eg this:
Protected Sub chkBad_CheckedChanged(sender As Object, e As EventArgs)
Dim ckBox As CheckBox = sender
Dim RowID As Integer = ckBox.Attributes.Item("MyRowID")
Dim gvRow As GridViewRow = GridView1.Rows(RowID)
If ckBox.Checked = True Then
' user checked the bad box, un-check the good one
Dim ckBoxGood As CheckBox = gvRow.FindControl("chkGood")
ckBoxGood.Checked = False
Else
' code here if the user just un-checked the "bad" check box
End If
End Sub
So in above we just hard right past the GridView bult in events.
So in above, if you check one box and the other is checked - we un-check it. Needless to say, I would use a button list, or a checkbox list, and that above code would of course then not be required. But it still a good example on how to pluck/get the current row. And then get/pluck controls from that row.
Note that for the first 3 rows (the databound), you can NOT use findControl, and they are referenced using the gvRow.Cells(0) (starting at 0 to N columns. So no findcontrol is required for these databound columns or autogenerated ones. They do NOT have a name - you have to use number starting at 0 in the cells collection. Of course for "templated" controls that we added as per above? Then you do in fact use findcontrol as per above.

Select Gridview Row - Send to next page

I'm new to this area of programming (ASP.NET VB) and i'm wondering if anyone has a kick start for me so that I can get things moving on my end which I would greatly appreciate! I'm currently using code to select an entire row (PatientName & Room ) in a Gridview without a "select" button (Below). I would then like to pass these from the row to the next page. The receiving page would have the two in labels. This is where i'm lost.
I know there are examples out there but i can not find an example that fits my case unless someone can point me in the right direction. Thank you
Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
'Allows you to "select"/Highlight a row without a select button
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes("onmouseover") = "this.style.cursor='pointer';this.style.backgroundColor = '#87CEFF';"
e.Row.Attributes("onmouseout") = "this.style.textDecoration='none';this.style.backgroundColor = '#FFFFFF';"
e.Row.ToolTip = "Click to select row"
e.Row.Attributes("onclick") = Me.Page.ClientScript.GetPostBackClientHyperlink(Me.GridView1, "Select$" & e.Row.RowIndex)
End If
End Sub
I would highly suggest including a Select button. The Select Event that it fires would give you all the information you need to find the data you want to display on the next page.
As for the passing of the data, QueryString is the best. Session would be my second choice if you don't want to have it available in the URL (and don't want to encrypt or otherwise obfuscate it).
You can accomplish this in a couple of ways. This is just one way of getting what you need. Say you have the following Data structure.
Public Class Patient
Private patient_Id As Integer
Public ReadOnly Property PatientID As Integer
Get
Return patient_Id
End Get
End Property
Public Property PatientName As String
Public Property Room As Integer
Public Sub New(_patientId As Integer, ByVal _PatientName As String, ByVal _Room As Integer)
patient_Id = _patientId
PatientName = _PatientName
Room = _Room
End Sub
End Class
We need a PatientID to easily find a Patient from a list, an array, etc. In your GridView you can add a HiddenField with the ID of your patients, like this:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Patient Name">
<ItemTemplate>
<asp:HiddenField runat="server" ID="hiddenPatientID" Value='<%# Eval("PatientID")%>' />
<asp:Label ID="lblPatientName" runat="server" Text='<%# Eval("PatientName")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Room">
<ItemTemplate>
<asp:Label ID="lblPatientRoom" runat="server" Text='<%# Eval("Room")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
That will make the ID of your patient available for your use. You may also access the other information from your patien (PatientName, PatientRoom), I am using PatientID for the sake of this example. The way you access this data from code behind, is by implemmenting the RowDataBound event of your GridView control, like this:
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim selectedPatient As Integer = -16
selectedPatient = DirectCast(e.Row.Cells(0).FindControl("hiddenPatientID"), HiddenField).Value
If selectedPatient > 0 Then
e.Row.Attributes("onclick") = Response.Redirect("~/MyOtherPage.aspx?&PatientID=" & selectedPatient)
End If
End If
End Sub
Simple, by using the row's FindControl function, you can access the HiddenField's data, which is in this case your patient's id. You can use this ID in the next page to get to the patient (getPatientById method?) or you could just use the patient's data directly.

add an initial line-through text-decoration on a label in a asp listview

My goal is to display a list of tasks with checkboxes. Checking the checkbox for a task will update the display of the task so a line-through text decoration will appear. Everythign is working great, except I can't figure out how to initially show the list of tasks with a line-through if they are completed, or normal if the task is not completed. Here's code excerpts:
<asp:Listview .../>
...
<ItemTemplate>
<asp:HiddenField ID="TaskCompleted" runat="server" Value='<%#Bind("TaskCompleted")%>'/
<asp:Checkbox ID="CompletedCheckbox" runat="server" AutoPostBack="True" OnCheckedChanged="CompletedCheckboxChange" Checked='<%#IIf(Eval("TaskCompleted"), "True", "False")%>' />
<asp:Label id="TaskLabel" text='<%#Eval("TaskDesc")%>' runat="server" />
</ItemTemplate>
...
</asp:ListView>
Then the code behind (minus the database stuff which works fine):
Protected Sub CompletedCheckboxChange( ByVal sender As Object, ByVal e As EventArgs )
Dim Completed As CheckBox = TryCast( sender, CheckBox )
Dim AnnualProgramTasksId as HiddenField = TryCast(Completed.Parent.FindControl("AnnualProgramTasksId"), HiddenField)
Dim TaskLabel As Label = TryCast(Completed.Parent.FindControl("TaskLabel"), Label)
If Completed.Checked Then
'update task displayed, give it a line-through
TaskLabel.Style("text-decoration") = "line-through"
Else
'update task displayed, give it a line-through
TaskLabel.Style("text-decoration") = "none"
End If
End Sub
So this works great, when I click on a checkbox the lable gets a line-through or none based on the checkbox. Only problem is when I initially load the page, I can't figure out how to update the style of TaskLabel to show a line-through or not. I've tried some various routes, but nothing is panning out. Any ideas?
This is how I have always done something like whay you are trying to.
Try
<asp:Label id="TaskLabel" text='<%#Eval("TaskDesc")%>' runat="server"
OnDataBinding="TaskLabel_DataBinding" />
and
Protected Sub TaskLabel_DataBinding( ByVal sender As Object, ByVal e As EventArgs )
Dim Completed As CheckBox = TryCast(DirectCast( sender, Control).Parent.FindControl("CompletedCheckbox"), CheckBox)
CompletedCheckboxChange(Completed, EventArgs.Empty)
End Sub

Why is my CommandArgument Empty?

I have an ASP.Net page, which displays a list of options to the user. When they select from the list, it does a post back and queries a sql server. The results are displayed in a listview below the options in an update panel. Below is a snippet of the ItemTemplate:
<asp:LinkButton Text="Save IT" OnCommand="SaveIt" CommandArgument="<%# Container.DataItemIndex %>" runat="server" />
The DataItemIndex does not appear, so my commandargument is empty. However, the object sender is the button, which shows the item.
Why is the index item not appearing in the CommandArgument?
Could it be the post back? If so, why would it be the post back? Is there a way around it?
Edit:
Sorry, from my attempts to solve it before, I posted bad code, but it still isn't appearing.
Resolution:
I found another work around in that the sender of the OnCommand is the link button, which has the CommandArgument. I will chalk this issue up to be an issue with multiple postbacks and javascript.
You can't use the <%= %> syntax inside properties on a tag with a runat="server" attribute. I'm surprised the code will even run. :)
UPDATE:
You probably want to use the ItemDataBound event on the repeater, find the linkbutton and set the CommandArgument property.
Not very elegant, but here's a VB.NET sample.
Private Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem
Dim b As LinkButton = e.Item.FindControl("btn")
b.CommandArgument = e.Item.ItemIndex
b.DataBind()
End Select
End Sub
You're not setting it
You possibly want
<%# Container.DataItemIndex %>
or
<%= Container.DataItemIndex %>
:)
Try
<asp:LinkButton Text="Save IT" OnCommand="SaveIt" CommandArgument="<%# Container.DataItemIndex %>" runat="server" />
You were missing the "#" sign.
This site really helped me with this problem: http://forums.asp.net/t/1671316.aspx
The issue I ran into was that I was being passed null arguments in the commandargument when I clicked on the button a second time. As the post above explains, this is because commandargument is only set in the databind event. So, to fix this, include a databind event in the page_load sub
Ex. (VB)
Private Sub BindSelectButtons()
'Purpose: bind the data to the select buttons for commandargument to be used
Dim i As Integer
For i = 0 To gridview1.Rows.Count - 1
gridview1.Rows(i).Cells(8).FindControl("btnID").DataBind()
Next
End Sub
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
'Rebind select buttons so that the commandargument refreshes
BindSelectButtons()
End Sub
Make sure View State is enabled
e.Row.EnableViewState = true;

Dynamically add drop down lists and remember them through postbacks

I'm trying to figure out the best way to achieve this:
I need to show a drop down list when the page loads, the default selected value is nothing, or an empty element (such as "-"). When the user selects one value from the list, another drop down list is added below the first one, and so on.
My problem with this is how you make the page to remember the drop down lists created through postbacks and the values they have selected? Do I have to use some kind of array to store them or something?
Thank you very much.
EDIT: I did this example for dynamically add drop down lists and suscribe them to an event handler but the event won't fire.
EDIT 2: Changed the code because I labelled for VB.Net and posted something in C#. I update the code with some trivial improvements, but still won't fire the event :(
Private myDdlArray As New List(Of DropDownList)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Session("ddl") Is Nothing) Then
Session("ddl") = myDdlArray
End If
If (Session("ddlcount") Is Nothing) Then
Session("ddlcount") = 0
End If
myDdlArray = CType(Session("ddl"), List(Of DropDownList))
Dim myDdl As New DropDownList
myDdl = New DropDownList
For Each myDdl In myDdlArray
panel.Controls.Add(myDdl)
panel.Controls.Add(New LiteralControl("<br/>"))
Next
Session("ddl") = myDdlArray
End Sub
Protected Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click
myDdlArray = CType(Session("ddl"), List(Of DropDownList))
Dim ddlCount As Integer = CInt(Session("ddlcount")) + 1
Dim newDdl As New DropDownList
newDdl.ID = String.Format("ddlNPKMaterial{0}", ddlCount)
newDdl.AutoPostBack = True
AddHandler newDdl.SelectedIndexChanged, AddressOf cbo_SelectedIndexChanged
newDdl.Items.Add("Uno")
newDdl.Items.Add("Dos")
newDdl.Items.Add("Tres")
myDdlArray.Add(newDdl)
panel.Controls.Clear()
Dim myDdl As New DropDownList
myDdl = New DropDownList
For Each myDdl In myDdlArray
panel.Controls.Add(myDdl)
panel.Controls.Add(New LiteralControl("<br/>"))
Next
Session("ddl") = myDdlArray
Session("ddlcount") = ddlCount + 1
End Sub
Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
Session("ddl") = Nothing
Session("ddlcount") = Nothing
End Sub
Protected Sub btnShow_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnShow.Click
' Insert brain here
End Sub
Public Sub cbo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Write(CType(sender, DropDownList).ID)
End Sub
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
If (Session("ddl") Is Nothing) Then
panel.Controls.Clear()
End If
End Sub
in the aspx I have:
<form id="form1" runat="server">
<div>
<asp:Button ID="btn" runat="server" Text="Add" />
<asp:Button runat="server" ID="btnShow" Text="Tell me" />
<asp:Button runat="server" ID="btnReset" Text="Reset" />
<br />
<asp:Panel runat="server" ID="panel">
<asp:GridView runat="server" ID="grd">
<Columns>
<asp:BoundField HeaderText="Id" DataField="Id" />
</Columns>
</asp:GridView>
</asp:Panel>
</div>
</form>
I'll use OnLoad to recreate the controls, but if you're not storing the # of controls in ViewState (you could put it into Session, a HiddenField, Cookie, etc.) then you can do it in OnInit. Either way should still fire the postback event.
<asp:PlaceHolder id="phDdl" runat="server">
<asp:DropDwonList id="ddl" runat="server"
OnSelectedIndexChanged="ddl_SelectedIndexChanged" />
</asp:PlaceHolder>
<script runat="server">
int DdlIndex {
get {
object o = ViewState["_ddlIndex"];
if (o == null) {
ViewState["_ddlIndex"] = 0;
}
return (int)ViewState["_ddlIndex"];
}
set {
ViewState["_ddlIndex"] = value;
}
}
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
for (int i = 0; i < DdlIndex; i++) {
AddDropDownList(i);
}
}
void ddl_SelectedIndexChanged(object sender, EventArgs e) {
var ddl = (DropDownList)sender;
if (ddl.SelectedIndex > 0) {
AddDropDownList(DdlIndex++);
}
}
void AddDropDownList(int i) {
var ddl = new DropDownList();
ddl.Id = string.Format("ddl{0}", i);
ddl.SelectedIndexChanged += ddl_SelectedIndexChanged;
// add items
phDdls.Add(ddl);
}
</script>
You can simply have both dropdown lists exist in your asp code with only one visible on first page load. So something like...
<asp:DropDownList ID="mainDDL" Visible="true" runat="server">
<asp:ListItem Text="Elements here" Value="0" />
<asp:ListItem Text="More elements" Value="1" />
</asp:DropDownList>
<asp:DropDownList ID="dynamicDDL" Visible="false" runat="server">
<asp:ListItem Text="This is an element of the dynamic DDL" Value="3" />
</asp:DropDownList>
And then when, say the "More Elements" item selected, switch the dynamicDDL's visibility to true.
Then on each postback, on the Page_Load event, check what the value of mainDDL is. If it is 0, set dynamicDDL to have visible=true
Edit:
Okay, I took a stab at this. There is some headway in this, however, and maybe it will lead us to some clues.
To start off, we DO need an array to store this. We will need a static array of DDLs and a static integer to count our elements. These can be defined as...
Private Shared ddl_arr As DropDownList() = New DropDownList(100) {} 'max, 100 ddls.
Private Shared ddl_count As Integer = 0
Now, we'll need a panel to store our DDLs in. This is simple asp scripting, such as...
<asp:Panel ID="parentPanel" runat="server">
<asp:DropDownList ID="mainDDL" AutoPostBack="true" Visible="true" runat="server">
<asp:ListItem Text="Elements here" Value="0" />
<asp:ListItem Text="More elements" Value="1" />
</asp:DropDownList>
</asp:Panel>
So now, on our page load we will want to load any of our dropdowns that we have saved so far. This can be coded in a way such as..
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
If TypeOf ddl_arr(0) Is DropDownList Then
For Each ddl As DropDownList In ddl_arr
add_ddl(ddl)
Next
End If
Catch ex As Exception ' this is a bad idea, but for brevity..
End Try
End Sub
Our add_ddl method will simply add our new drop down to the parentPanel.
Protected Sub add_ddl(ByVal ddl As DropDownList)
Try
parentPanel.Controls.Add(ddl)
'add any formatting you would like after each ddl here.
Catch ex As Exception
End Try
End Sub
And finally, our method when we change the ddl. This creates a brand new ddl, gives it an id (and whatever properties you may want for it), adds it to the array, increments our counter, and adds it to the page..
Protected Sub mainDDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles mainDDL.SelectedIndexChanged
Dim newDDL As DropDownList = New DropDownList()
newDDL.ID = "ddlID" & ddl_count 'we will need to store a new ID for each one. So, dynamically generate this.
'continue to add properties to your ddl in this fashion...
ddl_arr(ddl_count) = newDDL
ddl_count = ddl_count + 1
add_ddl(newDDL)
End Sub
This method should definitely check for the end of the array (among other things), but lets keep things simple. This code will only add a new DDL whenever you change the index of the ORIGINAL DDL. You will need to set each newly created DDL to have a method called (performing the above instructions) whenever the selected index changes for all of those newly crafted DDLs.
Hopefully this gets you in the right direction. This was way less organized than I'd hoped, sorry!
Dynamic controls are pretty tricky to create. I would start by reading this.
Create and add the dropdowns during the Page_Init method and generate the names based on the backing data that you're gathering. When getting input from them, either read their returned values off of the page using the FindControl() method or by getting the returned value of the control from the Request.Form[]. FindControl uses the id of the control, and Requst.Name uses the client id - you can get this information using the same function you use to generate the names.
Overall, ASP.NET isn't very good at working with generated controls, but by keeping the data you're working with in a model or datastructure separate from the controls that are being generated it isn't difficult to both create the controls and retrieve the results.

Resources