Unable to get values of a bounded control when postingback, How do I get previous state?

the second DDL doesn't work, cuz the items are refilled in the postback event.
and of course if I fill in the items only if Not Page.IsPostBack, it doesn't fill anything.

however, how can I get the previous DropDownList2 instantiated control, I mean I want to get the control as it would be looking before the user changed index + new selected index as an argument.

or else - maybe I have to do it completely in a different way (?).

 

<%@ Page Language="VB" %> 
<%@ Import Namespace="System.Linq" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server"> 
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
DropDownList2.DataSource = Enumerable.Range(Now.Year, 14)
DataBind()
End Sub

Protected Sub DropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
'I try to put a breakpoint here and it doesn't stop!!!
Label1.Text = DropDownList1.SelectedValue
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
    <div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
<asp:AsyncPostBackTrigger ControlID="DropDownList2" EventName="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"   
OnSelectedIndexChanged="DropDownList_SelectedIndexChanged" AutoPostBack="True" >
<asp:ListItem Text="0" Value="0" Selected="True" />
<asp:ListItem Text="1" Value="1"/>
<asp:ListItem Text="2" Value="2"/>
<asp:ListItem Text="3" Value="3"/>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" 
OnSelectedIndexChanged="DropDownList_SelectedIndexChanged">
</asp:DropDownList>
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
    </div>
    </form>
</body>
</html>



  
Shimmy
0
weitzhandler
9/3/2008 2:29:12 AM
asp.net.ajax-discussion 13973 articles. 1 followers. Follow

5 Replies
1061 Views

Similar Articles

[PageSpeed] 57

If I understand you correctly, you are trying to fill a second dropdown based on the first dropdown.  If this is the case, why not use the CascadingDropDown control: http://www.asp.net/ajax/ajaxcontroltoolkit/samples/CascadingDropDown/CascadingDropDown.aspx.  There is also an example with using it with a database: http://www.asp.net/ajax/ajaxcontroltoolkit/samples/Walkthrough/CCDWithDB.aspx

It's much cleaner than using an UpdatePanel to do the same thing.

-Damien


Visoft, Inc - Home | Blogs

Latest Blog Post: SQL 2008 x64 and VS2008 SP1
0
dwhite
9/3/2008 2:40:42 AM

dwhite:
If I understand you correctly, you are trying to fill a second dropdown based on the first dropdown.  If this is the case, why not use the CascadingDropDown

 

you probably don't...
but thank s for your reply anyway


Shimmy
0
weitzhandler
9/3/2008 2:44:36 AM

Hi,

From your description, I understand that you would like to bind source to the DropDownList2 and change the Label1’s text when the DropDownList2 ‘s selection is changed. The issue is when the selection changed  the page is reloaded and the DropDownList2 is bound again.

I have built a test application based on your code, please refer to :

.aspx file 

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="TestDropDownList.aspx.vb"
    Inherits="Test_UpdatePanel.TestDropDownList" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script runat="server"> 
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                DropDownList2.DataSource = Enumerable.Range(Now.Year, 14)
                DataBind()
            End If
        End Sub

        Protected Sub DropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged, DropDownList1.SelectedIndexChanged
            'I try to put a breakpoint here and it doesn't stop!!!
            Label1.Text = DropDownList2.SelectedValue
        End Sub
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
                <asp:AsyncPostBackTrigger ControlID="DropDownList2" EventName="SelectedIndexChanged" />
            </Triggers>
            <ContentTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
                    <%-- OnSelectedIndexChanged="DropDownList_SelectedIndexChanged"--%>
                    <asp:ListItem Text="0" Value="0" Selected="True" />
                    <asp:ListItem Text="1" Value="1" />
                    <asp:ListItem Text="2" Value="2" />
                    <asp:ListItem Text="3" Value="3" />
                </asp:DropDownList>
                <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True">
                </asp:DropDownList>
                <br />
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>
.aspx.vb file 
Public Partial Class TestDropDownList
    Inherits System.Web.UI.Page
End Class
Have my code helped?

Best regards,

Zhi-Qiang Ni


Microsoft Online Community Support

Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as
Answer” if a marked post does not actually answer your question.
0
Zhi
9/5/2008 8:36:39 AM

well for me it doesn't work (in a Web Site not a web application.

please try to create a website and replace the Default.aspx with this one and delete Default.aspx.vb:

 

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Register Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
	Namespace="System.Web.UI" TagPrefix="asp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
	Protected Sub yearDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles yearDropDownList.SelectedIndexChanged
		If yearDropDownList.SelectedValue = Now.Year AndAlso Now.Month > 1 Then
			Dim items As New List(Of ListItem)
			For Each item In monthDropDownList.Items
				If item.Value >= Now.Month Then items.Add(item)
			Next
			monthDropDownList.Items.Clear()
			monthDropDownList.Items.AddRange(items.ToArray)
		End If
	End Sub

	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'when I put a breakpoint here it doesn't even stop!
		If Not IsPostBack Then
			Dim years As IEnumerable(Of Integer) = Enumerable.Range(Now.Year, 14)

			Dim items As New List(Of ListItem)
			For Each y In years : items.Add(New ListItem With {.Text = y, .Value = y}) : Next

			yearDropDownList.DataSource = items
			yearDropDownList.SelectedValue = Now.Year
			yearDropDownList.DataBind()
			AddHandler yearDropDownList.SelectedIndexChanged, AddressOf yearDropDownList_SelectedIndexChanged
		End If
	End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
	<asp:ScriptManager ID="ScriptManager1" runat="server" />
    <div>
		<asp:UpdatePanel ID="UpdatePanel1" runat="server">
		<ContentTemplate>
		<asp:DropDownList ID="yearDropDownList" runat="server" />
		<br />
		<asp:DropDownList ID="monthDropDownList" runat="server">
											<asp:ListItem Selected="True" Value="01">01 (Jan)</asp:ListItem>
									<asp:ListItem Value="02">02 (Feb)</asp:ListItem>
									<asp:ListItem Value="03">03 (Mar)</asp:ListItem>
									<asp:ListItem Value="04">04 (Apr)</asp:ListItem>
									<asp:ListItem Value="05">05 (May)</asp:ListItem>
									<asp:ListItem Value="06">06 (Jun)</asp:ListItem>
									<asp:ListItem Value="07">07 (Jul)</asp:ListItem>
									<asp:ListItem Value="08">08 (Aug)</asp:ListItem>
									<asp:ListItem Value="09">09 (Sep)</asp:ListItem>
									<asp:ListItem Value="10">10 (Oct)</asp:ListItem>
									<asp:ListItem Value="11">11 (Nov)</asp:ListItem>
									<asp:ListItem Value="12">12 (Dec)</asp:ListItem>
		</asp:DropDownList>
		</ContentTemplate>
		</asp:UpdatePanel>
    </div>
    </form>
</body>
</html>
 
the above doesn't work for me.
 
Shimmy
0
Weitzhandler
9/5/2008 9:57:49 AM

Hi,

I have tested your code again based on your situation. I'm afraid the cause is the missing of the "AutoPostBack='True'" in your yearDropDownList's tag.Stick out tongue

Have my suggestion helped?

Best regards,

Zhi-Qiang Ni


Microsoft Online Community Support

Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as
Answer” if a marked post does not actually answer your question.
0
Zhi
9/5/2008 10:25:18 AM
Reply:

Similar Artilces:

Unable to get value of out bound Listbox control
 <option value="29">Party</option> <option value="18">Plumbing</option> <option value="33">Pressure Washers</option> <option value="34">Pumps</option> <option value="35">Rug/Floor Care</option> Hello, I have a list box control that I am binding to a datasource which returns a list of "category" items. However, I am unable to get the value out of the list box once I select it, it simply returns null. For example, to keep it simple, I have a list of categories...

How do I get get values of controls inside datalist?
How do I get values of controls inside datalist?   For example, if I have a label inside header template or item template or footer template? Is there anyway to change property of label or any other controls inside those template as well? I would really appreciate if anybody can just post a sample code or a helpful link about datalist? Would really like to know more about the events fired for datalist, itembound, itemcreating etc?   hey there,I used this page to work out how to use datalists http://www.eggheadcafe.com/tutorials/aspnet/d89d1d96-03f1-4784-bbb2-a3db1af...

POST GET GET GET GET... WTH?
Hi, I'm at a loss to explain this... I have an ASPX page that I can't always navigate to. When I developed the app (it's Starter Kit Portal-based), I did it on my LAN and have had no trouble accessing the page. However, upon deployment, I see the following behavior: 1. POST request goes out looking for MyForm.aspx 2. GET request for MyForm.aspx 3. GET request for MyForm.aspx 4. ... X. GET request Sometimes, the page finally loads. Other times, it just ends up blank. Many times, it doesn't load at ALL the first POST - I just get a "The pa...

FileUpload control value gets clear when another control uses Ajax to retrieve data
Hello all, I have a web form.  There is a DropDownList control inside an update panel and the content is updated when a RadioButtonList control fires SelecteIndexChanged.  I also have another control, a FileUpload control.  If you select a file for the file upload control and then change the value RadionButtonList list control, the selected file in the FileUpload control disappears.How do I prevent RadioButtonList from clearing the value of the FileUpload control?Any help will greatly appreciated,  TIA,Dawa Below is the code: 1 <asp:RadioButtonList ID=&quo...

Get user input from dynamic created control, why i cant get the value???
Hi!, i was tried to dynamically generate some textboxes according the selected number in a dropdownlist, but when i need to get the user input from those textboxes.. i cant get at all!!, Can any1 guide me on tis? Below is my code.Basically, i have 1 dropdownlist (ddltbCount), 1 placeHolder(Pholder1), 1 button(btGetValue), i dont know y i cant post the html here... Private count As Integer = 1 Sub IterateThroughChildren(ByVal parent As Control) For Each c As Control In parent.Controls If c.GetType.ToString.Equals("System.Web.UI.WebControls.TextBox") AndAlso c.ID Is Nothing Then ' CType(c, T...

Ajax Control that Gets Values from Web Service...
I don't know if this is the right place to ask but here it goes.....I have a page with three .ascx files. The first .ascx has a ajax cascading dropdown control that depending on the first selection a list of items are available on the second dropdown. Then depending on what you select on the second one the third dropdown selections are dynamically populated. Then you have a series of other questions that are asked, well when we click continue to go to the next .ascx control we are getting this error:  It is a messagebox that says Object reference not set to an instance of an o...

Get original bound value of DataList control
I am using a DataList control that has a DropDownList in the Edit template.  As part of the Update command handler, I want to look at the current value of the selected item and compare it to the original value from the data source (that is, the value as it exists in the database before the Update is carried out).  I'm trying to find an easy way of doing this but I'm coming up empty.  Is there a way?   Thanks, Graham Powell Well yes, there is an easy way, directly on the DataSet itself. On each row like dataSet.Tables[0].Rows[i] You can ask if it has an ori...

Ajax User Control getting values overwritten
I wrote a User Control that loads an image from a database inside an UpdatePanel.  Everything works great until I throw this User Control inside a GridView.  The GridView initializes fine and then it seems the User Control gets initialized twice and all of its contents get wiped out.  Anyone seen this happen before?...

Unable to get control values after image button click
Hi, All I have trouble with getting control values after I click a 'save' image button. I am doing a editing data process. I use a textbox to display a record data. After I click the 'save' button I want to get the textbox new data. Code in 'save' button click event: listorder = me.txtListOrder.text The error is I am always getting the old data which I displayed in the textbox but not the data I entered later. When I look at the trace Form Collection it shows the new data I entered later. $ContentPlaceHolder1$txtListorder       456 The strange thing ...

Unable to get control values from Datalist in edit mode
HiI'm using a generic collection as a datasource for a datalist. The collection object is a class used to contain a string and a few bools. The code I'm using is taken from the ASP.NET Visual Web Developer Helpfile page titled "Allow users to edit items in datalist webserver controls".In the update method I'm extracting the user entered values from the editItemTemplate as follows:    string s = ((TextBox)e.item.findcontrol("username")).Text; But no matter what I do or what I enter into the username control of the datalist the above bit of c...

Keep getting..."Unable to get project file from server",when using vs.net
When trying to open any existing asp.net project in Visual Studio it keeps giving me the message above. It is also throwing an error when I create the project in the first place, it happens when a dos window is opened, the text in the dos window is running "Common Language Runtime Minidump Utility version 1.0.3705.0" and then VS craps out says there was an error and shuts down. I've re-installed the .net components and framework with no avail, and FrontPage extensions etc.. are all installed. I'm running XP professional and have installed IIS 5.1. This is driving me fec...

Getting control's value which has set through ajax callback
Hi,  I am populating an asp:dropdownlist though java script in an ajax callback. I am selecting a value from that dropdown and trying to retrieve that server side. However, I am not able to get that value server side. I tried to retrieve the control's value directly or using a hiddenfield, but I am not able to get that in code behind. Couuld you please give me a solution for this.   Here is my code: HTML: <div id="divStateDdl"><asp:DropDownList ID="ddlStates" runat="server" Width="210px" Height="25px" /> </di...

How to get cell values when GridView is bound with Template Control Fields
Hi, I have following GridView <asp:GridView ID="gdvProcAdmin" runat="server" AutoGenerateColumns="false" AllowSorting="true"> <Columns> <asp:TemplateField HeaderText="ID" Visible="false"> <ItemTemplate> <asp:Label ID="lblID" runat="server" Text='<%#Eval("ID")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Category" SortExpression="Type"> <ItemTemplate> <asp:Lab...

problem with get value from textbox after get it value in load page
hi every one  i have problem with my web site when i get textbox value in load page   and Changed textbox the page do not take last value .   it take value in load page   this is code:Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)   TextBox1.Text = "aa" End SubProtected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) MsgBox(TextBox1.Text) End Sub   when i press button message write "aa" while i changed the textbox   please if enyone knowen help me &nbs...

Web resources about - Unable to get values of a bounded control when postingback, How do I get previous state? - asp.net.ajax-discussion

Resources last updated: 12/9/2015 12:34:11 AM