ASP.Net DropDownList, Repeater, Databind...ARGH! - 08/08/2012
This is yet another one of those things that should be sooooo easy but every coder out there seems to make it as complex, convoluted and difficult as humanly possible. I got there eventually.
You've got a lovely repeater, like me you prefer repeaters as the datagrid makes a mess of your beautiful pretty code and it's more like a dark art than sensible code. Now you've decided you want a nice dropdownlist in that repeater, and that dropdownlist needs to be populated from another table in the database, or some other set of data.
I'll tease no more, here's the code...
WITHIN THE REPEATER... note replace '[' ']' with html less than greater than
[asp:dropdownlist id = "rollformat"
runat = "server"
DataValueField = "uidf"
datatextfield = "format"
datasource = "[%# Getformats %]"
SelectedValue = '[%# container.dataitem("rollformat") %]'
So what's what? the two things you're interested in are datasource = "[%# Getformats %]" and SelectedValue = '[%# container.dataitem("rollformat") %]' Datasource is where it's really at, as far as I can tell this nudges the dropdownlist to get it's list from whatever's in datasource. And within the datasource where going to point to a function, just a regular, boring old function that returns a dataset for the dropdownlist to work with, fill itself with.
So here's the function "Getformats" as referred to under datasource
Function Getformats() as DataSet
dim mydataset as new dataset
Dim myadap As OleDbDataAdapter
qwe = "SELECT * FROM formats"
myadap = New OleDbDataAdapter(qwe, dbconn)
Now this is not COMPLETE, you may need to work out a few variables etc, but the idea is to fill a dataset, either in this case from an sql query, or maybe a datatable or whatever data you have, as long as you can create the dataset with suitable data in it, you're away. Where it says return mydataset, your passing the dataset back to the dropdownlist with it's contents, the dropdownlist uses those contents to populate the list!
Now why is it made so COMPLEX everywhere else, they use OnItemDataBind and strange convoluted methods. Grrrr!
Post A Comment