473,326 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

ASP 2.0 Webpart Connections

I am having some trouble with my webpart connections on postbacks.

I basically have two webparts in the same zone, 1 provider and 1 consumer.
The provider has a dropdown list and submit button and the consumer has a
text box that displays data depending on what is selected in the provider.
On the initial page hit, The consumer connection is established before the
CreateChildControls event (which is fine), but on the postback, the
CreateChildControls event occurs before the consumer connection is
established, so the the CreateChildControls method does not know which value
was selected in the provider at this time.

Does anyone else have this problem or know the correct way to do this?

I've noticed that the CreateChildControls only gets fired before the
connection gets established when there are any input (text, checkbox, etc)
controls on the form. If you run the code below, you'll notice the consumer
is only rendered every other postback because when the connection isn't set,
the consumer can't render the control because it doesn't know which type to
display. And when the consumer has no input controls, on the next postback,
the connection is established first and the controls get rendered.

I've come up with a workaround, but I'd like a cleaner solution. My
workaround is to just call CreateChildControls again in the Consumer
connection code (You'll see it commented out in the code below). This will
ensure that the controls are created again after the connection has been
made. I don't like this because 1) On the initial page hit the controls we
be rendered and then deleted and re-rendered when the connection is made
which is inefficient.

I guess some other questions would be...

1. I know the CreateChildControls event is called at different times
depending on page circumstances, but does anyone know those circumstances?

2. Is there a way to make sure that the connection happens before the
CreateChildControls? Or a way to force the connection at some point in the
page life cycle?
Any help with this would be greatly appreciated... I've been banging my head
against this one for a couple days now.

Thanks!!!

Code follows...

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" EnableViewState="false"%>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS.Controls" %>

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:WebPartManager ID="mgr" runat="server">
<StaticConnections>
<asp:WebPartConnection ID="conn1"
ConsumerConnectionPointID="MyConsumer"
ConsumerID="consumer1"
ProviderConnectionPointID="MyProvider"
ProviderID="provider1" />
</StaticConnections>
</asp:WebPartManager>

<asp:WebPartZone ID="WebPartZone1" runat="server">
<ZoneTemplate>
<aspSample:ZipCodeWebPart ID="provider1" runat="server" Title="My
Provider" />
<aspSample:WeatherWebPart ID="consumer1" runat="server" Title="My
Consumer" />
</ZoneTemplate>
</asp:WebPartZone>

<asp:ConnectionsZone ID="ConnectionsZone1" runat="server">
</asp:ConnectionsZone>
</form>
</body>
</html>


namespace Samples.AspNet.CS.Controls
{
using System;
using System.Web;
using System.Web.Security;
using System.Security.Permissions;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public interface IMyConnection
{
string MySelection { get; }
}

public class ZipCodeWebPart : WebPart, IMyConnection
{
private string _mySelection = String.Empty;
private DropDownList _dll;

public ZipCodeWebPart()
{
}

public virtual string MySelection
{
get { return _mySelection; }
}

[ConnectionProvider("Zip Code Provider", "MyProvider")]
public IMyConnection ProvideMyProvider()
{
return this;
}

protected override void CreateChildControls()
{
Controls.Clear();

_dll = new DropDownList();

_dll.Items.Add("One");
_dll.Items.Add("Two");
_dll.Items.Add("Three");
_dll.CausesValidation = true;
this.Controls.Add(_dll);

Button myButton = new Button();
myButton.Text = "Display";
myButton.Click +=new EventHandler(myButton_Click);
this.Controls.Add(myButton);
}

void myButton_Click(object sender, EventArgs e)
{
_mySelection = _dll.SelectedValue;
}

}

public class WeatherWebPart : WebPart
{
private IMyConnection _provider;

[ConnectionConsumer("Zip Code Consumer", "MyConsumer")]
public void GetMyProvider(IMyConnection Provider)
{
_provider = Provider;
}

protected override void OnPreRender(EventArgs e)
{
EnsureChildControls();

}

protected override void CreateChildControls()
{

if (this._provider != null)
{
Controls.Clear();

TextBox tb = new TextBox();
tb.ID = "myTextBox";
this.Controls.Add(tb);

switch (_provider.MySelection)
{
case "One":
tb.Text = "Display One Data";
break;
case "Two":
tb.Text = "Display Two Data";
break;
case "Three":
tb.Text = "Display Three Data";
break;
}
}
}
}
}
Nov 20 '05 #1
0 1479

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: J. Jespersen | last post by:
Hi people, I fiddling with webparts, and have created my own custom webpart from scratch. The webpart itself is contained in a myPart.dll I have two different sites. SiteA and SiteB. Both are...
3
by: DexHex | last post by:
Hi, I am stumpt. Is there anyway to set the background image on a webpart title?
0
by: tony dong | last post by:
Hi there I use vs.net 2005 with standard sql 2005 under machine\sql2005 for instant when I create webpart, the code get from quickstart as follow: <%@ Page Language="C#" %> <%@ Register...
0
by: arthernan | last post by:
If I separate the code into two buttons on a web page it works. One button creates the webpart and the second creates the connection. But the minute I move the connection creation code to the web...
3
by: Jav | last post by:
I create a UserControl (.ascx), and then change it in code to: Inherits System.Web.UI.WebControls.WebParts.WebPart expecting that it will now be treated as a WebPart. But this immediately...
2
by: MrCrool | last post by:
Hi I am trying to develop WebParts that will interact with each other to make up a highly customizable "system", but I am having trouble with the order in which the webparts connect and...
0
by: rote | last post by:
I'm trying to use jan's wonderful SmartPart from http://weblogs.asp.net/jan/archive/2007/02.aspx After trying the Connections Demo from the site and making a connectable webpart When using the...
1
by: archu007 | last post by:
Hi frds i hav used webparts in my application and i'm using sqlserver2000 when i run the application the following error is displayed "An error has occurred while establishing a connection to...
4
by: Arnab das | last post by:
I am working in asp.net webpart.I want to show the delete warning message in a modal popup. How can I achieve this?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.