[英]How to configure the index of a datalist in asp.net ?
我在Visual Studio中開發了一個Web應用程序,該應用程序可以在一頁中顯示數據庫中的房間圖片及其名稱,並在數據列表中每個房間旁邊顯示一個速率按鈕。
然后,當用戶單擊“費率”按鈕時,房間圖片及其名稱應被轉移到費率頁面,但是,
對我來說,發生的事情是,如果我單擊任何按鈕,則僅顯示房價頁面上顯示的第一張房間圖片和名稱:
我認為它與數據列表的索引相對應,但是我不知道如何處理它!
我該怎么解決?
這是代碼
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();
}
}
}
}
您正在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");
}
正如Rahul在評論部分中提到的那樣,您也不應該在每DataList
發帖中都對DataList
進行DataBind
,而應僅對最初的發貼。 因此,請使用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和局部變量(而不是字段)也是一種好習慣。 這樣可以確保即使發生錯誤也可以將其關閉。
您為什么不嘗試使用DataList_ItemCommand
事件重定向到另一個頁面,以單擊相應的項目。
我建議您嘗試以下方法:
LinkButton
而不是Button
將每個房間的PrimaryKey Id存儲到Command Argument
屬性中。 在后面的代碼上,創建DataList_ItemCommand
事件並獲取被單擊的項目的Command Argument,如下所示:
var id = e.CommandArgument;
在同一函數中使用Response.Redirect
,然后像現在一樣在會話中將此ID作為查詢字符串參數傳遞,並在“重定向”頁面上獲取該ID。
您也可以從此鏈接獲取有關DataList_ItemCommand
事件的一些參考: 使用DataList和Querystring將數據傳遞到其他頁面
希望這可以幫助。
放入按鈕控件的CommandName和CommandArgument屬性。 將記錄的主鍵放在CommandArgument中,而不使用按鈕的click事件。 使用按鈕的Command事件。 在這種情況下,您將獲得CommandEventArgs類型的參數。 並且在這種情況下,您可以使用CommandEventArgs參數“ CommandArgument”屬性來獲取記錄的主鍵,然后您可以對該特定記錄執行任何操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.