asp.net consultancy chorley ASP.NET Server Side Scripting Wigan
Website Development Radcliffe

IT Services And Support

Email : ren@techsolus.co.uk
Mobile Phone : 0788 68 41 411
Answerphone : 01204 469683
bespoke invoice systems Standish Information Technology Advice Worsley
    development systems Ramsbottom
data manipulation Farnworth access databases Whitefield SQL connections Atherton
online accessible software Leyland software compatible Coppull


Get connected and Online Tottington
remote management Makerfield code and design Aspull

ASP.Net Repeater, Dropdownlist, Databind & SelectedValue - 19/11/2015

ARGH! This is another one with incredibly complex solutions that are totally unnecessary. I suspect people write using programs like Visual Studio so they don't actually need to understand what it is they are creating. Anyhow...my take on how to put a DropDownList into a repeater that is Databound. Then also how to set one of the values to the data that created the repeater.
 
Here's the code...
 
sub mydatabind(Sender As Object, e As RepeaterItemEventArgs)
  qwe = "SELECT * FROM format ORDER BY formatf"
 
  runcommand = new oledbcommand(qwe, quoteconn)
  dbread = runcommand.executereader
 
  dim formatx as dropdownlist = e.Item.FindControl("formatx") 
  dim formathidden as literal = e.Item.FindControl("formathidden") 
 
  formatx.datasource = dbread
  formatx.datatextfield = "formatf"
  formatx.datavaluefield = "format_id"
  formatx.databind
  formatx.selectedvalue = formathidden.text
end sub
 
-----------------------------------------------------------------------------------------------------------------------------------------------
 
[asp:repeater id = "mygrid" onitemdatabound = "mydatabind" runat = "server"]
[itemtemplate]
 
[asp:DropDownList id = "formatx" runat = "server" /]
[asp:literal id = "formathidden" text = '[%# container.dataitem("format_id") %]' visible = "false" runat = "server" /]
 
[/itemtemplate]
[/asp:repeater]
 
Let me explain...
 
When we start the repeater we include the "onitemdatabound" thingymajig. This means that whenever we are looping through each itemtemplate creation we pop off to the subroutine, in this case "mydatabind". So if you have 100 items in your repeater from 100 products in your database, you're going to end up running the "mydatabind" subroutine 100 times! So beware of loading up the server for BIG repeaters.
 
The dropdown list just has the ID "formatx". Also below we have the literal "formathidden" and this is set to visible false. This won't be seen on the page but the data within can also be grabbed in the "mydatabind" sub. What we are using this for is to pass the data from the repeater to the sub. 
 
So for example if the database say that the previously selected format was number 22, the number 22 is put into the hidden literal "formathidden". When we populate the dropdownlist in the subroutine we then finally set the selectedvalue to 22 whcih is taken from "formathidden"
 
Works fer me :)

Post A Comment

Name Comment
programming services asp.net specialist
Valid XHTML 1.0 Transitional
Admin
GD