DropDownList problem - argh

  • Thread starter Thread starter Martin Schmid
  • Start date Start date
M

Martin Schmid

When testing for the value of my DropDownList.SelectedItem.Text - it always
returns the Text of the first element of the list. It is a databound
control, and I have a breakpoint in my code to test the value. I am trying
to insert the Text from this control into a field in a database, and it
perpetually inserts the text from the first item of the list instead of the
selected item.

What the heck could I be missing here?
 
I noticed that OE stripped the attachments... here they are pasted as text:

WebForm1.aspx
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="DropList.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:DropDownList id=DropDownList1 runat="server" DataSource="<%# arrTest
%>">
</asp:DropDownList>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
<asp:Literal id="Literal1" runat="server"></asp:Literal>
</form>
</body>
</HTML>


WebForm1.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace DropList
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Literal Literal1;
protected System.Web.UI.WebControls.DropDownList DropDownList1;

public string[] arrTest={"One","Two","Three"};

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
DropDownList1.DataBind();

}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
Literal1.Text=DropDownList1.SelectedItem.Text;
}
}
}
 
I found a solution... but I don't yet understand it... can someone shed some
light?

I moved the DropDownList.DataBind(); expression into :
if(!IsPostBack)
{
drpMyDropDownList.DataBind();
}

Now, where I have my break clause to test the value of
drpMyDropDownList.SelectedItem.Text, it has the selected item value as
expected.
 
I found a solution... but I don't yet understand it... can someone
shed some light?

I moved the DropDownList.DataBind(); expression into :
if(!IsPostBack)
{
drpMyDropDownList.DataBind();
}

Now, where I have my break clause to test the value of
drpMyDropDownList.SelectedItem.Text, it has the selected item value as
expected.

If you have the DataBind outside of the !IsPostBack then it is done
everytime when the Page class is instantiated BEFORE any other events of
that page ocure. Even bevore a change event. You would be overwriting the
SelectedItem.Text to its original value.

Now you do the Bind just once when the page is displayed for the first time
and in the postback it is not newly binded. therefore the selectedItem will
have the text that's been selected on the webpage.
 
I noticed that OE stripped the attachments... here they are pasted as text:
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
DropDownList1.DataBind();

}

This should be

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
DropDownList1.DataBind();
}

--------------

What's happening is that when the page is posted back, Page_Load runs
*before* the event handlers. Your original Page_load code is rebinding
the DropDownList which reinstates the original values, and then the
Button1 event handler is picking up those original values.
 
Sort of what I was concluding. Leave it to Microsoft Press to publish crap
code. 'Developing Web Applications with Microsoft VB.NET and VC#.NET' has
an example in which this is the issue..
 
Sort of what I was concluding. Leave it to Microsoft Press to publish
crap code. 'Developing Web Applications with Microsoft VB.NET and
VC#.NET' has an example in which this is the issue..

This is not the first MS Press Book where I found fatal errors. Also
ADO.NET Step By Step has very stupid errors, showing that the writers
didn't even bother about testing the code they publish.

Well, it's a bad situation, everyone is thinking only about time-to-market
and no more about quality of books. Wrox died this year in march so my
favourite publisher is also gone...
 
Back
Top