首页 > 编程 > ASP > 正文

asp+中文教程(四)-- 服务器端控制(二)

2024-05-04 11:06:26
字体:
来源:转载
供稿:网友
8、    dropdownlist : 单选下拉框,你有两种方法可以增加下拉框中的选择项,一种和传统的select一样,把option项列出来,象下面这样:
     
        <asp:dropdownlist id=dropdown1 runat="server">
            <asp:listitem>item 1</asp:listitem>
            <asp:listitem>item 2</asp:listitem>
            <asp:listitem>item 3</asp:listitem>
            <asp:listitem>item 4</asp:listitem>
            <asp:listitem>item 5</asp:listitem>
            <asp:listitem>item 6</asp:listitem>
        </asp:dropdownlist>

另一种方法是和数据绑定,如下:

               arraylist values = new arraylist();

               values.add ("in");
               values.add ("ks");
               values.add ("md");
               values.add ("mi");
               values.add ("or");
               values.add ("tn");

               dropdown1.datasource = values;
               dropdown1.databind();

这两种法法你可以任意选择,我个人觉得后一种方法比较实用,尤其是当遇到用数据库中得到的数据来做下拉框选项时。

9、    hyperlink : 超连接,但和html的<a href>不同的是hyperlink作为一个服务器端控制,具有text 和 navigateurl两个属性,并且可以用数据绑定的方法来定义,下面我列出两种定义方法:
方法一:

        <asp:hyperlink id=hyperlink1 navigateurl= “/quickstart” runat="server">
            go to quickstart
        </asp:hyperlink>



        方法二:

       <%@ import namespace="system.data" %>

<html>

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

    void page_load(object src, eventargs e) {

        datatable dt = new datatable();
        datarow dr;

        dt.columns.add(new datacolumn("integervalue", typeof(int32)));
        dt.columns.add(new datacolumn("stringvalue", typeof(string)));
        dt.columns.add(new datacolumn("datetimevalue", typeof(datetime)));
        dt.columns.add(new datacolumn("boolvalue", typeof(bool)));
        dt.columns.add(new datacolumn("currencyvalue", typeof(double)));

        for (int i = 0; i < 9; i++) {

            dr = dt.newrow();

            dr[0] = i;
            dr[1] = "item " + int32.tostring(i);
            dr[2] = datetime.now;
            dr[3] = (i % 2 != 0) ? true : false;
            dr[4] = 1.23 * (i+1);

            dt.rows.add(dr);
        }

        myrepeater.datasource=new dataview(dt);
        myrepeater.databind();
    }

</script>

<body>

    <h3><font face="verdana">databinding htmlanchor</font></h3>

    <p>

    <form runat=server>

        <asp:repeater id="myrepeater" runat="server">

            <template name="itemtemplate">

                link for

                <asp:hyperlink id=hyperlink1 text='<%# databinder.eval(container.dataitem, "stringvalue") %>' navigateurl='<%# databinder.eval(container.dataitem,"stringvalue","detailspage.aspx?id={0}") %>' runat="server" />

                <p>

            </template>

        </asp:repeater>

    </form>

</body>
</html>


10、image :图片,只有一个属性imageurl, 现在你可以实时改变图片了,例子我就不给了。

11、    magebutton:图片按钮,比较好理解,但你可千万别小瞧它呀,比起dhtml中的那个图片按钮来,它的功能强大多了。首先,它可以作为一个按钮来使用(废话),比起这个基本功能,下边要谈到的这个功能就会让你咋舌的,它可以判断你点击的相对坐标,什么,没什么用?我。。我扁你!看看下面这段程序:
    
imagebutton2.aspx

<html>
<head>

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

        void imagebutton1_onclick(object source, imageclickeventargs e) {
        
            int x=e.x;
            int y=e.y;

            label1.text = "x: " + x.tostring();
            label2.text = "y: " + y.tostring();
            button1.imageurl = "/quickstart/aspplus/images/billg.gif";

            // check to see if an eye, ear, nose or mouth was clicked.
            
            if (x>20 && x<35 && y>72 && y<88) {
                label3.text = "you clicked bill's ear.";
            }
            else if (x>42 && x<58 && y>48 && y<64) {
                label3.text = "you clicked bill's eye.";
            }
            else if (x>62 && x<90) {
            
                if (y>46 && y<56) {
                    label3.text = "you clicked bill's eye.";
                }
                else if (y<72 && y>60) {
                    label3.text = "you clicked bill's nose.";
                }
                else if (y<92 && y>74) {
                    button1.imageurl = "/quickstart/aspplus/images/billg_tongue.gif";
                    label3.text = "phfttttt-t-t-t-t-t!.";
                }
                else {
                    whoops();
                }
            }
            else if (x>100 && x<108 && y>56 && y<72) {
                label3.text = "you clicked bill's ear.";
            }
            else {
                whoops();
            }
        }

        void whoops() {
            label3.text = "you missed! try clicking bill's eyes, ears, nose or mouth.";
        }

    </script>

</head>
<body>

    <h3><font face="verdana">using imagebutton as an image map</font></h3>

    <form runat=server>

        <table width="100%" border=0>
          <tr>
            <td width="25%">
                <asp:imagebutton id=button1 imageurl="/quickstart/aspplus/images/billg.gif" borderwidth="2px" onclick="imagebutton1_onclick" runat="server" />
            </td>
            <td>
                <asp:label id=label1 text="x:" runat="server"/>
                
                <br>
                
                <asp:label id=label2 text="y:" runat="server"/>
            </td>
          </tr>
        </table>
        
        <asp:label id=label3 font-bold="true" text="" runat="server"/>
        
    </form>

</body>
</html>

运行一下,你看到了什么?是比尔.盖茨傻兮兮的一张笑脸,你如果点中他的眼睛和鼻子,就会告诉你你点中了哪儿,如果你点中他的嘴,他会伸一下舌头,当然,这些只是根据你鼠标点中的相对位置来判断相应的操作而已,但你别忘了这只是简单的一个imagebutton就能够做到的功能呀。


12、    label , 前面的例子已经讲过了,是一个服务器端文本控制,它只有一个属性:text , 你可以动态改变它。

13、    linkbutton :有一种服务器端按钮控制,它的外形象hyperlink , 属性也相同,但不同的是它可以提交表单,同其他两种button控制一样。

14、    listbox , 列表框,它和下拉框一样,也有两种方式定义选项,一种是直接列表,另一种是数据绑定,我就不再赘述了。

15、    panel : 面板服务器端控制,这可是个好东西,它可以作为其他服务器端控制的集合,它最实用的地方是可以生成一组服务器端控制或者显示/隐藏一组服务器端控制,看看下面这个例子吧,是不是很奇妙?

panel1.aspx

<html>
<head>

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

        void page_load(object sender, eventargs e) {
        
            // show/hide panel contents
        
            if (check1.checked) {
                panel1.visible=false;
            }
            else {
                panel1.visible=true;
            }

            // generate label controls
            
            int numlabels = int.fromstring(dropdown1.selecteditem.value);
            
            for (int i=1; i<=numlabels; i++) {
                label l = new label();
                l.text = "label" + int32.tostring(i);
                l.id = "label" + int32.tostring(i);
                panel1.controls.add(l);
                panel1.controls.add(new literalcontrol("<br>"));
            }

            // generate textbox controls
            
            int numtexts = int.fromstring(dropdown2.selecteditem.value);
            
            for (int i=1; i<=numtexts; i++) {
                textbox t = new textbox();
                t.text = "textbox" + int32.tostring(i);
                t.id = "textbox" + int32.tostring(i);
                panel1.controls.add(t);
                panel1.controls.add(new literalcontrol("<br>"));
            }
        }

    </script>

</head>
<body>

    <h3><font face="verdana">panel example</font></h3>

    <form runat=server>

        <asp:panel id="panel1" runat="server"
            backcolor="gainsboro"
            height="200px"
            width="300px">

            panel1: here is some static content...
            <p>

        </asp:panel>

        <p>
        
        generate labels:
        <asp:dropdownlist id=dropdown1 runat="server">
            <asp:listitem value="0">0</asp:listitem>
            <asp:listitem value="1">1</asp:listitem>
            <asp:listitem value="2">2</asp:listitem>
            <asp:listitem value="3">3</asp:listitem>
            <asp:listitem value="4">4</asp:listitem>
        </asp:dropdownlist>

        <br>
        
        generate textboxes:
        <asp:dropdownlist id=dropdown2 runat="server">
            <asp:listitem value="0">0</asp:listitem>
            <asp:listitem value="1">1</asp:listitem>
            <asp:listitem value="2">2</asp:listitem>
            <asp:listitem value="3">3</asp:listitem>
            <asp:listitem value="4">4</asp:listitem>
        </asp:dropdownlist>

        <p>
        <asp:checkbox id="check1" text="hide panel" runat="server"/>
            
        <p>
        <asp:button text="refresh panel" runat="server"/>

    </font>
    </form>

</body>
</html>


好了,先说到这里吧,有点儿累了。以后我将介绍剩下的13种服务器端控制。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表