close

ASP.NET 2.0中callback的一些變化+使用示例(中)


本文地址:http://qzone.qq.com/blog/101766959-1217964930

 
 
在下面的完整代碼裏我還做了如下兩件事,以提高代碼的複用性: 對_callbackEventArgument稍加處理,以便調用任意多個參數的方法(FillProduct只有一個參數) 提取了FillProduct中Rander Control的那部分,以便於其他方法也可以使用。(這項操作用VS2005的Refactor很容易就完成了 其實你可以把這兩部分整理到一個CallbackHelp類中,這樣複用性就更高了。
Enjoy it.
Callback.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Callback.aspx.cs" Inherits="Callback" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<script type="text/javascript">
   function CallServer(arg, context)
     {
          context.innerHTML = "Loading";
        <%= ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context") %>;
      }

    function ReceiveServerData(result, context)
      {
          context.innerHTML = result;
      }
</script>

<head runat="server">
    <title>Callback</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                  ConnectionString="<%$ ConnectionStrings:NORTHWNDConnectionString1 %>"
                  SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories]">
              </asp:SqlDataSource>
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                  ConnectionString="<%$ ConnectionStrings:NORTHWNDConnectionString1 %>"
                  SelectCommand="SELECT [ProductID], [ProductName] FROM [Products] WHERE ([CategoryID] = @CategoryID)">
                <SelectParameters>
                    <asp:ControlParameter ControlID="ddlCategory" Name="CategoryID"
                            PropertyName="SelectedValue" />
                </SelectParameters>
            </asp:SqlDataSource>
            <div id="_div1" runat="server">
                <asp:DropDownList ID="ddlCategory" runat="server" 
                      DataSourceID="SqlDataSource1" DataTextField="CategoryName"
                      DataValueField="CategoryID" AppendDataBoundItems="True">
                    <asp:ListItem Value="">- Select Category -</asp:ListItem>
                </asp:DropDownList>
                <span id="_span1">
                    <asp:DropDownList ID="ddlProduct" runat="server" 
                          DataSourceID="SqlDataSource2" DataTextField="ProductName"
                          AppendDataBoundItems="True">
                        <asp:ListItem Value="">- Select Product -</asp:ListItem>
                    </asp:DropDownList>
                </span><span id="_span2">
                    <asp:Button ID="btnBuy" runat="server" Text="Buy" Enabled="false" OnClick="btnBuy_Click" />
                    <br />
                </span>
            </div>
            <asp:Label ID="Label1" runat="server"></asp:Label>
        </div>
    </form>
</body>
</html>

ASP.NET 2.0中callback的一些變化+使用示例(上)

ASP.NET 2.0中callback的一些變化+使用示例(中)

ASP.NET 2.0中callback的一些變化+使用示例(下)


arrow
arrow
    全站熱搜

    Roger 發表在 痞客邦 留言(0) 人氣()