我开发在Visual Studio中,所有的数据列表显示房间的照片和他们的名字从一个页面数据库和速率按钮每个房间旁边如何在asp.net中配置datalist的索引?
然后Web应用程序,当用户点击率按键房间图片,它的名字应该被转移到速度页面,但
什么跟我的情况是,如果我点击任何按钮,只有第一个房间的图片和房价页面显示的名称:!“(
我认为它对应与datalist的索引,但我不知道如何处理它!!
我应该怎么做才能修复它?
下面是代码
webform1.aspex
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="HotelG.WebForm1" EnableEventValidation="false" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" Width="615px" Height="439px" >
<ItemTemplate>
<table>
<tr>
<td><asp:Image ID="Img1" runat="server" ImageUrl=<%# Eval("Picture")%> Height="100" style="position: relative; top: 0px; left: 98px; width: 100" /> </td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<asp:Label ID="Label1" runat="server" Text=<%# Eval("Room_Type")%>></asp:Label>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td><asp:Button ID="btn1" runat="server" Text="Rate" OnClick="Button1_Click" /></td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
<br />
</div>
</form>
</body>
</html>
WebForm1的代码隐藏文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace HotelG
{
public partial class WebForm1 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename=C:\Users\user\Desktop\database\Golden_Rose.mdf;Integrated Security = True; Connect Timeout = 30");
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
string sel = "select Room_Type , Picture from room_details";
SqlCommand cmd = new SqlCommand(sel, con);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
DataList1.DataSource = dt;
DataList1.DataBind();
con.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (DataListItem li in DataList1.Items)
{
Image img = (Image)li.FindControl("Img1");
Label lbl = (Label)li.FindControl("Label1");
string labeltext = lbl.Text;
string url = img.ImageUrl;
Session["type"] = labeltext;
Session["img"] = url;
Response.Redirect("WebForm2.aspx");
}
}
}
}
webform2代码隐藏文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace HotelG
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["type"] != null)
{
Label1.Text = Session["type"].ToString();
Label5.Text = Session["type"].ToString();
}
if (Session["img"] != null)
{
Image1.ImageUrl = Session["img"].ToString();
Label4.Text = Session["img"].ToString();
}
}
}
}
@TimSchmelter是正确的。另外,你不应该在每个页面加载时绑定'DataList'。考虑在'!IsPostBack'中绑定它。 –
@RahulSingh:在我的回答中提到它。谢谢 –