📄 cb200006ne_f.asp.htm
字号:
<p class=BodyText> It
should be pointed out that <i>TWebModule</i>
can simplify creating Web applications for processing user entries. A typical
example of such an application is a form that must be filled in by a user. </p>
<p class=BodyText> </p>
<p class=BodyText> Let's
modify our example by adding another <i>TWebActionItem</i>
object (see Figure 9). </p>
<p class=BodyText> </p>
<p class=Captions><img width=250
height=105 src="images/cb200006ne_f_image017.gif" tppabs="http://www.cbuilderzine.com/features/2000/06/cb200006ne_f/cb200006ne_f_image017.gif"> <br>
<b>Figure 9:</b>
Adding another <i>TWebActionItem</i> object
to show an HTML form. </p>
<p class=BodyText> </p>
<p class=BodyText> Let this
action be the default <i>TWebAction</i>.
Then, place a <i>TPageProducer</i> component
in our <i>WebModule1</i>. This component
will generate an HTML document from a previously prepared template (see Figure
10). </p>
<p class=BodyText> </p>
<p class=Captions><img width=179
height=121 src="images/cb200006ne_f_image018.gif" tppabs="http://www.cbuilderzine.com/features/2000/06/cb200006ne_f/cb200006ne_f_image018.gif"> <br>
<b>Figure 10:</b>
Adding <i>TPageProducer</i> to WebModule1. </p>
<p class=BodyText> </p>
<p class=BodyText> To
create this template, we must use an HTML editor that allows creating forms,
e.g. Microsoft FrontPage (see Figure 11). </p>
<p class=BodyText> </p>
<p class=Captions> <img
width=250 height=194 src="images/cb200006ne_f_image020.gif" tppabs="http://www.cbuilderzine.com/features/2000/06/cb200006ne_f/cb200006ne_f_image020.gif"> <br>
<b>Figure 11: </b>Creating
an HTML form with Microsoft FrontPage. </p>
<p class=BodyText> </p>
<p class=BodyText> The HTML
source for the document in Figure 11 looks like the following: </p>
<p class=BodyText> </p>
<p class=Code><span class=Code><html> </span></p>
<p class=Code><span class=Code><head> </span></p>
<p class=Code><span class=Code><meta
http-equiv="Content-Type" content="text/html; </span></p>
<p class=Code><span class=Code> charset=windows-1251"> </span></p>
<p class=Code><span class=Code><meta
name="GENERATOR" content="Microsoft FrontPage 2.0"> </span></p>
<p class=Code><span class=Code><title>Untitled
Normal Page</title></head> </span></p>
<p class=Code><span class=Code><body
bgcolor="#FFFFFF"> </span></p>
<p class=Code><span class=Code><p>&nbsp;</p> </span></p>
<p class=Code><span class=Code><form
method="GET"> </span></p>
<p class=Code><span class=Code><p>What
is Your name?<font size="4"> </span></p>
<p class=Code><span class=Code><input
type="text" size="20"
name="T1"></font></p> </span></p>
<p class=Code><span class=Code><p>What
is Your e-mail?<font size="4"> </span></p>
<p class=Code><span class=Code><input
type="text" size="20" name="T2"></font></p> </span></p>
<p class=Code><span class=Code><p><font
size="4"><input type="submit" name="B1" </span></p>
<p class=Code><span class=Code> value="Submit"><input
type="reset" name="B2" </span></p>
<p class=Code><span class=Code> value="Reset"></font></p> </span></p>
<p class=Code><span class=Code></form> </span></p>
<p class=Code><span class=Code></body> </span></p>
<p class=Code><span class=Code></html> </span></p>
<p class=BodyText> </p>
<p class=BodyText> This
document can be saved to a file, and in this case, we must type its name in the
<i>HTMLFile</i> property of the <i
style='mso-bidi-font-style:normal'>TPageProducer</i> component. We can also
copy the HTML source to the Clipboard, and paste it into the <i
style='mso-bidi-font-style:normal'>HTMLDoc</i> property editor. </p>
<p class=BodyText> </p>
<p class=BodyText> Now, we
must create the <i>OnAction</i> event
handler for the <i>TWebActionItem2</i>
component: </p>
<p class=BodyText> </p>
<p class=Code><span class=Code><b>void</b> <b
style='mso-bidi-font-weight:normal'>__fastcall</b> TWebModule1::WebModule1WebActionItem2Action(</span></p>
<p class=Code><span class=Code> TObject *Sender, TWebRequest *Request,
TWebResponse *Response, </span></p>
<p class=Code><span class=Code> <b> bool</b>
&Handled) </span></p>
<p class=Code><span class=Code>{</span></p>
<p class=Code><span class=Code> Response->Content=PageProducer1->Content();</span></p>
<p class=Code><span class=Code>}</span></p>
<p class=BodyText> </p>
<p class=BodyText> Compile
and save this project. Then test it (see Figure 12). </p>
<p class=BodyText> </p>
<p class=Captions> <img
width=250 height=166 src="images/cb200006ne_f_image022.gif" tppabs="http://www.cbuilderzine.com/features/2000/06/cb200006ne_f/cb200006ne_f_image022.gif"><br>
<b>Figure 12:</b>
Testing the Web application with the created form. </p>
<p class=BodyText> </p>
<p class=BodyText> Now we
must process the user input, for example, by generating an HTML page with data
from the edit fields of this form. So we must add another <i>TPageProducer</i> component (see Figure 13). </p>
<p class=BodyText> </p>
<p class=Captions> <img
width=179 height=121 src="images/cb200006ne_f_image023.gif" tppabs="http://www.cbuilderzine.com/features/2000/06/cb200006ne_f/cb200006ne_f_image023.gif"><br>
<b>Figure 13:</b>
Adding <i>PageProducer2</i> to generate HTML pages by processing the results of
the user entry. </p>
<p class=BodyText> </p>
<p class=BodyText> Insert
the following text into its <i>HTMLDoc</i>
property: </p>
<p class=BodyText> </p>
<p class=Code><span class=Code><html> </span></p>
<p class=Code><span class=Code><head> </span></p>
<p class=Code><span class=Code><title>Thank
You!</title> </span></p>
<p class=Code><span class=Code></head> </span></p>
<p class=Code><span class=Code><body > </span></p>
<p class=Code><span class=Code><p>Dear <#T1>!</p> </span></p>
<p class=Code><span class=Code><p>Thank
you for completing this form. We have included your</span></p>
<p class=Code><span class=Code>e-mail address
<#T2> in our mailing list. You will receive a</span></p>
<p class=Code><span class=Code>lot of spam
from us!</p> </span></p>
<p class=Code><span class=Code></body> </span></p>
<p class=Code><span class=Code></html> </span></p>
<p class=BodyText> </p>
<p class=BodyText> This is
a document template. It contains special tags that will be replaced by strings
from the edit fields of the previous form. In the case of our application,
these tags are <#T1> and <#T2>. They are the names of the edit
fields of the input form (you can look at the form HTML source and find them
there). </p>
<p class=BodyText> </p>
<p class=BodyText> To
replace these tags, we must create the <i>OnHTMLTag</i>
event handler of the <i>PageProducer2</i>
component: </p>
<p class=BodyText> </p>
<p class=Code><span class=Code><b>void</b> <b
style='mso-bidi-font-weight:normal'>__fastcall</b>
TWebModule1::PageProducer2HTMLTag(</span></p>
<p class=Code><span class=Code> TObject *Sender, TTag Tag, <b
style='mso-bidi-font-weight:normal'>const</b> AnsiString TagString, </span></p>
<p class=Code><span class=Code> TStrings *TagParams, AnsiString &ReplaceText) </span></p>
<p class=Code><span class=Code>{</span></p>
<p class=Code><span class=Code> ReplaceText =
Request->QueryFields->Values[TagString] +</span></p>
<p class=Code><span class=Code> Request->ContentFields->Values[TagString]; </span></p>
<p class=Code><span class=Code>}</span></p>
<p class=BodyText> </p>
<p class=BodyText> <i>Request</i>
is the <i>TWebRequest</i> object generated
by a user request. The <i>QueryFields</i>
property - a <i>TStrings</i> object -
contains the names of parameters and their values entered by a user, in the
form Name=Value. They
are stored in the QUERY_STRING environment variable of the Web server in the
form of Name1=Value1&Name2=Value2&... <i>TagString</i>
is a string contained in a tag to be replaced. The <i>Values </i>property of the <i>TStrings</i>
object is used if strings contained in this object can be represented in the
form Name=Value, as in
this case. </p>
<p class=BodyText> </p>
<p class=BodyText> It
should be mentioned that the <form> tag of the form's HTML source can use
the GET method instead of the POST method. In this case, we must use the <i
style='mso-bidi-font-style:normal'>ContentFields</i> property instead of the <i
style='mso-bidi-font-style:normal'>QueryFields</i> property. </p>
<p class=BodyText> </p>
<p class=BodyText> Now
create the next <i>TWebAction</i> with: </p>
<p class=BodyText> </p>
<p class=Code><span class=Code>PathInfo="/t3" </span></p>
<p class=BodyText> </p>
<p class=BodyText> to
represent a page generated by <i>PageProducer2</i>,
and add its <i>OnAction</i> event handler: </p>
<p class=BodyText> </p>
<p class=Code><span class=Code><b>void</b> <b
style='mso-bidi-font-weight:normal'>__fastcall</b>
TWebModule1::WebModule1WebActionItem3Action(</span></p>
<p class=Code><span class=Code> TObject *Sender, TWebRequest *Request,
TWebResponse *Response, </span></p>
<p class=Code><span class=Code> <b> bool</b>
&Handled) </span></p>
<p class=Code><span class=Code>{</span></p>
<p class=Code><span class=Code> Response->Content=PageProducer2->Content();</span></p>
<p class=Code><span class=Code>}</span></p>
<p class=BodyText> </p>
<p class=BodyText> How can
this page generation be initiated after the user presses the Submit button? We must take care of the processing of this event, which
should be done by editing the <i>HTMLDoc</i>
property of the <i>PageProducer1</i>
component: </p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -