2011-04-28 151 views
0

我一直在嘗試從兩個dropdownlist(page1.aspx)傳遞選定的數據到(page2.aspx)我沒有任何運氣,因爲它似乎沒有被選中時傳遞數據。請幫助,這似乎並不困難,但我無法得到它的工作。在(page2.aspx)上,下拉列表中的數據將被傳遞給一個存儲過程,所有結果都將在gridview中。從下拉列表中傳遞數據

這裏是我的代碼:

Page1.aspx的

<asp:DropDownList ID="ddlState" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="ST_Code" DataValueField="ST_Code" /> 
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbConnection %>"SelectCommand="SELECT [ST_Code] FROM [State]"> 
</asp:SqlDataSource> City: <asp:DropDownList ID="ddlCity" runat="server" DataSourceID="SqlDataSource2" DataTextField="RS_City" DataValueField="RS_City" /> 
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:dbConnection %>"SelectCommand="ListbyStateSPROC"SelectCommandType="StoredProcedure">` 
     <SelectParameters> 
      <asp:ControlParameter ControlID="ddlState" Name="State" PropertyName="SelectedValue" Type="String" />  
     </SelectParameters> 
</asp:SqlDataSource> 

     <asp:Button ID="Submit" runat="server" Text="Submit" /> 
    &nbsp; 

</asp:Content> 

Page1.aspx.vb

Imports System.Data 
Imports System.Data.Common 
Imports System.Data.SqlClient 
Imports System.Web.UI.WebControls.DataGrid 
Imports System.Web.UI.WebControls.DropDownList 


Partial Public Class LiveEventSearch 
    Inherits System.Web.UI.Page 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 

    End Sub 

    Public Sub ddlState_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) 
     SqlDataSource2.SelectParameters.Clear() 
     SqlDataSource2.SelectParameters.Add(New Parameter("@State", DbType.String, ddlState.SelectedValue)) 
     ddlCity.DataBind() 


    End Sub 

    Protected Sub ddlCity_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlCity.SelectedIndexChanged 

    End Sub 


    Protected Sub Submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit.Click 

    End Sub 
End Class 

Page2.aspx

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" 
     DataSourceID="SqlDataSource1" Style="z-index: 100; left: 324px; position: absolute; 
     top: 226px"> 
     <Columns> 
      <asp:BoundField DataField="Code" HeaderText="Code" ReadOnly="True" SortExpression="R_Code" /> 
      <asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" SortExpression="R_Name" /> 
      <asp:BoundField DataField="Number" HeaderText="Number" ReadOnly="True" SortExpression="RS_Number" /> 
      <asp:BoundField DataField="Addr_1" HeaderText="Addr_1" ReadOnly="True" SortExpression="RS_Addr_1" /> 
      <asp:BoundField DataField="City" HeaderText="City" ReadOnly="True" SortExpression="RS_City" /> 
      <asp:BoundField DataField="State" HeaderText="State" ReadOnly="True" SortExpression="RS_State" /> 
      <asp:BoundField DataField="RS_Zip" HeaderText="RS_Zip" ReadOnly="True" SortExpression="RS_Zip" /> 
     </Columns> 
    </asp:GridView> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbConnection %>" 
     SelectCommand="ListbyCityStSPROC" SelectCommandType="StoredProcedure"> 
     <SelectParameters> 
      <asp:QueryStringParameter Name="City" QueryStringField="RS_City" Type="String" /> 
      <asp:QueryStringParameter Name="State" QueryStringField="ST_Code" Type="String" /> 
     </SelectParameters> 
    </asp:SqlDataSource> 

</asp:Content> 
+0

是你SqlDataSource2至今仍然在頁面調用活着嗎?你不需要將它存儲在會話對象中嗎? – 2011-04-28 13:44:48

+0

@Tony The Tiger,SqlDataSource2僅在page1.aspx上打開。我被告知在這裏使用會話不是一個好主意,但是你有什麼想法? – FluxEngine 2011-04-28 13:55:44

回答

0

有效地在每一頁ASP.NET應用程序就像一個單獨的應用程序本身並不會跨越頁面,因此您需要實現一個保持SQLDataSource的類,以便跨頁面調用它,或者需要實現一些方法在頁面之間複製數據。

你也可以使用FindControl函數來查找所有網頁的SqlDataSource:

SqlDataSource sql = (SqlDataSource)Page.Master.Findcontrol("SqlDataSource1"); 
+0

好的,那麼這段代碼會進入page2.aspx代碼隱藏嗎?我仍然有點不確定它是如何通過的。 – FluxEngine 2011-04-28 14:28:39

+0

@user你真的應該瞭解ASP.NET頁面是如何工作的,但是如果控件在頁面1中定義,則需要在頁面2中使用findcontrol,如果是反向,則需要反向 – 2011-04-28 14:36:44

+1

謝謝您的輸入,非常感謝。 – FluxEngine 2011-04-28 14:53:37