Public Class bum: Inherits System.Web.UI.Page Public ddl As DropDownList Public placehold As PlaceHolder Public Sub Page_Load ddl = New DropDownList Dim ar As New ArrayList ar.Add("2") ar.Add("4") ddl.DataSource = ar ddl.DataBind() ddl.AutoPostBack = true placehold.controls.add(ddl) Trace.write(ddl.SelectedItem.Value.ToString()) End Sub End Class
yep fordi du siger new til din dropdownliste i page load...så husker den ikke viewstate på den.
Altså, viewstate køre før page load, og hvis kontrollen ikke er oprettet på det tidspunkt hvor viewstate køre, så bliver asp.net enig med sig selv om at du sikkert ikke har brug for den mere og smider den tidligere værdi ud.
Men hvis du siger new til din dropdownlist på init tidspunktet, er det en anden sag, fordi init køre FØR viewstate :) og så findes kontrollen jo når viewstate køre, og derfor husker asp.net den tidligere værdi.
Hvis du tilføjer kontrollen i Page_Load kan du også bare "vente lidt" med at spørge på værdien....
Prøv den her :
Imports System Imports System.Collections Imports System.Web Imports System.Web.UI.Webcontrols Imports System.Web.UI.Htmlcontrols
Public Class bum : Inherits System.Web.UI.Page Public WithEvents ddl As DropDownList Protected Form1 As HtmlForm Public Sub Page_Load() ddl = New DropDownList Dim ar As New ArrayList ar.Add("2") ar.Add("4") ddl.DataSource = ar ddl.DataBind() ddl.AutoPostBack = True AddHandler ddl.SelectedIndexChanged, AddressOf ddl_SelectedIndexChanged Form1.Controls.Add(ddl) Response.Write("I Page_Load : " + ddl.SelectedItem.Text) End Sub Protected Overrides Sub OnPreRender(ByVal e As EventArgs) Response.Write("<br>I OnPreRender : " + ddl.SelectedItem.Text) End Sub Private Sub ddl_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Response.Write("<br>I en EventHandler : " + ddl.SelectedItem.Text) End Sub
Private Sub InitializeComponent()
End Sub End Class
Du vil kunne spørge på værdien i en given eventhandler (også dens egne), eller i f.eks. PreRender fasen.
Der er samme problem med vb________________________________ Imports System Imports System.Collections Imports System.Web Imports system.Web.UI.Webcontrols Imports system.Web.UI.Htmlcontrols
Public Class bum: Inherits System.Web.UI.Page Public ddl As DropDownList Public placehold As PlaceHolder Public Sub Page_Init ddl = New DropDownList End Sub Public Sub Page_Load Dim ar As New ArrayList ar.Add("2") ar.Add("4") ddl.DataSource = ar ddl.DataBind() ddl.AutoPostBack = true placehold.controls.add(ddl) Trace.write(ddl.SelectedItem.Value.ToString()) End Sub End Class
Public Class bum : Inherits System.Web.UI.Page Public WithEvents ddl As DropDownList Protected enform As HtmlForm Public Sub Page_Load() ddl = New DropDownList Dim ar As New ArrayList ar.Add("2") ar.Add("4") ddl.DataSource = ar ddl.DataBind() ddl.AutoPostBack = True AddHandler ddl.SelectedIndexChanged, AddressOf ddl_SelectedIndexChanged enform.Controls.Add(ddl) Response.Write("I Page_Load : " + ddl.SelectedItem.Text) End Sub Protected Overrides Sub OnPreRender(ByVal e As EventArgs) Response.Write("<br>I OnPreRender : " + ddl.SelectedItem.Text) End Sub Private Sub ddl_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Response.Write("<br>I en EventHandler : " + ddl.SelectedItem.Text) End Sub
Okay... Det viser sig at jeg ikke bare liiige kan bruge det med OnPreRender eller SelectedIndexChanged... Jeg skal bruge værdien fra DropDownListen i min Page_Load... I denne kode bliver min str ved med at være tom i min Page_Load...
Public Class bum : Inherits System.Web.UI.Page Public WithEvents ddl As DropDownList Public placehold As PlaceHolder Public str As String Public Sub Page_Load() ddl = New DropDownList Dim ar As New ArrayList ar.Add("2") ar.Add("4") ddl.DataSource = ar ddl.DataBind() ddl.AutoPostBack = True AddHandler ddl.SelectedIndexChanged, AddressOf ddl_SelectedIndexChanged placehold.Controls.Add(ddl) Trace.Write("Selected : " + ddl.SelectedItem.Text) Trace.Write("Pub str " & str) End Sub Protected Overrides Sub OnPreRender(ByVal e As EventArgs) str = ddl.SelectedItem.Text Trace.Write("preRender " & str) End Sub Private Sub ddl_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) str = ddl.SelectedItem.Text Trace.Write("Changed " & str) End Sub
Imports System Imports System.Collections Imports System.Web Imports System.Web.UI.Webcontrols Imports System.Web.UI.Htmlcontrols
Public Class bum : Inherits System.Web.UI.Page Public WithEvents ddl As DropDownList Protected Form1 As HtmlForm Public Sub Page_Load() Response.Write("I Page_Load : " + ddl.SelectedItem.Text) End Sub Protected Overrides Sub OnInit(ByVal e As EventArgs) ddl = New DropDownList Dim ar As New ArrayList ar.Add("2") ar.Add("4") ddl.DataSource = ar ddl.DataBind() ddl.AutoPostBack = True AddHandler ddl.SelectedIndexChanged, AddressOf ddl_SelectedIndexChanged Form1.Controls.Add(ddl) End Sub Protected Overrides Sub OnPreRender(ByVal e As EventArgs) Response.Write("<br>I OnPreRender : " + ddl.SelectedItem.Text) End Sub Private Sub ddl_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Response.Write("<br>I en EventHandler : " + ddl.SelectedItem.Text) End Sub
Ehm.. Tak... Jeg valgte bare at have en button-event som aflæser værdien og bruger, og det fik jeg til at virke.. Men læg et svar, så giver jeg point..
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.