Server.Transfer可用于两个窗体之间传递参数
例子:从订单列表页生成订货单,传递多个订单ID到订货单生成页面
订单列表页面前台代码:
<asp:GridView ID="myGridView" runat="server" AutoGenerateColumns="False" Width="98%" DataKeyNames="SalesOrderID" OnRowDataBound="myGridView_RowDataBound"> <Columns> <asp:TemplateField> <HeaderTemplate> <input onclick="checkFormAll(this.checked)" type="checkbox"> </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="chkSelect" runat="server"></asp:CheckBox> </ItemTemplate> <ItemStyle Width="20px" /> </asp:TemplateField> <asp:BoundField DataField="SalesOrderID" HeaderText="ID" ReadOnly="True"> <ItemStyle Width="50px" /> </asp:BoundField> <asp:TemplateField HeaderText="订单详情"> <ItemTemplate> <a href="javascript:void(0)" mce_href="javascript:void(0)" onclick="OpenModalDialog('SalesOrderShow.aspx?SalesOrderID=<%# Eval("SalesOrderID") %>');"> 查看</a> </ItemTemplate> <ItemStyle Width="60px" /> </asp:TemplateField> <asp:TemplateField HeaderText="用料单明细"> <ItemTemplate> <a href="javascript:void(0)" mce_href="javascript:void(0)" onclick="OpenModalDialog('NeedMaterialDetailShow.aspx?SalesOrderID=<%# Eval("SalesOrderID") %>');"> 查看</a> </ItemTemplate> <ItemStyle Width="70px" /> </asp:TemplateField> </Columns> </asp:GridView> < br> <asp:Button ID="btnCreatPurchaseOrderHeader" runat="server" Font-Size="16px" Height="30px" OnClick="btnCreatPurchaseOrderHeader_Click" Text="根据已选订单生成订货单" Width="200px" />
后台代码:
//定义一个公共变量 保存选中的订单列表 供生成订货单页面使用 public string strArrSalesOrderID = string.Empty; /// <summary> /// Handles the Click event of the btnCreatPurchaseOrderHeader control.根据所选的订单生成订货单 /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> protected void btnCreatPurchaseOrderHeader_Click(object sender, EventArgs e) { //遍历所有的 chkSelect foreach (GridViewRow gr in this.myGridView.Rows) { CheckBox chk = (CheckBox)gr.FindControl("chkSelect"); if (chk.Checked) { strArrSalesOrderID = strArrSalesOrderID + this.myGridView.DataKeys[gr.RowIndex].Values["SalesOrderID"].ToString() + ","; } } if (strArrSalesOrderID != string.Empty) { //去掉后面的逗号 strArrSalesOrderID = strArrSalesOrderID.Substring(0, strArrSalesOrderID.Length - 1); Server.Transfer("PurchaseOrderHeaderEditFromSalesOrder.aspx"); } else { UART.Common.MsgBox.Alert("请选择订单!", Request.Url.ToString()); } }
接收页面后台代码:
//设置一个订货单ID字符串,保存生成的订货单ID private string strArrPurchaseOrderHeaderID = string.Empty; /// <summary> /// Handles the Load event of the Page control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> protected void Page_Load(object sender, EventArgs e) { //UART.Common.CheckRoleAndPermission.CheckLogin(); //UART.Common.CheckRoleAndPermission.Check(0, 10); if (!this.IsPostBack) { //实例上一个窗体SalesOrderListForPurchaseOrderHeader.aspx SalesOrderListForPurchaseOrderHeader newWeb = (SalesOrderListForPurchaseOrderHeader)Context.Handler; ; this.lblstrArrSalesOrderID.Text = newWeb.strArrSalesOrderID; //绑定订单列表 this.BindSalesOrder(); } }
然后可根据传过来的参数继续下一步操作