Passing parameters from a Web page to a custom control

J

Joe Bloggs

Does anyone know if its possible to pass parameters or the values of
Request.QueryString from a web page to a custom control class? I'm using
a C# Web Application. For Example I have Web Page1 which has to
parameters passed to it from another Web page

Parameter # 1 dbid and Parameter # 2 reportid.

I know that I can access the values of the parameters from the code
behind .aspx.cs using Request.QueryString so

Request.QueryString["dbid"],Request.QueryString["reportid"]

but what I want to so is to generate dynamic HTML based upon the values
of the Request.QueryString parameters. I can create a custom control
(example below)
and hard code in some values for the parameters , call another method
and produce the HTML I want but I don't know how to pass the values of
the parameters
to the class containing the override version of the Render method. The
reason for this is I have information held in a table that depending on
the values of the parameters
will then determine the content of the HTML to display.

Can anyone comment on if this is even the right approach or am I going
in the worng direction all together.

Here is an example.

Here is my HTML code - note the reference to the custom control via the
<Custom> tag.

<%@ Register TagPrefix="Custom" Namespace="ReportUIClassLibrary"
Assembly = "ReportUIClassLibrary" %>
<%@ Page language="c#" Codebehind="DynamicTest.aspx.cs"
AutoEventWireup="false" Inherits="ReportUI.DynamicTest" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DynamicTest</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<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 MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<Custom:WebCustomControl1 Runat="Server" Text="Test" Id="WC1" />
<asp:Label id="Label1" style="Z-INDEX: 101; LEFT: 312px; POSITION:
absolute; TOP: 104px" runat="server">Label</asp:Label>
</form>
</body>
</HTML>

Here is the class containing the code for the custom control and the
override of the Render method. Note here I am just calling another
method GeneratePrompts that returns a string containing the HTML I want
displayed. This method takes two parameters , I want the parameters to
be the contents of my Request.QueryString variables, but this is
where I get stuck and I am hard coding in some values just to see if the
method call will work.

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace ReportUIClassLibrary
{
/// <summary>
/// Summary description for CustomControls.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:WebCustomControl1
runat=server></{0}:WebCustomControl1>")]
public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
{
private string text;

[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string Text
{
get
{
return text;
}
set
{
text = value;
}
}

protected override void Render(HtmlTextWriter output)
{
Text = ReportUIClassLibrary.BizObjects.GeneratePrompts("1","3");
output.Write(Text);
}
}

}
 
J

James Curran

First of all, you should not be setting Text in Render(). (It should
not change any property)

Render really wants to be something like:

protected override void Render(HtmlTextWriter output)
{
string myhtml = ReportUIClassLibrary.BizObjects.GeneratePrompts(Text,
MoreText);
output.Write(myhtml);
}


Then in your webpage's codebehind:

WC1.Text = Request.QueryString["dbid"];
WC1.MoreText = Request.QueryString["reportid"];

Actually, you'll probably want to create properties called DbId & ReportId
instead of Text & MoreText

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
Joe Bloggs said:
Does anyone know if its possible to pass parameters or the values of
Request.QueryString from a web page to a custom control class? I'm using
a C# Web Application. For Example I have Web Page1 which has to
parameters passed to it from another Web page

Parameter # 1 dbid and Parameter # 2 reportid.

I know that I can access the values of the parameters from the code
behind .aspx.cs using Request.QueryString so

Request.QueryString["dbid"],Request.QueryString["reportid"]

but what I want to so is to generate dynamic HTML based upon the values
of the Request.QueryString parameters. I can create a custom control
(example below)
and hard code in some values for the parameters , call another method
and produce the HTML I want but I don't know how to pass the values of
the parameters
to the class containing the override version of the Render method. The
reason for this is I have information held in a table that depending on
the values of the parameters
will then determine the content of the HTML to display.

Can anyone comment on if this is even the right approach or am I going
in the worng direction all together.

Here is an example.

Here is my HTML code - note the reference to the custom control via the
<Custom> tag.

<%@ Register TagPrefix="Custom" Namespace="ReportUIClassLibrary"
Assembly = "ReportUIClassLibrary" %>
<%@ Page language="c#" Codebehind="DynamicTest.aspx.cs"
AutoEventWireup="false" Inherits="ReportUI.DynamicTest" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DynamicTest</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<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 MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<Custom:WebCustomControl1 Runat="Server" Text="Test" Id="WC1" />
<asp:Label id="Label1" style="Z-INDEX: 101; LEFT: 312px; POSITION:
absolute; TOP: 104px" runat="server">Label</asp:Label>
</form>
</body>
</HTML>

Here is the class containing the code for the custom control and the
override of the Render method. Note here I am just calling another
method GeneratePrompts that returns a string containing the HTML I want
displayed. This method takes two parameters , I want the parameters to
be the contents of my Request.QueryString variables, but this is
where I get stuck and I am hard coding in some values just to see if the
method call will work.

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace ReportUIClassLibrary
{
/// <summary>
/// Summary description for CustomControls.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:WebCustomControl1
runat=server></{0}:WebCustomControl1>")]
public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
{
private string text;

[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string Text
{
get
{
return text;
}
set
{
text = value;
}
}

protected override void Render(HtmlTextWriter output)
{
Text = ReportUIClassLibrary.BizObjects.GeneratePrompts("1","3");
output.Write(Text);
}
}

}
 
J

Joe Bloggs

Thanks for the information James,
I tried what you suggested but having Render as

protected override void Render(HtmlTextWriter output)
{
string myhtml = ReportUIClassLibrary.BizObjects.GeneratePrompts(Text,
MoreText);
output.Write(myhtml);
}

caused a 'System.StackOverflowException' exception in the
DefaultDomain. It's definitely this code that is causing the exception.
 

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

Top