如何通过从下拉列表中选择来显示选择表单

时间:2013-03-09 13:22:32

标签: asp.net c#-4.0 drop-down-menu dropdownbox

我想在下拉列表中选择值时显示不同的表单页面。我有一个两个网页首先我有下拉列表,在第二页我有一个表格现在我想要当我从我的下拉列表中选择值为该值将出现我怎么能这样做任何人可以帮助我

首先形成aspx代码:

<form id="form1" runat="server">
   <div style="margin-left: 80px">
       <asp:Label ID="lblPortal" runat="server" Text="Select Portal"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
   <asp:DropDownList ID="ddlPortal" runat="server" AutoPostBack="True" 
       onselectedindexchanged="MyPage">
       <asp:ListItem>None</asp:ListItem>
       <asp:ListItem>TRAVELONG</asp:ListItem>
       <asp:ListItem>ONETRAVEL</asp:ListItem>
       <asp:ListItem>.UK</asp:ListItem>
       <asp:ListItem>.CA-YYZ</asp:ListItem>
       <asp:ListItem>.CA-YVR</asp:ListItem>
   </asp:DropDownList>
   </div>

第二个aspx表单:

<div style="margin-left: 80px">
   <asp:Label ID="lblPortal" runat="server" Text="Select Portal"></asp:Label>

   <asp:DropDownList ID="ddlPortal" runat="server" AutoPostBack="True" 
       onselectedindexchanged="MyPage_Indexchange">
       <asp:ListItem>None</asp:ListItem>
       <asp:ListItem>TRAVELONG</asp:ListItem>
       <asp:ListItem>ONETRAVEL</asp:ListItem>
       <asp:ListItem>.UK</asp:ListItem>
       <asp:ListItem>.CA-YYZ</asp:ListItem>
       <asp:ListItem>.CA-YVR</asp:ListItem>
   </asp:DropDownList>
   <asp:ValidationSummary ID="vdm" runat="server" ForeColor="Red" />
<table class="style1">
       <tr>
           <td class="style13">
               <asp:Label ID="lblTid" runat="server" Text="TID"></asp:Label>
           </td>
           <td class="style5">
               <asp:TextBox ID="tbTid" runat="server"></asp:TextBox>
           </td>
           <td class="style4">
               <asp:RequiredFieldValidator ID="rfvTid" runat="server" 
                   ControlToValidate="tbTid" ErrorMessage="Enter Tid" Font-Bold="True" 
                   ForeColor="Red">*</asp:RequiredFieldValidator>
           </td>
           <td class="style5">
               <asp:Label ID="lblUnusedTicketAmount" runat="server" 
                   Text="Unused Ticket Amount"></asp:Label>
           </td>
           <td class="style11">
               <asp:TextBox ID="tbUnusedTicketAmount" runat="server"></asp:TextBox>
           </td>
           <td>
               <asp:RequiredFieldValidator ID="rfvUnusedTicketAmount" runat="server" 
                   ControlToValidate="tbUnusedTicketAmount" ErrorMessage="Enter Amount" 
                   Font-Bold="True" ForeColor="Red">*</asp:RequiredFieldValidator>
           </td>
           <td>
               <asp:DropDownList ID="ddlUnusedAmount" runat="server">
                   <asp:ListItem>USD</asp:ListItem>
                   <asp:ListItem>CAD</asp:ListItem>
                   <asp:ListItem>GBP</asp:ListItem>
               </asp:DropDownList>
           </td>
       </tr>
       <tr>
           <td class="style14">
               <asp:Label ID="lblPNR" runat="server" Text="PNR"></asp:Label>
           </td>
           <td class="style9">
               <asp:TextBox ID="tbPNR" runat="server"></asp:TextBox>
           </td>
           <td class="style8">
               <asp:RequiredFieldValidator ID="rfvPNR" runat="server" 
                   ControlToValidate="tbPNR" ErrorMessage="Enter PNR" Font-Bold="True" 
                   ForeColor="Red">*</asp:RequiredFieldValidator>
           </td>
           <td class="style9">
               <asp:Label ID="lblAirlinePenality" runat="server" Text="Airline Penality"></asp:Label>
           </td>
           <td class="style12">
               <asp:TextBox ID="tbAirlinePenality" runat="server"></asp:TextBox>
           </td>
           <td class="style10">
               <asp:RequiredFieldValidator ID="rfvAirlinePenality" runat="server" 
                   ControlToValidate="tbAirlinePenality" ErrorMessage="Enter Penality" 
                   Font-Bold="True" ForeColor="Red">*</asp:RequiredFieldValidator>
           </td>
           <td class="style10">
               <asp:DropDownList ID="ddlAirlinePenality" runat="server">
                   <asp:ListItem>USD</asp:ListItem>
                   <asp:ListItem>CAD</asp:ListItem>
                   <asp:ListItem>GBP</asp:ListItem>
               </asp:DropDownList>
           </td>
       </tr>
       <tr>
           <td class="style13">
               <asp:Label ID="lblTicketNumber" runat="server" Text="Ticket Number"></asp:Label>
           </td>
           <td class="style5">
               <asp:TextBox ID="tbTicketNumber" runat="server"></asp:TextBox>
           </td>
           <td class="style4">
               <asp:RequiredFieldValidator ID="rfvTicketNumber" runat="server" 
                   ControlToValidate="tbTicketNumber" ErrorMessage="Enter ESAC" Font-Bold="True" 
                   ForeColor="Red">*</asp:RequiredFieldValidator>
           </td>
           <td class="style5">
               <asp:Label ID="lblNetRefundProcess" runat="server" Text="Net Refund Process"></asp:Label>
           </td>
           <td class="style11">
               <asp:TextBox ID="tbNetRefundProcess" runat="server"></asp:TextBox>
           </td>
           <td>
               <asp:RequiredFieldValidator ID="rfvNetRefundProcess" runat="server" 
                   ControlToValidate="tbNetRefundProcess" ErrorMessage="RequiredFieldValidator" 
                   Font-Bold="True" ForeColor="Red">*</asp:RequiredFieldValidator>
           </td>
           <td>
               <asp:DropDownList ID="ddlNetRefundProcess" runat="server">
                   <asp:ListItem>USD</asp:ListItem>
                   <asp:ListItem>CAD</asp:ListItem>
                   <asp:ListItem>GBP</asp:ListItem>
               </asp:DropDownList>
           </td>
       </tr>
       <tr>
           <td class="style13">
               <asp:Label ID="lblESACCode" runat="server" Text="ESAC Code"></asp:Label>
           </td>
           <td class="style5">
               <asp:TextBox ID="tbESACCode" runat="server"></asp:TextBox>
           </td>
           <td class="style4">
               <asp:RequiredFieldValidator ID="rfvESACCode" runat="server" 
                   ControlToValidate="tbESACCode" ErrorMessage="Enter ESAC code" Font-Bold="True" 
                   ForeColor="Red">*</asp:RequiredFieldValidator>
           </td>
           <td class="style5">
               <asp:Label ID="lblRefundableCommision" runat="server" 
                   Text="Refundable Commission"></asp:Label>
           </td>
           <td class="style11">
               <asp:TextBox ID="tbRefundableCommision" runat="server"></asp:TextBox>
           </td>
           <td>
               <asp:RequiredFieldValidator ID="rfvRefundableCommission" runat="server" 
                   ControlToValidate="tbRefundableCommision" ErrorMessage="RequiredFieldValidator" 
                   Font-Bold="True" ForeColor="Red">*</asp:RequiredFieldValidator>
           </td>
           <td>
               <asp:DropDownList ID="ddlRefundableCommission" runat="server">
                   <asp:ListItem>USD</asp:ListItem>
                   <asp:ListItem>CAD</asp:ListItem>
                   <asp:ListItem>GBP</asp:ListItem>
               </asp:DropDownList>
           </td>
       </tr>
       <tr>
           <td class="style13">
               <asp:Label ID="lblWaiverCode" runat="server" Text="Waiver Code"></asp:Label>
           </td>
           <td class="style5">
               <asp:TextBox ID="tbWaiverCode" runat="server"></asp:TextBox>
           </td>
           <td class="style4">
               <asp:RequiredFieldValidator ID="rfvWaiverCode" runat="server" 
                   ControlToValidate="tbWaiverCode" ErrorMessage="Enter Waiver Code" 
                   Font-Bold="True" ForeColor="Red">*</asp:RequiredFieldValidator>
           </td>
           <td class="style5">
               <asp:Label ID="lblCouponRefunded" runat="server" Text="Coupon Refunded"></asp:Label>
           </td>
           <td class="style11">
               <asp:TextBox ID="tbCouponRefund" runat="server"></asp:TextBox>
           </td>
           <td>
               <asp:RequiredFieldValidator ID="rfvCouponRefunded" runat="server" 
                   ControlToValidate="tbCouponRefund" ErrorMessage="Enter Coupon Refund" 
                   Font-Bold="True" ForeColor="Red">*</asp:RequiredFieldValidator>
           </td>
           <td>
               &nbsp;</td>
       </tr>
       <tr>
           <td class="style13">
               <asp:Label ID="Label7" runat="server" Text="Remarks"></asp:Label>
           </td>
           <td class="style5">
               <asp:TextBox ID="tbRemarks" runat="server" TextMode="MultiLine"></asp:TextBox>
           </td>
           <td class="style4">
               <asp:RequiredFieldValidator ID="rfvRemarks" runat="server" 
                   ControlToValidate="tbRemarks" ErrorMessage="Enter Remarks" Font-Bold="True" 
                   ForeColor="Red">*</asp:RequiredFieldValidator>
           </td>
           <td class="style5">
               <asp:Label ID="lblRefundType" runat="server" Text="Refund Type"></asp:Label>
           </td>
           <td class="style11">
               <asp:DropDownList ID="ddlRefundType" runat="server">
                   <asp:ListItem>Full</asp:ListItem>
                   <asp:ListItem>Partial</asp:ListItem>
               </asp:DropDownList>
           </td>
           <td>
               <asp:RequiredFieldValidator ID="rfvRefundType" runat="server" 
                   ControlToValidate="ddlRefundType" ErrorMessage="Select Refund Type" 
                   Font-Bold="True" ForeColor="Red">*</asp:RequiredFieldValidator>
           </td>
           <td>
               &nbsp;</td>
       </tr>
       <tr>
           <td class="style13" colspan="7">
               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
               &nbsp;<asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
                   Text="Submit" />
           </td>
       </tr>
   </table> 
   </div>

Second Form .cs文件代码:

protected void Page_Load(object sender, EventArgs e)
{

}
protected void MyPage_Indexchange(object sender, EventArgs e)
{
    Response.Redirect(@"~/Default.aspx");
}

1 个答案:

答案 0 :(得分:0)

可以使用名为Cross-Page Posting的众所周知的技术来完成。简而言之:您将帖子发回到另一个页面,您可以在其中访问原始页面中的控件。

不幸的是,DropDownList没有内置此功能(例如Button具有PostBackUrl属性的控件),但PostBackOptions无需救援。

Form1(假设,这是~\default.aspx):

protected void Page_Load(object sender, EventArgs e)
{
    var pbo = new PostBackOptions(ddlPortal);
    pbo.ActionUrl = "default2.aspx";
    pbo.AutoPostBack = true;
    ddlPortal.Attributes["onchange"] = ClientScript.GetPostBackEventReference(pbo);
}

然后添加:

<%@ PreviousPageType VirtualPath="~/default.aspx" %>

到第二种形式(假设,这是default2.aspx),然后在第二页的Page_Load事件处理程序旁边添加:

if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
{
    var ddlPortal = (DropDownList)PreviousPage.FindControl("ddlPortal");
    var selectedValue = ddlPortal.SelectedValue;
    // do something with the value from the previous page
}

这很容易,不是吗?