Building a Web Application->Working with Server Controls

ASP.NET server controls //服务器端控件are identified //识别within a page using declarative tags//声明的标签 that contain a runat="server" attribute. The following example declares//声明,陈述,展示 three <asp:label runat="server"> server controls //服务器端控件and customizes //用户化the text and style properties //样式属性of each one individually. //个别地

C# Controls1.aspx
<%@ Page Language="C#" %>

<html>

    
<body>

       
<h3><font face="Verdana">Declaring Server Controls</font></h3>

       This sample demonstrates how to declare the 
&lt;asp:label&gt; server control and
       manipulate its properties within a page.

       
<p>

       
<hr>

       
<asp:label id="Message1" font-size="16" font-bold="true" forecolor="red" runat=server>This is Message One</asp:label>

       
<br>

       
<asp:label id="Message2" font-size="20" font-italic="true" forecolor="blue" runat=server>This is Message Two</asp:label>

       
<br>

       
<asp:label id="Message3" font-size="24" font-underline="true" forecolor="green" runat=server>This is Message Three</asp:label>

    
</body>

</html>

Manipulating //处理Server Controls//服务器端控件

You can programmatically //计划性地identify //识别an individual ASP.NET server control //服务器端控件within a page by providing it with an id attribute. You can use this id reference //id参数 to programmatically //计划性地 manipulate the server control's object model //对象模型at run time//运行时. For example, the following sample demonstrates //举例说明how a page developer could programmatically//计划性地 set an <asp:label runat="server"> control's Text property within the Page_Load event//页面载入事件.

C# Controls2.aspx
<html>

    
<script language="C#" runat="server">

        
void Page_Load(Object Src, EventArgs E) {
            Message.Text 
= "You last accessed this page at: " + DateTime.Now;
        }


    
</script>

    
<body>

       
<h3><font face="Verdana">Manipulating Server Controls</font></h3>

       This sample demonstrates how to manipulate the 
&lt;asp:label&gt; server control within 
       the Page_Load 
event to output the current time.

       
<p>

       
<hr>

       
<asp:label id="Message" font-size="24" font-bold="true" runat=server/>

    
</body>

</html>

Handling Control Events//处理控件事件

ASP.NET server controls //服务器端控件can optionally//可选择地 expose //揭露,展示,阐述and raise//上升,增高,升起,饲养 server events//服务器事件, which can be handled by page developers. A page developer may accomplish//完成,实现 this by declaratively //宣言地 wiring an event to a control (where the attribute name //属性名称of an event wireup //indicates the event name and the attribute value//属性值 indicates the name of a method//方法名 to call//调用). For example, the following code example demonstrates//举例说明 how to wire an OnClick event //单击事件to a button control.

C# Controls3.aspx
 
<html>

    
<script language="C#" runat="server">

        
void EnterBtn_Click(Object Src, EventArgs E) {
            Message.Text 
= "Hi " + Name.Text + ", welcome to ASP.NET!";
        }


    
</script>

    
<body>

       
<h3><font face="Verdana">Handling Control Action Events</font></h3>

       
<p>

       This sample demonstrates how to access a 
&lt;asp:textbox&gt; server control within the "Click" 
       
event of a &lt;asp:button&gt;, and use its content to modify the text of a &lt;asp:label&gt;.

       
<p>

       
<hr>

       
<form action="controls3.aspx" runat=server>

          
<font face="Verdana"> 

             Please enter your name: 
<asp:textbox id="Name" runat=server/> 
                                     
<asp:button text="Enter" Onclick="EnterBtn_Click" runat=server/>

             
<p>

             
<asp:label id="Message"  runat=server/>

          
</font>

       
</form>

    
</body>

</html>

Handling Multiple Control Events//处理多个控件事件

Event handlers//事件处理器 provide a clean way for page developers to structure logic//结构逻辑 within an ASP.NET page. For example, the following sample demonstrates how to wire //电报,接收and handle//处理 four button events on a single page//单独的页面中.

C# Controls4.aspx
<html>

    
<script language="C#" runat="server">

        
void AddBtn_Click(Object Src, EventArgs E) {

            
if (AvailableFonts.SelectedIndex != -1{

               InstalledFonts.Items.Add(
new ListItem(AvailableFonts.SelectedItem.Value));
               AvailableFonts.Items.Remove(AvailableFonts.SelectedItem.Value);
            }

        }


        
void AddAllBtn_Click(Object Src, EventArgs E) {

            
while (AvailableFonts.Items.Count != 0{

               InstalledFonts.Items.Add(
new ListItem(AvailableFonts.Items[0].Value));
               AvailableFonts.Items.Remove(AvailableFonts.Items[
0].Value);
            }

        }


        
void RemoveBtn_Click(Object Src, EventArgs E) {

            
if (InstalledFonts.SelectedIndex != -1{

               AvailableFonts.Items.Add(
new ListItem(InstalledFonts.SelectedItem.Value));
               InstalledFonts.Items.Remove(InstalledFonts.SelectedItem.Value);
            }

        }


        
void RemoveAllBtn_Click(Object Src, EventArgs E) {

            
while (InstalledFonts.Items.Count != 0{

               AvailableFonts.Items.Add(
new ListItem(InstalledFonts.Items[0].Value));
               InstalledFonts.Items.Remove(InstalledFonts.Items[
0].Value);
            }

        }


    
</script>

    
<body>

       
<h3><font face="Verdana">Handling Multiple Control Action Events</font></h3>

       
<p>

       This sample demonstrates how to handle multiple control action events raised from 
       different 
&lt;asp:button&gt; controls.

       
<p>

       
<hr>

       
<form action="controls4.aspx" runat=server>

          
<table>
             
<tr>
               
<td>
                 Available Fonts 
               
</td>
               
<td>
                 
<!-- Filler -->
               
</td>
               
<td>
                 Installed Fonts 
               
</td>
             
</tr> 
             
<tr>
               
<td>
                 
<asp:listbox id="AvailableFonts" width="100px" runat=server>
                    
<asp:listitem>Roman</asp:listitem>
                    
<asp:listitem>Arial Black</asp:listitem>
                    
<asp:listitem>Garamond</asp:listitem>
                    
<asp:listitem>Somona</asp:listitem>
                    
<asp:listitem>Symbol</asp:listitem>
                 
</asp:listbox>
               
</td>
               
<td>
                 
<!-- Filler -->
               
</td>
               
<td>
                 
<asp:listbox id="InstalledFonts" width="100px" runat=server>
                    
<asp:listitem>Times</asp:listitem>
                    
<asp:listitem>Helvetica</asp:listitem>
                    
<asp:listitem>Arial</asp:listitem>
                 
</asp:listbox>
               
</td>
             
</tr> 
             
<tr>
               
<td>
                 
<!-- Filler -->
               
</td>
               
<td>
                 
<asp:button text="<<" OnClick="RemoveAllBtn_Click" runat=server/>
                 
<asp:button text="<" OnClick="RemoveBtn_Click" runat=server/> 
                 
<asp:button text=">" OnClick="AddBtn_Click" runat=server/>
                 
<asp:button text=">>" OnClick="AddAllBtn_Click" runat=server/> 
               
</td>
               
<td>
                 
<!-- Filler -->
               
</td>
             
</tr>
          
</table>

       
</form>

    
</body>

</html>

Performing Page Navigation//实现页面导航 (Scenario//情节,剧情 1)

Page navigation among multiple pages is a common scenario in virtually//几乎,差不多 all Web applications. The following sample demonstrates how to use the <asp:hyperlink runat=server> control to navigate to//导航到 another page (passing custom query string parameters //查询字符串参数along //向着,沿the way). The sample then demonstrates how to easily get access to //获得these query string parameters //查询字符串参数from the target page//目标页面.

C# Controls5.aspx
<html>

    
<script language="C#" runat="server">

        
void Page_Load(Object Src, EventArgs E) {

           Random randomGenerator 
= new Random(DateTime.Now.Millisecond);

           
int randomNum = randomGenerator.Next(03);

           
switch(randomNum) {

              
case 0:
                Name.Text 
= "Scott";
                
break;

              
case 1:
                Name.Text 
= "Fred";
                
break;

              
case 2:
                Name.Text 
= "Adam";
                
break;
           }


           AnchorLink.NavigateUrl 
= "controls_navigationtarget_cs.aspx?name=" + System.Web.HttpUtility.UrlEncode(Name.Text);
        }


    
</script>

    
<body>

       
<h3><font face="Verdana">Performing Page Navigation (Scenario 1)</font></h3>

       
<p>

       This sample demonstrates how to generate a HTML Anchor tag that will cause the client to
       navigate to a 
new page when he/she clicks it within the browser.

       
<p>

       
<hr>

       
<p>

       
<asp:hyperlink id="AnchorLink" font-size=24 runat=server>
          Hi 
<asp:label id="Name" runat=server/> please click this link!
       
</asp:hyperlink>

    
</body>

</html>

Performing Page Navigation //实现页面导航(Scenario //情景,剧情2)

Not all page navigation scenarios are initiated //初始化,开始through hyperlinks on the client//客户端. Client-side page//客户端页面 redirects or navigations can also be initiated //初始化,开始from the server//服务器端 by an ASP.NET page developer by calling the Response.Redirect(url) method. This is typically //典型地done when server-side//服务器端 validation //确认is required on some client input//客户端输入 before the navigation actually //实际上takes place//发生.

The following sample demonstrates how to use the Response.Redirect method //方法to pass parameters //传递参数to another target page//目标页面. It also demonstrates how to easily get access to //获得,可以使用these parameters//参数 from the target page.

C# Controls6.aspx
<html>

    
<script language="C#" runat="server">

        
void EnterBtn_Click(Object Src, EventArgs E) {

            
// Navigate to a new page (passing name as a querystring argument) if
            
// user has entered a valid name value in the <asp:textbox>

            
if (Name.Text != ""{
               Response.Redirect(
"Controls_NavigationTarget_cs.aspx?name=" + System.Web.HttpUtility.UrlEncode(Name.Text));
            }

            
else {
               Message.Text 
= "Hey! Please enter your name in the textbox!";
            }

        }


    
</script>

    
<body>

       
<h3><font face="Verdana">Performing Page Navigation (Scenario 2)</font></h3>

       
<p>

       This sample demonstrates how to navigate to a 
new page from within a &lt;asp:button&gt; click event,
       passing a 
&lt;asp:textbox&gt; value as a querystring argument (validating first that the a legal
       textbox value has been specified).

       
<p>

       
<hr>

       
<form action="controls6.aspx" runat=server>

          
<font face="Verdana">

             Please enter your name: 
<asp:textbox id="Name" runat=server/>
                                     
<asp:button text="Enter" Onclick="EnterBtn_Click" runat=server/>

             
<p>

             
<asp:label id="Message" forecolor="red" font-bold="true" runat=server/>

          
</font>

       
</form>

    
</body>

</html>

posted on 2007-04-25 15:11  改变热爱  阅读(269)  评论(0)    收藏  举报

导航