New "databind" method in .net

Today I found out a new method that can bind various types data to server side controls, even html controls are run on server side.

 

In gerneral, we can use:

 

DataGrid.DataSource=data;

DataGrid.DataBind();

 

to bind data to the grid control. the bind function was occured by the control self, a view was published on MSDN; when DataBind() function was invoked, it can bind the data to the control and its children controls. so I made the below test that gave me a good result. you can see the codes please.

==============================/*
<html>
<head>
 
    <script language="C#" runat="server">

        void Page_Load(Object sender, EventArgs e) {
            Page.DataBind();
        }
       
        string custID{
            get {
                return "ALFKI";
            }
        }
       
        int orderCount{
            get {
                return 11;
            }
        }


    </script>

</head>
<body>

    <h3><font face="宋体">到页属性的数据绑定</font></h3>

    <form runat=server>
   
        客户:<b><%# custID %></b><br>
        未结的订单:<b><%# orderCount %></b>

    </form>

</body>
</html>

 =============================================*/


 

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

        void SubmitBtn_Click(Object sender, EventArgs e) {
                  
          Page.DataBind();
        }

    </script>

</head>
<body>

    <h3><font face="宋体">到另一个服务器控件的属性的数据绑定</font></h3>

    <form runat=server>

        <asp:DropDownList id="StateList" runat="server">
          <asp:ListItem>CA</asp:ListItem>
          <asp:ListItem>IN</asp:ListItem>
          <asp:ListItem>KS</asp:ListItem>
          <asp:ListItem>MD</asp:ListItem>
          <asp:ListItem>MI</asp:ListItem>
          <asp:ListItem>OR</asp:ListItem>
          <asp:ListItem>TN</asp:ListItem>
          <asp:ListItem>UT</asp:ListItem>
        </asp:DropDownList>
       
        <asp:button Text="提交" OnClick="SubmitBtn_Click" runat=server/>
       
        <p>
       
        选定的州:<asp:label text='<%# StateList.SelectedItem.Text %>' runat=server/>
       
    </form>

</body>
</html>

=========================================*/


I am sure that you can find out a trick through these codes. using this method can save many manual codes. is it?





posted @ 2005-04-04 13:47  23热爱,自学业余码农。  阅读(353)  评论(1)    收藏  举报