业务逻辑>数据层>SqlDataSource>sql的输入参数>控件参数>System.Web.UI.WebControls.GridView.SelectedValue
前台代码
1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CH7_DemoForm008.aspx.cs" %>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 <html xmlns="http://www.w3.org/1999/xhtml">
6 <head id="Head1" runat="server">
7 <title>示范使用 GridView 控件作为参数的控件源</title>
8 </head>
9 <body>
10 <form id="form1" runat="server">
11 <div style="text-align: center">
12 <a target="_blank" href="http://www.cnblogs.com/liminzhang">
13 <img style="border: 0;" alt="" src="Images/CH7_DemoForm008_Banner.jpg"
14 title="前往章立民研究室" /></a>
15 <hr />
16 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
17 AutoGenerateColumns="False" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None"
18 BorderWidth="1px" CellPadding="3" DataKeyNames="订单号码,客户编号" DataSourceID="SqlDataSource1"
19 GridLines="Horizontal" PageSize="5" SelectedIndex="0" Width="473px">
20 <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
21 <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
22 <Columns>
23 <asp:CommandField ShowSelectButton="True" />
24 <asp:BoundField DataField="客户编号" HeaderText="客户编号" ReadOnly="True" SortExpression="客户编号" />
25 <asp:BoundField DataField="公司名称" HeaderText="公司名称" SortExpression="公司名称" />
26 <asp:BoundField DataField="订单号码" HeaderText="订单号码" InsertVisible="False" ReadOnly="True"
27 SortExpression="订单号码" />
28 <asp:BoundField DataField="订单日期" DataFormatString="{0:d}" HeaderText="订单日期" SortExpression="订单日期" />
29 </Columns>
30 <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
31 <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
32 <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
33 <AlternatingRowStyle BackColor="#F7F7F7" />
34 </asp:GridView>
35 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:chtNorthwind %>"
36 SelectCommand="SELECT 客户.客户编号, 客户.公司名称, 订货主档.订单号码, 订货主档.订单日期 FROM 客户 INNER JOIN 订货主档 ON 客户.客户编号 = 订货主档.客户编号 ORDER BY 客户.客户编号, 订货主档.订单号码">
37 </asp:SqlDataSource>
38 <hr />
39 <asp:ListView ID="ListView1" runat="server" DataKeyNames="产品编号,订单号码" DataSourceID="SqlDataSource2">
40 <AlternatingItemTemplate>
41 <tr style="background-color: #FFFFFF; color: #284775;">
42 <td>
43 <asp:Label ID="折扣Label" runat="server" Text='<%# Eval("折扣") %>' />
44 </td>
45 <td>
46 <asp:Label ID="数量Label" runat="server" Text='<%# Eval("数量") %>' />
47 </td>
48 <td>
49 <asp:Label ID="单价Label" runat="server" Text='<%# Eval("单价") %>' />
50 </td>
51 <td>
52 <asp:Label ID="产品编号Label" runat="server" Text='<%# Eval("产品编号") %>' />
53 </td>
54 <td>
55 <asp:Label ID="订单号码Label" runat="server" Text='<%# Eval("订单号码") %>' />
56 </td>
57 <td>
58 <asp:Label ID="订购金额Label" runat="server" Text='<%# Eval("订购金额") %>' />
59 </td>
60 </tr>
61 </AlternatingItemTemplate>
62 <LayoutTemplate>
63 <table id="Table1" runat="server">
64 <tr id="Tr1" runat="server">
65 <td id="Td1" runat="server">
66 <table id="itemPlaceholderContainer" runat="server" border="1" style="background-color: #FFFFFF;
67 border-collapse: collapse; border-color: #999999; border-style: none; border-width: 1px;
68 font-family: Verdana, Arial, Helvetica, sans-serif;">
69 <tr id="Tr2" runat="server" style="background-color: #E0FFFF; color: #333333;">
70 <th id="Th1" runat="server">
71 折扣
72 </th>
73 <th id="Th2" runat="server">
74 数量
75 </th>
76 <th id="Th3" runat="server">
77 单价
78 </th>
79 <th id="Th4" runat="server">
80 产品编号
81 </th>
82 <th id="Th5" runat="server">
83 订单号码
84 </th>
85 <th id="Th6" runat="server">
86 订购金额
87 </th>
88 </tr>
89 <tr id="itemPlaceholder" runat="server">
90 </tr>
91 </table>
92 </td>
93 </tr>
94 <tr id="Tr3" runat="server">
95 <td id="Td2" runat="server" style="text-align: center; background-color: #5D7B9D; font-family: Verdana, Arial, Helvetica, sans-serif;
96 color: #FFFFFF">
97 </td>
98 </tr>
99 </table>
100 </LayoutTemplate>
101 <InsertItemTemplate>
102 <tr style="">
103 <td>
104 <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="插入" />
105 <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="清除" />
106 </td>
107 <td>
108 <asp:TextBox ID="折扣TextBox" runat="server" Text='<%# Bind("折扣") %>' />
109 </td>
110 <td>
111 <asp:TextBox ID="数量TextBox" runat="server" Text='<%# Bind("数量") %>' />
112 </td>
113 <td>
114 <asp:TextBox ID="单价TextBox" runat="server" Text='<%# Bind("单价") %>' />
115 </td>
116 <td>
117 <asp:TextBox ID="产品编号TextBox" runat="server" Text='<%# Bind("产品编号") %>' />
118 </td>
119 <td>
120 <asp:TextBox ID="订单号码TextBox" runat="server" Text='<%# Bind("订单号码") %>' />
121 </td>
122 <td>
123 <asp:TextBox ID="订购金额TextBox" runat="server" Text='<%# Bind("订购金额") %>' />
124 </td>
125 </tr>
126 </InsertItemTemplate>
127 <SelectedItemTemplate>
128 <tr style="background-color: #E2DED6; font-weight: bold; color: #333333;">
129 <td>
130 <asp:Label ID="折扣Label" runat="server" Text='<%# Eval("折扣") %>' />
131 </td>
132 <td>
133 <asp:Label ID="数量Label" runat="server" Text='<%# Eval("数量") %>' />
134 </td>
135 <td>
136 <asp:Label ID="单价Label" runat="server" Text='<%# Eval("单价") %>' />
137 </td>
138 <td>
139 <asp:Label ID="产品编号Label" runat="server" Text='<%# Eval("产品编号") %>' />
140 </td>
141 <td>
142 <asp:Label ID="订单号码Label" runat="server" Text='<%# Eval("订单号码") %>' />
143 </td>
144 <td>
145 <asp:Label ID="订购金额Label" runat="server" Text='<%# Eval("订购金额") %>' />
146 </td>
147 </tr>
148 </SelectedItemTemplate>
149 <EmptyDataTemplate>
150 <table id="Table2" runat="server" style="background-color: #FFFFFF; border-collapse: collapse;
151 border-color: #999999; border-style: none; border-width: 1px;">
152 <tr>
153 <td>
154 未传回资料。
155 </td>
156 </tr>
157 </table>
158 </EmptyDataTemplate>
159 <EditItemTemplate>
160 <tr style="background-color: #999999;">
161 <td>
162 <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="更新" />
163 <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="取消" />
164 </td>
165 <td>
166 <asp:TextBox ID="折扣TextBox" runat="server" Text='<%# Bind("折扣") %>' />
167 </td>
168 <td>
169 <asp:TextBox ID="数量TextBox" runat="server" Text='<%# Bind("数量") %>' />
170 </td>
171 <td>
172 <asp:TextBox ID="单价TextBox" runat="server" Text='<%# Bind("单价") %>' />
173 </td>
174 <td>
175 <asp:Label ID="产品编号Label1" runat="server" Text='<%# Eval("产品编号") %>' />
176 </td>
177 <td>
178 <asp:Label ID="订单号码Label1" runat="server" Text='<%# Eval("订单号码") %>' />
179 </td>
180 <td>
181 <asp:TextBox ID="订购金额TextBox" runat="server" Text='<%# Bind("订购金额") %>' />
182 </td>
183 </tr>
184 </EditItemTemplate>
185 <ItemTemplate>
186 <tr style="background-color: #E0FFFF; color: #333333;">
187 <td>
188 <asp:Label ID="折扣Label" runat="server" Text='<%# Eval("折扣") %>' />
189 </td>
190 <td>
191 <asp:Label ID="数量Label" runat="server" Text='<%# Eval("数量") %>' />
192 </td>
193 <td>
194 <asp:Label ID="单价Label" runat="server" Text='<%# Eval("单价") %>' />
195 </td>
196 <td>
197 <asp:Label ID="产品编号Label" runat="server" Text='<%# Eval("产品编号") %>' />
198 </td>
199 <td>
200 <asp:Label ID="订单号码Label" runat="server" Text='<%# Eval("订单号码") %>' />
201 </td>
202 <td>
203 <asp:Label ID="订购金额Label" runat="server" Text='<%# Eval("订购金额") %>' />
204 </td>
205 </tr>
206 </ItemTemplate>
207 </asp:ListView>
208 <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:chtNorthwind %>"
209 SelectCommand="SELECT 折扣, 数量, 单价, 产品编号, 订单号码, (单价 * 数量) * (1 - 折扣) AS 订购金额 FROM 订货明细 WHERE (订单号码 = @订单号码)">
210 <SelectParameters>
211 <asp:ControlParameter ControlID="GridView1" Name="订单号码" PropertyName="SelectedValue"
212 Type="Int32" />
213 </SelectParameters>
214 </asp:SqlDataSource>
215 <hr />
216 </div>
217 </form>
218 </body>
219 </html>
220
221
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5 <html xmlns="http://www.w3.org/1999/xhtml">
6 <head id="Head1" runat="server">
7 <title>示范使用 GridView 控件作为参数的控件源</title>
8 </head>
9 <body>
10 <form id="form1" runat="server">
11 <div style="text-align: center">
12 <a target="_blank" href="http://www.cnblogs.com/liminzhang">
13 <img style="border: 0;" alt="" src="Images/CH7_DemoForm008_Banner.jpg"
14 title="前往章立民研究室" /></a>
15 <hr />
16 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
17 AutoGenerateColumns="False" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None"
18 BorderWidth="1px" CellPadding="3" DataKeyNames="订单号码,客户编号" DataSourceID="SqlDataSource1"
19 GridLines="Horizontal" PageSize="5" SelectedIndex="0" Width="473px">
20 <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
21 <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
22 <Columns>
23 <asp:CommandField ShowSelectButton="True" />
24 <asp:BoundField DataField="客户编号" HeaderText="客户编号" ReadOnly="True" SortExpression="客户编号" />
25 <asp:BoundField DataField="公司名称" HeaderText="公司名称" SortExpression="公司名称" />
26 <asp:BoundField DataField="订单号码" HeaderText="订单号码" InsertVisible="False" ReadOnly="True"
27 SortExpression="订单号码" />
28 <asp:BoundField DataField="订单日期" DataFormatString="{0:d}" HeaderText="订单日期" SortExpression="订单日期" />
29 </Columns>
30 <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
31 <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
32 <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
33 <AlternatingRowStyle BackColor="#F7F7F7" />
34 </asp:GridView>
35 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:chtNorthwind %>"
36 SelectCommand="SELECT 客户.客户编号, 客户.公司名称, 订货主档.订单号码, 订货主档.订单日期 FROM 客户 INNER JOIN 订货主档 ON 客户.客户编号 = 订货主档.客户编号 ORDER BY 客户.客户编号, 订货主档.订单号码">
37 </asp:SqlDataSource>
38 <hr />
39 <asp:ListView ID="ListView1" runat="server" DataKeyNames="产品编号,订单号码" DataSourceID="SqlDataSource2">
40 <AlternatingItemTemplate>
41 <tr style="background-color: #FFFFFF; color: #284775;">
42 <td>
43 <asp:Label ID="折扣Label" runat="server" Text='<%# Eval("折扣") %>' />
44 </td>
45 <td>
46 <asp:Label ID="数量Label" runat="server" Text='<%# Eval("数量") %>' />
47 </td>
48 <td>
49 <asp:Label ID="单价Label" runat="server" Text='<%# Eval("单价") %>' />
50 </td>
51 <td>
52 <asp:Label ID="产品编号Label" runat="server" Text='<%# Eval("产品编号") %>' />
53 </td>
54 <td>
55 <asp:Label ID="订单号码Label" runat="server" Text='<%# Eval("订单号码") %>' />
56 </td>
57 <td>
58 <asp:Label ID="订购金额Label" runat="server" Text='<%# Eval("订购金额") %>' />
59 </td>
60 </tr>
61 </AlternatingItemTemplate>
62 <LayoutTemplate>
63 <table id="Table1" runat="server">
64 <tr id="Tr1" runat="server">
65 <td id="Td1" runat="server">
66 <table id="itemPlaceholderContainer" runat="server" border="1" style="background-color: #FFFFFF;
67 border-collapse: collapse; border-color: #999999; border-style: none; border-width: 1px;
68 font-family: Verdana, Arial, Helvetica, sans-serif;">
69 <tr id="Tr2" runat="server" style="background-color: #E0FFFF; color: #333333;">
70 <th id="Th1" runat="server">
71 折扣
72 </th>
73 <th id="Th2" runat="server">
74 数量
75 </th>
76 <th id="Th3" runat="server">
77 单价
78 </th>
79 <th id="Th4" runat="server">
80 产品编号
81 </th>
82 <th id="Th5" runat="server">
83 订单号码
84 </th>
85 <th id="Th6" runat="server">
86 订购金额
87 </th>
88 </tr>
89 <tr id="itemPlaceholder" runat="server">
90 </tr>
91 </table>
92 </td>
93 </tr>
94 <tr id="Tr3" runat="server">
95 <td id="Td2" runat="server" style="text-align: center; background-color: #5D7B9D; font-family: Verdana, Arial, Helvetica, sans-serif;
96 color: #FFFFFF">
97 </td>
98 </tr>
99 </table>
100 </LayoutTemplate>
101 <InsertItemTemplate>
102 <tr style="">
103 <td>
104 <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="插入" />
105 <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="清除" />
106 </td>
107 <td>
108 <asp:TextBox ID="折扣TextBox" runat="server" Text='<%# Bind("折扣") %>' />
109 </td>
110 <td>
111 <asp:TextBox ID="数量TextBox" runat="server" Text='<%# Bind("数量") %>' />
112 </td>
113 <td>
114 <asp:TextBox ID="单价TextBox" runat="server" Text='<%# Bind("单价") %>' />
115 </td>
116 <td>
117 <asp:TextBox ID="产品编号TextBox" runat="server" Text='<%# Bind("产品编号") %>' />
118 </td>
119 <td>
120 <asp:TextBox ID="订单号码TextBox" runat="server" Text='<%# Bind("订单号码") %>' />
121 </td>
122 <td>
123 <asp:TextBox ID="订购金额TextBox" runat="server" Text='<%# Bind("订购金额") %>' />
124 </td>
125 </tr>
126 </InsertItemTemplate>
127 <SelectedItemTemplate>
128 <tr style="background-color: #E2DED6; font-weight: bold; color: #333333;">
129 <td>
130 <asp:Label ID="折扣Label" runat="server" Text='<%# Eval("折扣") %>' />
131 </td>
132 <td>
133 <asp:Label ID="数量Label" runat="server" Text='<%# Eval("数量") %>' />
134 </td>
135 <td>
136 <asp:Label ID="单价Label" runat="server" Text='<%# Eval("单价") %>' />
137 </td>
138 <td>
139 <asp:Label ID="产品编号Label" runat="server" Text='<%# Eval("产品编号") %>' />
140 </td>
141 <td>
142 <asp:Label ID="订单号码Label" runat="server" Text='<%# Eval("订单号码") %>' />
143 </td>
144 <td>
145 <asp:Label ID="订购金额Label" runat="server" Text='<%# Eval("订购金额") %>' />
146 </td>
147 </tr>
148 </SelectedItemTemplate>
149 <EmptyDataTemplate>
150 <table id="Table2" runat="server" style="background-color: #FFFFFF; border-collapse: collapse;
151 border-color: #999999; border-style: none; border-width: 1px;">
152 <tr>
153 <td>
154 未传回资料。
155 </td>
156 </tr>
157 </table>
158 </EmptyDataTemplate>
159 <EditItemTemplate>
160 <tr style="background-color: #999999;">
161 <td>
162 <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="更新" />
163 <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="取消" />
164 </td>
165 <td>
166 <asp:TextBox ID="折扣TextBox" runat="server" Text='<%# Bind("折扣") %>' />
167 </td>
168 <td>
169 <asp:TextBox ID="数量TextBox" runat="server" Text='<%# Bind("数量") %>' />
170 </td>
171 <td>
172 <asp:TextBox ID="单价TextBox" runat="server" Text='<%# Bind("单价") %>' />
173 </td>
174 <td>
175 <asp:Label ID="产品编号Label1" runat="server" Text='<%# Eval("产品编号") %>' />
176 </td>
177 <td>
178 <asp:Label ID="订单号码Label1" runat="server" Text='<%# Eval("订单号码") %>' />
179 </td>
180 <td>
181 <asp:TextBox ID="订购金额TextBox" runat="server" Text='<%# Bind("订购金额") %>' />
182 </td>
183 </tr>
184 </EditItemTemplate>
185 <ItemTemplate>
186 <tr style="background-color: #E0FFFF; color: #333333;">
187 <td>
188 <asp:Label ID="折扣Label" runat="server" Text='<%# Eval("折扣") %>' />
189 </td>
190 <td>
191 <asp:Label ID="数量Label" runat="server" Text='<%# Eval("数量") %>' />
192 </td>
193 <td>
194 <asp:Label ID="单价Label" runat="server" Text='<%# Eval("单价") %>' />
195 </td>
196 <td>
197 <asp:Label ID="产品编号Label" runat="server" Text='<%# Eval("产品编号") %>' />
198 </td>
199 <td>
200 <asp:Label ID="订单号码Label" runat="server" Text='<%# Eval("订单号码") %>' />
201 </td>
202 <td>
203 <asp:Label ID="订购金额Label" runat="server" Text='<%# Eval("订购金额") %>' />
204 </td>
205 </tr>
206 </ItemTemplate>
207 </asp:ListView>
208 <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:chtNorthwind %>"
209 SelectCommand="SELECT 折扣, 数量, 单价, 产品编号, 订单号码, (单价 * 数量) * (1 - 折扣) AS 订购金额 FROM 订货明细 WHERE (订单号码 = @订单号码)">
210 <SelectParameters>
211 <asp:ControlParameter ControlID="GridView1" Name="订单号码" PropertyName="SelectedValue"
212 Type="Int32" />
213 </SelectParameters>
214 </asp:SqlDataSource>
215 <hr />
216 </div>
217 </form>
218 </body>
219 </html>
220
221
请摒弃样式的研究,语法的限制,专注于控件函数的观点。
那便是:
读取数据(sql语句)
{
sql语句(输入参数);
}
输入参数便是,
<asp:ControlParameter ControlID="GridView1" Name="订单号码" PropertyName="SelectedValue" Type="Int32" />
合乎自然而生生不息。。。