2015-12-02 30 views
1

我开发在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(); 

      } 



     } 
    } 
} 
+0

@TimSchmelter是正确的。另外,你不应该在每个页面加载时绑定'DataList'。考虑在'!IsPostBack'中绑定它。 –

+1

@RahulSingh:在我的回答中提到它。谢谢 –

回答

2

您在重定向一个foreach,您知道Response.Redirect会立即将客户端重定向到新的URL,并在完成后抛出ThreadAbortException

相反,你只需要在当前项,而不是所有使用FindControl(实际上只有第一个因为foreach不完全列举),这是该按钮的NamingContainer

protected void Button1_Click(object sender, EventArgs e) 
{ 
    DataListItem currentItem = (DataListItem)((Button) sender).NamingContainer; 
    Image img = (Image)currentItem.FindControl("Img1"); 
    Label lbl = (Label)currentItem.FindControl("Label1"); 
    string labeltext = lbl.Text; 
    string url = img.ImageUrl; 
    Session["type"] = labeltext; 
    Session["img"] = url; 
    Response.Redirect("WebForm2.aspx"); 
} 

正如拉胡尔提到在评论部分,您也不应在每个帖子后面DataBindDataList,但只在最初的帖子上。因此请使用IsPostBack进行检查:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if(!IsPostBack) 
    { 
     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(); 
    } 
} 

否则,所有更改都会丢失并且不会触发事件。这仅适用于启用ViewState这是默认设置。使用using -statement和一个局部变量作为连接而不是字段也是很好的做法。这可确保即使出现错误,它也会关闭。

+0

我试过你的解决方案,但它给了我这个错误在这一行DataListItem currentItem =((Button)sender).NamingContainer; 错误\t CS0266 \t无法将类型'System.Web.UI.Control'隐式转换为'System.Web.UI.WebControls.DataListItem'。 –

+0

Thaaaaaaaaaanks现在很好,非常感谢你bro:').. –

0

为什么不尝试使用DataList_ItemCommand事件重定向到另一个页面,以查看被点击的相应项目。

这里是我的建议,你应该尝试:

  1. 商店的PrimaryKey ID每个房间的进Command Argument财产使用的LinkButton代替Button
  2. 在后面的代码,创建DataList_ItemCommand事件,获取该被点击喜欢这个项目的命令参数:

    var id = e.CommandArgument;

  3. 使用Response.Redirect在相同的功能,通过这个ID作为查询字符串参数或Session中,就像你现在正在做的那样并在重定向页面上获取它。

您也可以从这个环节DataList_ItemCommand事件有一定的参考:Pass Data to other Page using DataList and Querystring

希望这有助于。

0

放入Button控件的CommandName和CommandArgument属性。把记录的主键放在CommandArgument中,而不是使用按钮的单击事件。使用按钮的Command事件。在那种情况下,你将得到一个CommandEventArgs类型的参数。在这种情况下,您可以通过使用CommandEventArgs参数“CommandArgument”属性获取记录的主键,然后您可以对该特定记录执行任何操作。