2017-05-14 40 views
0

因此,基本上我想根據選擇的索引在下拉列表中使用sqldatasource進行計算,因爲它的源代碼和麻煩在另一個頁面上查看它。我正在使用會話過程中。從使用sqldatasource作爲數據源的下拉列表中計算值

下面是下拉列表中的正在使用的SqlDataSource編碼: -

<asp:DropDownList ID="DdlMRoom" runat="server" DataSourceID="SqlDataSource2" DataTextField="Room" DataValueField="Price" Height="16px" Width="84px" Visible="False"> 
</asp:DropDownList> 
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:FlightConnectionString %>" SelectCommand="SELECT [Room], [Price] FROM [Meneur Hotel]"></asp:SqlDataSource> 
<asp:DropDownList ID="DdlGRoom" runat="server" DataSourceID="SqlDataSource3" DataTextField="Room" DataValueField="column1" Height="16px" Width="84px" Visible="False"> 
</asp:DropDownList> 
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:FlightConnectionString %>" SelectCommand="SELECT [Room], [Price (RM)] AS column1 FROM [Gardenia Hotel]"></asp:SqlDataSource> 
<asp:DropDownList ID="DdlARoom" runat="server" DataSourceID="SqlDataSource4" DataTextField="Room" DataValueField="column1" Height="16px" Width="84px" Visible="False"> 
</asp:DropDownList> 
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:FlightConnectionString %>" SelectCommand="SELECT [Room], [Price (RM)] AS column1 FROM [Al-Rashid Hotel]"></asp:SqlDataSource> 
<asp:DropDownList ID="DdlPRoom" runat="server" DataSourceID="SqlDataSource5" DataTextField="Room" DataValueField="column1" Height="16px" Width="84px" Visible="False"> 
</asp:DropDownList> 
<asp:SqlDataSource ID="SqlDataSource5" runat="server" ConnectionString="<%$ ConnectionStrings:FlightConnectionString %>" SelectCommand="SELECT [Room], [Price (RM)] AS column1 FROM [Petra Sella Hotel]"></asp:SqlDataSource> 
<asp:DropDownList ID="DdlTORoom" runat="server" DataSourceID="SqlDataSource6" DataTextField="Room" DataValueField="column1" Height="16px" Width="84px" Visible="False"> 
</asp:DropDownList> 
<asp:SqlDataSource ID="SqlDataSource6" runat="server" ConnectionString="<%$ ConnectionStrings:FlightConnectionString %>" SelectCommand="SELECT [Room], [Price (RM)] AS column1 FROM [The Olive Branch Hotel]"></asp:SqlDataSource> 

這裏的例子我的計算中的一個: -

int calculation() 
{ 
    int price=0; 

    if(DdlMRoom.SelectedIndex==1) 
    { 
     int price; 
     price = Convert.ToInt16(DdlMRoom.SelectedValue) * Convert.ToInt16(TxtPax.Text); 
    } 

    return price; 

} 

protected void Button1_Click(object sender, EventArgs e) 
{  
    calculation().ToString(); 
    Session["Price"] = price; 

    Response.Redirect("View.aspx); 
} 

這裏的編碼上View.aspx &的.cs : -

<asp:Label ID="lblPrice" runat="server"></asp:Label> 

code -behind

protected void Page_Load(object sender, EventArgs e) 
{ 
    lblPrice.Text = Session["Price"].ToString(); 
} 

我希望有人能回答這個問題,我這樣做是爲分配和明天到期。如果你們中的任何一位能幫助我,那將會很棒!

+0

你不存儲計算的'()的結果。的ToString()'。我沒有看到這一行'Session [「Price」] = price;'當這個函數的範圍內沒有名爲price的變量時會起作用 – Bowofola

+0

@Bowofola所以這意味着我必須在protected void Button1_Click中分配另一個變量(object sender ,EventArgs e)呢?例如prices = calculate()。ToString();會議[ 「價格」] =價格;這是否意味着可以從sqldatasource計算出一個值?我GOOGLE了,它說使用這個數據視圖的事情,我只是不明白。 – jdope

+0

你一定要這樣做,因爲你現在不應該建立給我。當你試圖運行它時發生了什麼? – Bowofola

回答

0

這應該工作

int calculation() 
{ 
    int price=0; 

    if(DdlMRoom.SelectedIndex==1) 
    { 
     price = Convert.ToInt16(DdlMRoom.SelectedValue) * Convert.ToInt16(TxtPax.Text); 
    } 

    return price; 

} 

protected void Button1_Click(object sender, EventArgs e) 
{  
    string price = calculation().ToString(); 
    Session["Price"] = price; 

    Response.Redirect("View.aspx"); 
} 
相關問題