sending parameter to UserControl, how?

J

Jeff

Hey

ASP.NET 2.0

This the ObjectDataSource in my UserControl,
<asp:ObjectDataSource ID="odsMessage" runat="server"
SelectMethod="ExecuteMessage"
TypeName="AH.MyNetwork.BLL.Network.Message">
<SelectParameters>
<asp:profileParameter Name="user" PropertyName="UserName"
Type="String" />
<asp:parameter Name="mode" Type="Boolean" />
</SelectParameters>
</asp:ObjectDataSource>

The problem is that I don't know a good way of setting the value of this
parameter "<asp:parameter Name="mode" Type="Boolean" />" from the web page
in which the UserControl is placed..

This is how I've embedded this UserControl into the web page:
<%@ Register Src="~/Controls/Messages.ascx" TagName="Messages"
TagPrefix="mb" %>
<mb:Messages ID="Messages1" runat="server" />

Maybe I should create a private variable in the UserControl, and set the
ObjectDataSource to reference it, but I don't know how to reference the
private variable from the ObjectDataSource in the markup/source...

My UserControl is designed to show messages (received or sent message). I
have a web page named inbox.aspx where this control is used to show received
message. But now I want to use the same UserControl on a web page named
Outbox.aspx showing sent message. Then I need a way to tell the UserControl
if it's received or sent message it should display

Or maybe I should instead use 2 repeat controls + 2 ObjectDataSource objects
(1 for each repeat control) and hide or show them programatically... if the
UserControl should display received message, then hide the sent message
Repeater and display the Received Reapeater..

Any suggestions?

Jeff
 
C

Cowboy \(Gregory A. Beamer\)

Create a PUBLIC property in the code section (or code behind if you use two
files). Once you compile (build website) you will see the new property and
you can set it either in the tags or in the code behind.

If you set a default condition in the controls load, you will want to have
property set reset the state of the control.

BTW, while this is a learning hurdle, it is a good coding style, as your
user controls become black boxes that are truly reuseable. One word of
caution: If you are not the only developer, either default the control or
throw custom exceptions that point to the property not being set (if it is
mandatory, that is). Otherwise, those maintaining the code will be lost,
esp. those who have not made the leap into making actual objects of their
user controls.
 
J

Jeff

thanks, but just more thing:

This is the property I created in the UserControl:
private Boolean _mode;
public Boolean Mode
{
get { return _mode; }
set { _mode = value; }
}

This is the new markup in the web page using the UserControl (see the
property "Mode"):
<mb:Messages ID="Messages1" runat="server" Mode="true" />

Somehow the correct value of Mode isn't passed into ExecuteMessage, (it gets
"false", but I was expecting "true"). I think this is related to
"<asp:parameter Name="Mode" Type="Boolean" />" (see blow for the entire
markup of my ObjectDataSource) which can't be correct...

Here is the markup of ObjectDataSource:
<asp:ObjectDataSource ID="odsMessage" runat="server"
SelectMethod="ExecuteMessage"
TypeName="AH.MyNetwork.BLL.Network.Message">
<SelectParameters>
<asp:profileParameter Name="user" PropertyName="UserName"
Type="String" />
<asp:parameter Name="Mode" Type="Boolean" />
</SelectParameters>
</asp:ObjectDataSource>

How should I set the SelectMethod "ExecuteMessage" to use the custom
property "Mode"?

Jeff
 
J

Jeff

odsMessage.SelectParameters.Add(new Parameter("Mode", TypeCode.Boolean,
Mode.ToString()));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top