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 19th, 2008, 04:08 PM
Authorized User
 
Join Date: Nov 2006
Posts: 93
Thanks: 0
Thanked 1 Time in 1 Post
Default

Actually, I will use any technique I can get to work. Here's a piece that may relavent. The project itself uses one main web page divided up with tables. There's an area for the header, footer and a menu area. Then there is a cell for the Data. Each group of data that I need to present is contained in a user control. Depending on the menu selection I load the web page with the appropriate control. This particular control is the Device Manager which is responsible for determining which devices will be monitored. I call the user control with:

Code:
else if (this.Request.QueryString["datapage"] == "devadmin")
{
  try
  {
    DevMgr.Roles = Roles;
    this.PageData.Controls.Clear();
    this.PageData.Controls.Add(DevMgr);
    DevMgr.Visible = true;
  }
  catch (Exception ex)
  {
    WriteLog(ex.Message);
  }
}
Every user control has to exist and be registered in the main web page. That's why you see the line "DevMgr.Visible = true;"

Where this is particularly noticible is when a Post occurs it goes to the default.aspx not to CEMDevMgmnt.aspx where the actual dropdownlist is located and where the code for it is contained.

Do you think that makes a difference. Because some of the code works as expected. Page_Load in the user control, for instance, is always called when a Post occurs. And I have 4 buttons in the last row of the table that all work now that I have put the OnClick registration in the declaration portion.



What you don't know can hurt you!
 
Old May 19th, 2008, 05:47 PM
Authorized User
 
Join Date: Nov 2006
Posts: 93
Thanks: 0
Thanked 1 Time in 1 Post
Default

One more shot at it. I created a new user control. This one is for managing user access to the system. It still needs a lot of work, but to keep it short I just basically created it with the wizards. Added 3 SQLDataSources for 2 list boxes and a dropdown listbox. One listbox shows the available access roles, the other shows the roles assigned to the user which is in the dropdownlist. When you select a different user in the dropdownlist, that users access should appear... it doesn't. here's the code:

.aspx code:
Code:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CEMUserMgmnt.ascx.cs" Inherits="pages_CEMUserMgmnt" %>
<asp:SqlDataSource ID="CEMSqlGroups" runat="server" ConnectionString="<%$ ConnectionStrings:TOL_CEM_Connect %>"
  SelectCommand="SELECT [Group_Name] FROM [Access_Groups]"></asp:SqlDataSource>
<asp:SqlDataSource ID="CEMSqlUserGroups" runat="server" ConnectionString="<%$ ConnectionStrings:TOL_CEM_Connect %>"
  SelectCommand="cemGetUserRoles" SelectCommandType="StoredProcedure">
  <SelectParameters>
    <asp:ControlParameter ControlID="cboUsers" Name="username" PropertyName="SelectedValue"
      Type="String" />
  </SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="CEMSqlUsers" runat="server" ConnectionString="<%$ ConnectionStrings:TOL_CEM_Connect %>"
  SelectCommand="SELECT [User_Name] FROM [Users]"></asp:SqlDataSource>
<table style="width: 641px; height: 122px">
  <tr>
    <td>
      Users</td>
    <td style="width: 154px">
      User Groups</td>
    <td style="width: 38px">
    </td>
    <td>
      Available Groups</td>
  </tr>
  <tr>
    <td>
      <asp:DropDownList ID="cboUsers" runat="server" AutoPostBack="True" 
        DataSourceID="CEMSqlUsers" DataTextField="User_Name" DataValueField="User_Name" OnSelectedIndexChanged="cboUsers_SelectedIndexChanged">
      </asp:DropDownList></td>
    <td style="width: 154px">
      <asp:ListBox ID="ListBox1" runat="server" DataSourceID="CEMSqlUserGroups" DataTextField="Group_Name"
        DataValueField="Group_Name"></asp:ListBox></td>
    <td style="width: 38px">
      <table style="width: 35px">
        <tr>
          <td>
            <asp:Button ID="btnAddAll" runat="server" Text="<<" /></td>
        </tr>
        <tr>
          <td>
            <asp:Button ID="btnAddOne" runat="server" Text=" < " /></td>
        </tr>
        <tr>
          <td>
            <asp:Button ID="btnRemOne" runat="server" Text=" > " /></td>
        </tr>
        <tr>
          <td style="height: 20px">
            <asp:Button ID="btnRemAll" runat="server" Text=">>" /></td>
        </tr>
      </table>
    </td>
    <td>
      <asp:ListBox ID="lstAccessGroups" runat="server" DataSourceID="CEMSqlGroups" DataTextField="Group_Name"
        DataValueField="Group_Name"></asp:ListBox></td>
  </tr>
  <tr>
    <td>
      <asp:Button ID="btnCancel" runat="server" Text="Cancel" />
    </td>
    <td style="width: 154px">
    </td>
    <td style="width: 38px">
    </td>
    <td>
      <asp:Button ID="btnSave" runat="server" Text="Save" /></td>
  </tr>
</table>
and Here is the .aspx.cs code:
Code:
using System;
using System.Data;
using System.IO;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class pages_CEMUserMgmnt : System.Web.UI.UserControl
{
  protected void Page_Load(object sender, EventArgs e)
    {
    }

  protected void WriteLog(string Msg)
  {
    try
    {
      StreamWriter err_log = File.AppendText(this.Request.Params["APPL_PHYSICAL_PATH"] +
        "\\logs\\error.log"); if (err_log != null)
      {
        err_log.WriteLine("{0}::{1}::{2}", DateTime.Now, this.ClientID, Msg);
        err_log.Close();
      }
    }
    catch (Exception)
    {
    }
  }
  protected void cboUsers_SelectedIndexChanged(object sender, EventArgs e)
  {
    WriteLog(String.Format("Selected user {0}.", cboUsers.SelectedValue));
  }
}
I really wish I understood why this does not work. Because if it did work, it would simplify coding, eliminating some of the more mundane aspects of it so you could focus on getting the business logic right. But over the years I have spent many hours trying to get Microsoft widgets to work with marginal success at best. Thus, the point of my rant.

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

Again, it seems you're not really showing us the while picture here.

I took your example user control, created a Users table with a User_Name column, removed the other two SqlDataSource controls.
I then added the User Control to a page, hit F5, chose a new item from the list and the breakpoint in cboUsers_SelectedIndexChanged was hit.

So, the controls work, the principle are OK, but not in your situation. This leads me to believe that things are handled differently in your situation. Maybe you are incorrectly, or too late, loading your dynamic controls. Maybe something else is going wrong.

However, what it is exactly is hard to say as a) you're not showing and explaining us the whole picture, and b) you seem to post a different code snippet from a different situation every time you post here. Remember, all we can see is the stuff you post here.....

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 20th, 2008, 03:17 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 432
Thanks: 0
Thanked 1 Time in 1 Post
Default

Are we not coming at this from the wrong angle? Reading this seems like reading one of my posts, where I dont fully understand the ASP.NET framework and application development model.. Would it not be best to work up an understand of the fundamentals of ASP.NET applications THEN try and build user controls and all the "widgets" you want to cut the fat out of your code? Thats what I am doing and its paying dividends..

This is not a flame, but I cant help but feeling there may be an element of "run before you can walk" here?

I mean, we all need to rant now and then, but there are thousands of ASP.NET developers out there, actively using MS Web Controls (they are not called Widgets, see Sam's post). Like I said, I too am new to ASP.NET development, so when it comes to User Controls, guess what I do? Nothing. I wont do them until I have a firm grasp on how pages are constructed, controls are rendered, the PostBack system etc. These are fundamentals and must be mastered first rather than "having a bash" and then blaming the underlying technology/vendor.

Its like trying to do Windows programming without understanding Events..

Just my thoughts, not a flame, just think maybe you should work up rather than down.

Rob
http://robzyc.spaces.live.com
 
Old May 21st, 2008, 10:50 AM
Authorized User
 
Join Date: Nov 2006
Posts: 93
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Rob,

Thanks for your comments, I don't see them as a flame as you have a legitimate issue. Unfortunately I, like many others, don't often have the time to fully explore all aspects of a development model before I am required to jump in and do something productive with it.

I did discover my problem (I think). Because my user controls were activated with with a PageData.Add method, I believe each time I called the default.aspx web page, the user control was regenerated before the postback methods could be called. An alternative could have been to add each user control to the PageData object at design and set their Visibility attribute to false.
Code:
<asp:TableCell ID="PageData" runat="server" HorizontalAlign="center">
  <uc2:CEMData ID="CEMData1" runat="server" Visible="true" />
  <uc3:CEMDevices ID="CEMDevices1" runat="server" Visible="false" />
  <uc4:CEMUsers ID="CEMUsers1" runat="server" Visible="false" />
</asp:TableCell>
Then make them visible as I needed them in a non-postback Load_Page.

But as most of the user controls required data this would add a lot of overhead as each user control is instantiated so I opted to use a separate web page for each task. I need to get a handle on Master Pages as this might do what I want without the overhead.

Now I have to figureout how to get SqlDataSource to Insert, Update and Delete. But I have some more reading to do before I start asking questins. I could rant on about tech books but I'll save that for another time...

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

Two things to be aware of regarding creating control manually are :

* you should probably do the creation of controls in the Page_Init event
* if you don't give the control the same ID each time event handlers wont be called correctly, although the postback will get triggered (i.e. the page will reload but your event wont get called).

I hope that helps.

/- Sam Judson : Wrox Technical Editor -/
 
Old May 22nd, 2008, 03:15 PM
Authorized User
 
Join Date: Nov 2006
Posts: 93
Thanks: 0
Thanked 1 Time in 1 Post
Default

Sam, I'm not sure what you mean by "creating controls manually".

If you change the name of a control you have to check the event handler statement (MyControl.OnSomeEvent += new EventHandler(EventHandler_Name);) to be sure "EventHandler_Name" is still correct. But I usually let the IDE do it, taking advantages of some of the macros.


What you don't know can hurt you!
 
Old May 22nd, 2008, 04:08 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

By manually I meant in code, i.e.

MyControl newControl = new MyControl();
newControl.ID = "myControl1";
Page.Controls.Add(newControl);

Without the line in bold events in the control will not be triggered.

I have written the following sample, which works fine for me here:

Control Code: WebControl1.ascx
Code:
<asp:ObjectDataSource runat="server" ID="objectSource1" SelectMethod="GetList" 
    TypeName="WebApplication2.ObjectData">
</asp:ObjectDataSource>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
    DataSourceID="objectSource1" DataTextField="Name" DataValueField="Index" 
    onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
Control Code: WebControl1.ascx.cs
Code:
    public partial class WebUserControl1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Debug.Fail("break here");
        }
    }
Page Code: Default.aspx
Code:
    <form id="form1" runat="server">
    <div>

        <uc1:WebUserControl1 ID="WebUserControl11" runat="server" />

    </div>
    </form>
Obviously your code will be more complicated than this - but perhaps you can use this simple example as a guide to see what you are doing differently. Once we know what you are doing differently we can try to work out why that is not working.


/- Sam Judson : Wrox Technical Editor -/





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.