从使用 sqldatasource 作为数据源的下拉列表中计算值

Calculate a value from drop down list that uses sqldatasource as data source

所以基本上我想根据下拉列表中的选定索引进行计算,该列表使用 sqldatasource 作为它的来源并且无法在另一个 page.I 正在使用会话的过程中查看它。

这是使用 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>

代码隐藏

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

我希望有人能回答这个问题,我正在为作业做这个,明天到期。如果你们中的任何人帮助我,那就太好了!

这应该有效

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");
}