Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 17th, 2008, 10:44 PM
Authorized User
 
Join Date: Nov 2006
Posts: 93
Thanks: 0
Thanked 1 Time in 1 Post
Default Why would anyone use MS Widgets?

Sorry, this is just a rant. Every time I try to use one of those nifty widgets Microsoft gives us that are supposed to reduce the amount of code you write (something I love doing), I wind up spending many hours trying to get the widget to do what I want it to.

I'm fairly new to ASP.Net so I thought I'd try some of the Data controls. The FormView looks particularly useful so ... I've spent several hours now trying to get it to work. The multiple templates looks cool, but I can't get it swap to the template I want. Apparently you can't access the events of the controls inside a FormView ... :-(

According to MS's documentation the wizard creates buttons for each template that could be used to change the current mode (template), but it's not clear how to make them work. So I'll go back to writing code, with which I can always get the result I want, and hope that some day Microsoft makes some usable widgets.


What you don't know can hurt you!
__________________
What you don\'t know can hurt you!
 
Old May 18th, 2008, 03:57 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Hi

I'm not sure what the 'widget' you are refering to is, so can't really help. Do you mean an ASP.Net web control? Is it one of the built in ones? If you could try and explain which control you are using and provide code examples then maybe we could help.

Sam

/- Sam Judson : Wrox Technical Editor -/
 
Old May 19th, 2008, 10:29 AM
Authorized User
 
Join Date: Nov 2006
Posts: 93
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Sam,

I'll take the DropDownList (DDL) widget for example. This seems like an extremely valuable tool to anyone who has ever coded one in html using javascript and asp. The problem is in getting it to work. I have an example I added to a project, the purpose (of the DDL) is to change the data displayed on the page based on the users selection on the DDL.

I bound the DDL to a SqlDataSource and that populated the list with no problem. I set "AutoPostBack" to true and set up an event handler for the "TextChanged" event in the Page_Load method. Now we begin to see things fall apart. While changing the selected item does post back to the web site, the "TextChanged" handler method I setup does not get called! Bummer! But the Page_Load gets called and IsPostBack is true so maybe we can use this to process our change. So I look (using the IDE's watch) at the sender object where I can see my DDL and it doesn't even have the list populated, much less what my selection was.

So, in the end, the MS widget DropDownList is, more or less, worthless. Ahh but you say it saved me from having to write all that code to populate the options in my html select statment! True, but I can't use it to effect a change in the webpage...

I find this is generally true of all Microsoft widgets. They only work in a very limited sense. As soon as you want some custom behavior, you need to code the whole thing. That's my gripe!!

What you don't know can hurt you!
 
Old May 19th, 2008, 10:39 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I think you might have been more interested in the SelectedIndexChanged event, rather than the TextChanged.

An no-one calls them widgets, so it would help your understanding (and others) if you called them Web Controls.

I'll be honest, DropDownList is one of the built in web controls, so if you are finding it useless then it is more likely to be because you are misunderstanding something rather than it actually being useless.

/- Sam Judson : Wrox Technical Editor -/
 
Old May 19th, 2008, 11:07 AM
Authorized User
 
Join Date: Nov 2006
Posts: 93
Thanks: 0
Thanked 1 Time in 1 Post
Default

Sam,

Don't get me wrong, I'm not saying Microsoft doesn't do anything of value, I just wish some of their more exotic controls worked in more situations.

By the way the "SelectedIndexChanged" works the same as the "TextChanged" event does ... it doesn't fire at all! Now there may be someway you can tell that it fired but you can't set up an event handler in the code that generated the page and expect to capture the event as you could if this were a DropDownList on a Window's Form.

As for widgets, I may be old enough to be your father so that term may pre-date you by a bit. It's not a technical term but a generic term that has to be understood within the context of it's use.



What you don't know can hurt you!
 
Old May 19th, 2008, 12:09 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Of course the SelectedIndexChanged fires and in a pretty similar way to what you'd see in a Win Forms application. Consider this example:
Code:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  protected void Page_Load(object sender, EventArgs e)
  {
    string[] movies = new string[] { "Reservoir Dogs", "Pulp Fiction", "Killing Zoe" };
    DropDownList1.DataSource = movies;
    DropDownList1.DataBind();
  }

  protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  {
    Label1.Text = string.Format("You selected {0}", DropDownList1.SelectedValue);
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
    <div>
      <asp:DropDownList ID="DropDownList1" runat="server" 
        OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
        AutoPostBack="true">
      </asp:DropDownList>
      <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
  </form>
</body>
</html>
What do you think will happen? Will the label get the right value of the movie you choose?

Nope, it doesn't. Page_Load *always* fires, both on initial load and on PostBack and does so *before* other events like SelecedIndexChanged and Click of a button fires.
What happens when you choose a new item in the list? The list is *repopulated* with the movies, overwriting your selection and resetting things to how they were the first time. Clean list + no selection == no SelectedIndexChanged. Quite likely the behavior you are witnessing.

What's the fix? Don't load the data on PostBack:
Code:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      string[] movies = new string[] { "Reservoir Dogs", "Pulp Fiction", "Killing Zoe" };
      DropDownList1.DataSource = movies;
      DropDownList1.DataBind();
    }
  }

  protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  {
    Label1.Text = string.Format("You selected {0}", DropDownList1.SelectedValue);
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
    <div>
      <asp:DropDownList ID="DropDownList1" runat="server" 
        OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
        AutoPostBack="true">
      </asp:DropDownList>
      <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
  </form>
</body>
</html>
With this code, everything works as expected.

Note that although this is a simple example, the principle applies to many other controls and situations. The fact things don't work as you expect may not have to do with the actual controls, but may have to do with your knowledge and the way you use these controls.

So, instead of complaining about dysfunctional widgets, I think you're better off trying to find out *why* things don't work, widen your knowledge and then fix your code so it does what you want, using the tools that are meant to be used.

BTW: you *can* get events in controls to fire, and you *can* switch templates. However, instead of calling method like ChangeMode, you need to set the DefaultMode property to a value from the FormViewMode enumeration: Insert, Edit or ReadOnly.

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old May 19th, 2008, 01:04 PM
Authorized User
 
Join Date: Nov 2006
Posts: 93
Thanks: 0
Thanked 1 Time in 1 Post
Default

Imar,

The difference between what you did and what I did was in the declaration statement:

Code:
<acp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" 
  OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</acp:DropDownList>
I was registering the event handler in Page_Load ...

Code:
protected void Page_Load(object sender, EventArgs e) {
  if(!Page.IsPostBack)
  {
    DropDownList1.OnSelectedIndexChanged += 
      new EventHandler(DropDownList1_SelectedIndexChanged);
  }
}
Obviously, this didn't work.

I prefer to find out why things work and why they don't work, but it can be challenging to find this information.

Thanks for the enlightenment!

What you don't know can hurt you!
 
Old May 19th, 2008, 01:13 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi David,

It *does* work, provided you use the right code.

First of all, you need to use DropDownList1.SelectedIndexChanged rather than DropDownList1.OnSelectedIndexChanged, but maybe that's just a typo in this post.

Secondly, because event handlers are not persisted in ViewState, you need to set up the handler on initial load and PostBack as well. The following example has programmatic wiring of the event handlers and has the exact same behavior as my previous example:
Code:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!Page.IsPostBack)
    {
      string[] movies = new string[] { "Reservoir Dogs", "Pulp Fiction", "Killing Zoe" };
      DropDownList1.DataSource = movies;
      DropDownList1.DataBind();
    }

    DropDownList1.SelectedIndexChanged += new EventHandler(DropDownList1_SelectedIndexChanged);
  }

  protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  {
    Label1.Text = string.Format("You selected {0}", DropDownList1.SelectedValue);
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
    <div>
      <asp:DropDownList ID="DropDownList1" runat="server" 
        AutoPostBack="true">
      </asp:DropDownList>
      <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
  </form>
</body>
</html>
Cheers,

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.
 
Old May 19th, 2008, 02:53 PM
Authorized User
 
Join Date: Nov 2006
Posts: 93
Thanks: 0
Thanked 1 Time in 1 Post
Default

Imar,

AS you alluded to I typed that code before rather than copy and paste it from my code. When I have registered the event handler in Page_Load I used the IDE code template and it completed the registration and generated the stub method. I typically use code in a separate file. So here is some code from my project:

From CEMDevMgmnt.ascx:

Code:
  <asp:TableCell ID="DeviceList" >
    <asp:DropDownList ID="cboDevices" runat="server" OnSelectedIndexChanged="cboDevices_Selected" AutoPostBack="true">
    </asp:DropDownList>
  </asp:TableCell>
and from CEMDevMgmnt.ascx.cs:
Code:
  protected void Page_Load(object sender, EventArgs e)
  {
    if(!Page.IsPostBack)
    {
      this.cboDevices.DataSource = this.SqlAlalyzerList;
      this.cboDevices.DataTextField = "ANALYZER_NAME";
      this.cboDevices.DataBind();
    }
  }

  protected void cboDevices_Selected(object sender, EventArgs e)
  {
    GetData(cboDevices.SelectedValue);
  }
"GetData()" takes a device name as an argument, queries the database and populates the rest of the values on the form. Except cboDevices_Selected never gets called!

What you don't know can hurt you!
 
Old May 19th, 2008, 03:08 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

 
Quote:
quote:I typically use code in a separate file.
Me too. I posted the example with in-line code as it's easier to communicate over a forum post.

Anyway, it shouldn't be like this. I have pretty similar code and it works fine:

[Default.aspx]
<form id="form1" runat="server">
<div>
  <asp:DropDownList ID="cboDevices" runat="server" AutoPostBack="True"
     onselectedindexchanged="cboDevices_SelectedIndexCh anged">
  </asp:DropDownList>
</div>
</form>

[Default.aspx.cs]
protected void Page_Load(object sender, EventArgs e)
{
  if (!Page.IsPostBack)
  {
    string[] movies = new string[] { "Reservoir Dogs", "Pulp Fiction", "Killing Zoe" };

    this.cboDevices.DataSource = movies;
    this.cboDevices.DataBind();
  }
}
protected void cboDevices_SelectedIndexChanged(object sender, EventArgs e)
{

}

cboDevices_SelectedIndexChanged gets hit every time.

I noticed you use event wiring in the control's markup, not programmatially as you said earlier. Which of the two techniques are you using?

Maybe you're not showing us the whole picture? Maybe something else is messing with your page and/or DropDownList?

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of Beginning ASP.NET 3.5 : in C# and VB, ASP.NET 2.0 Instant Results and Dreamweaver MX 2004
Want to be my colleague? Then check out this post.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Create new widgets. mallick_kuet ASP.NET 1.0 and 1.1 Basics 1 October 30th, 2008 11:47 AM
Database migration MS Access 2003 to MS SQL 2000 ayazhoda SQL Server 2000 3 April 23rd, 2007 11:38 AM
MS ACCESS 2003 FRONTEND AND MS SQL SERVER 2005 DB mohankumar0709 SQL Server 2005 3 March 23rd, 2007 12:48 AM
Web Site Widgets - toggle on/off? jlundebro BOOK: Professional Ajax ISBN: 978-0-471-77778-6 1 March 30th, 2006 08:44 PM
what is widgets paranthaman .NET Framework 2.0 1 February 25th, 2006 07:26 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.