UserControl是 .NET FRAMEWORK 2.0 非常好用的功能,
一旦物件建立之後, 包裝起來. 就可以重複使用.

testQuery.aspx

<%@ Page Language=”VB” %>

<%@ Register Src=”ucQueryCustomers.ascx” TagName=”ucQueryCustomers” TagPrefix=”uc1″ %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>

<script runat=”server”>

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        UcQueryCustomers1.Visible = True
    End Sub

    ‘3, 使用事件
    Protected Sub UcQueryCustomers1_Selected(ByVal sender As Object, ByVal e As System.EventArgs)
        TextBox1.Text = UcQueryCustomers1.CustomerID
        UcQueryCustomers1.Visible = False
    End Sub
</script>

<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
    <title>Untitled Page</title>
</head>
<body>
    <form id=”form1″ runat=”server”>
    <div>
        訂單<br />
        <br />
        Customer ID :
        <asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>
        <asp:Button ID=”Button1″ runat=”server” OnClick=”Button1_Click” Text=”…” /><br />
        <br />
        <uc1:ucQueryCustomers ID=”UcQueryCustomers1″ runat=”server” Visible=”false” OnSelected=”UcQueryCustomers1_Selected” />
   
    </div>
    </form>
</body>
</html>

———

(User Control: UcQueryCustomers1.ascx)

———

<%@ Control Language=”VB” ClassName=”ucQueryCustomers” %>

<script runat=”server”>

    Public ReadOnly Property CustomerID() As String
        Get
            Return GridView1.SelectedValue
        End Get
    End Property
   
    ‘1, 對外宣告事件
    Public Event Selected As EventHandler
   
    ‘2, 在正確時間點觸發事件給外界
    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        RaiseEvent Selected(sender, e)
    End Sub
</script>

<div>
    查詢 :
    <asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>
    <asp:Button ID=”Button1″ runat=”server” Text=”查詢” /><br />
    <asp:GridView ID=”GridView1″ runat=”server” AllowPaging=”True” AutoGenerateColumns=”False”
        CellPadding=”4″ DataKeyNames=”CustomerID” DataSourceID=”SqlDataSource1″ EmptyDataText=”There are no data records to display.”
        ForeColor=”#333333″ GridLines=”None” OnSelectedIndexChanged=”GridView1_SelectedIndexChanged”>
        <FooterStyle BackColor=”#1C5E55″ Font-Bold=”True” ForeColor=”White” />
        <Columns>
            <asp:CommandField ShowSelectButton=”True” />
            <asp:BoundField DataField=”CustomerID” HeaderText=”CustomerID” ReadOnly=”True” SortExpression=”CustomerID” />
            <asp:BoundField DataField=”ContactTitle” HeaderText=”ContactTitle” SortExpression=”ContactTitle” />
            <asp:BoundField DataField=”CompanyName” HeaderText=”CompanyName” SortExpression=”CompanyName” />
            <asp:BoundField DataField=”ContactName” HeaderText=”ContactName” SortExpression=”ContactName” />
        </Columns>
        <RowStyle BackColor=”#E3EAEB” />
        <EditRowStyle BackColor=”#7C6F57″ />
        <SelectedRowStyle BackColor=”#C5BBAF” Font-Bold=”True” ForeColor=”#333333″ />
        <PagerStyle BackColor=”#666666″ ForeColor=”White” HorizontalAlign=”Center” />
        <HeaderStyle BackColor=”#1C5E55″ Font-Bold=”True” ForeColor=”White” />
        <AlternatingRowStyle BackColor=”White” />
    </asp:GridView>
    <asp:SqlDataSource ID=”SqlDataSource1″ runat=”server” ConnectionString=”<%$ ConnectionStrings:NorthwindConnectionString1 %>”
        DeleteCommand=”DELETE FROM [Customers] WHERE [CustomerID] = @CustomerID” InsertCommand=”INSERT INTO [Customers] ([CustomerID], [ContactTitle], [CompanyName], [ContactName]) VALUES (@CustomerID, @ContactTitle, @CompanyName, @ContactName)”
        ProviderName=”<%$ ConnectionStrings:NorthwindConnectionString1.ProviderName %>”
        SelectCommand=”SELECT [CustomerID], [ContactTitle], [CompanyName], [ContactName] FROM [Customers] WHERE ([CompanyName] LIKE ‘%’ + @CompanyName + ‘%’)”
        UpdateCommand=”UPDATE [Customers] SET [ContactTitle] = @ContactTitle, [CompanyName] = @CompanyName, [ContactName] = @ContactName WHERE [CustomerID] = @CustomerID”>
        <DeleteParameters>
            <asp:Parameter Name=”CustomerID” Type=”String” />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name=”ContactTitle” Type=”String” />
            <asp:Parameter Name=”CompanyName” Type=”String” />
            <asp:Parameter Name=”ContactName” Type=”String” />
            <asp:Parameter Name=”CustomerID” Type=”String” />
        </UpdateParameters>
        <SelectParameters>
            <asp:ControlParameter ControlID=”TextBox1″ Name=”CompanyName” PropertyName=”Text”
                Type=”String” />
        </SelectParameters>
        <InsertParameters>
            <asp:Parameter Name=”CustomerID” Type=”String” />
            <asp:Parameter Name=”ContactTitle” Type=”String” />
            <asp:Parameter Name=”CompanyName” Type=”String” />
            <asp:Parameter Name=”ContactName” Type=”String” />
        </InsertParameters>
    </asp:SqlDataSource>
</div>

—————-

 

Leave a reply

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

required