如何从一个webform中的下拉列表中选择一个选项,并在另一个webform中的gridview中查看结果?

时间:2013-07-08 05:33:18

标签: asp.net vb.net gridview drop-down-menu

这是我的项目,我正在使用visual studio 2010,我的编码语言是vb。我是新手,可以说我还不太了解它。

我有一个登录页面,部门负责人输入他的详细信息(员工编号,员工姓名),选择他的部门并提交信息,以便以其他网络形式查看结果,即所有在其部门下的员工所有为公司工作的员工。

因为我不知道代码,所以我得到gridview中每个部门的所有员工,而不是从下拉列表中选择部门的员工。

这是第一页的代码

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="login.aspx.vb" Inherits="login" %>

<!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">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 1179px;
height: 106px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<br />
<img alt="iocl" class="style1" src="Images/materials_hrd.bmp" /><br />
<br />
<br />
<br />
</div>
<asp:Label ID="Label1" runat="server" Text="Employee Name"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
<asp:Label ID="Employee_Number" runat="server" Text="Employee Number"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
-<br />
<asp:Label ID="Label2" runat="server" Text="Department"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Information Systems</asp:ListItem>
<asp:ListItem>Human Resources</asp:ListItem>
<asp:ListItem>Resource &amp; Development</asp:ListItem>
<asp:ListItem>Finance</asp:ListItem>
<asp:ListItem>Pipelines</asp:ListItem>
<asp:ListItem>Marketing</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Submit" />
</form>
</body>
</html>

这是第二页的代码

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="HODLogin.aspx.vb" Inherits="HODLogin" %>

<!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">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 1259px;
height: 105px;
margin-top: 0px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>

<br />
<br />
<img alt="iocl" class="style1" src="Images/materials_hrd.bmp" /><br />
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
CellPadding="4" DataKeyNames="ID" DataSourceID="SqlDataSource1" 
ForeColor="#333333" GridLines="None" Height="195px" ShowHeaderWhenEmpty="True" 
Width="1125px">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Employee Number" HeaderText="Employee Number" 
SortExpression="Employee Number" />
<asp:BoundField DataField="Employee Name" HeaderText="Employee Name" 
SortExpression="Employee Name" />
<asp:BoundField DataField="Department" HeaderText="Department" 
SortExpression="Department" />
<asp:BoundField DataField="Grade" HeaderText="Grade" SortExpression="Grade" />
<asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />

</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:ConnectionString11 %>" 
ProviderName="<%$ ConnectionStrings:ConnectionString11.ProviderName %>" 
SelectCommand="SELECT * FROM [employee database]"></asp:SqlDataSource>
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:Label ID="lblempno" runat="server" 
Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Submit" />
<br />

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
</asp:UpdatePanel>
    </div>
</form>
</body>
</html>

请告诉我流程和代码,以及放在哪里,我对这一切都不是很熟悉。这是一种紧急情况。

2 个答案:

答案 0 :(得分:0)

您可以将所选项目文本存储在onw变量中,如下所示:

dim selText as string = DropDownList1.SelectedItem.Text

点击“提交”按钮,执行所需任务,然后将页面重定向到另一个页面以及部门名称。如下面的代码:

 Response.Redirect("Page2.aspx?department=" + selText)

并在第二页上,即Page2.aspx,只需在查询字符串中接收此值,使用以下代码..无论是在页面加载还是在您需要的地方都需要。

 If Request.QueryString("department") <> Nothing Then
  dim departmt as string = Request.QueryString("department")
 End If

在数据库查询中使用上述变量的值,并将相关数据提供给在上一个表单中选择的部门。

我希望它会对你有所帮助。 :)

答案 1 :(得分:0)

您还可以在会话中保存所选项目文本。之后,您可以从项目的任何页面获取此值:

Session("department") = DropDownList1.SelectedItem.Text

您也可以直接在Page2上的SqlDataSource中使用此值:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:ConnectionString11 %>" 
ProviderName="<%$ ConnectionStrings:ConnectionString11.ProviderName %>" 
SelectCommand="SELECT * FROM [employee database] WHERE Department LIKE @depart">
<SelectParameters>
        <asp:SessionParameter DefaultValue="%" Name="depart" SessionField="user" 
            Type="string" />
    </SelectParameters>
</asp:SqlDataSource>