泥鳅无泥养殖:两段代码实现同样的功能,有什么本质的不同(考到记事本中看)

来源:百度文库 编辑:神马品牌网 时间:2024/05/04 12:49:58
<html>
<head>

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

void SubmitBtn_Click(Object sender, EventArgs e) {

// 仅调用“Page.DataBind”,而不是从“StateList”
// 中显式取出变量,然后操作标签控件。
// 这将计算页内所有的 <%# %> 表达式

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 id="Label1" text='<%# StateList.SelectedItem.Text %>' runat=server/> <!-- !!!!!!!!!!!! -->

</form>

</body>
</html>

//////////////////////////////////////////////////////////////////////////////////////////////////////

<html>
<head>

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

void SubmitBtn_Click(Object sender, EventArgs e) {

// 仅调用“Page.DataBind”,而不是从“StateList”
// 中显式取出变量,然后操作标签控件。
// 这将计算页内所有的 <%# %> 表达式

Label1.Text = LabelStateList.Text; ///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
}

</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 id = "Label1" runat=server/> <!--///!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->

</form>

</body>
</html>

功能是一样,但是本质是不同的,效率也是不一样的.

前者是通过DropDownList这个服务器端控件进行数据绑定,来显示这个结果,而后者紧紧是通过取出DropDownList的VALUE来显示.

效率上肯定是后者大于前者,但是数据绑定是.NET中的核心问题,也是重点.推荐用第一种方法来做,虽然比较复杂,要写绑定的方法,但是可以学到一些更深层次的东西.

太长了 能不能短点写

功能一样,本质上也没什么不同,效率上也相当。
只不过一个是用类似ASP的方式赋值,另一个直接操作WEB控件的属性罢了。