[英]Update Database using Entity Framework having primary key on multiple Columns
[英]How to handle button clicks when using a repeater with a primary key formed by multiple columns
當通過跨越多列的主鍵標識該項目所基於的記錄時,我需要有關如何處理ASP.NET中繼器項目中的按鈕單擊的幫助。
背景:
我正在談論的頁面從數據庫中獲取一個Complaint
,這是一個具有某些屬性的對象,並向用戶顯示其屬性。
此屬性之一是Escalations
,它是System.Collections.Generic.List<Escalation>
對象。
Escalation
對象只是以下SQL表的代碼內表示:
create table [escalations]
(
[complaint_id] int not null,
[escalation_group_id] varchar (50) not null
[escalation_date] datetime not null,
[escalation_text] text not null,
[response_date] datetime null,
[response_text] text null,
constraint [pk_escalations] primary key ([complaint_id], [escalation_date]),
constraint [fk_complaints] foreign key ([complaint_id]) references [complaints].([complaint_id]),
constraint [fk_escalation_groups] foreign key ([escalation_group_id]) references [escalation_groups].([escalation_group_id])
)
為了顯示Escalations
列表,我選擇使用System.Web.UI.WebControls.Repeater
,因為我需要一個復雜的布局,所以我使用了以下代碼(這是實際代碼的簡化版本,以提高可讀性,例如“答復此投訴”部分放置在模式彈出窗口中,其中包含驗證器,依此類推):
<h2>Complaint #<asp:literal id="complaint_id" runat="server" /></h2>
<asp:repeater id="escalations" runat="server">
<itemtemplate>
<h3>Escalation date</h3>
<p><%# DataBinder.Eval(Container.DataItem, "complaint_date", "{0:yyyy-MM-dd}") %></p>
<h3>Complaint text</h3>
<p><%# DataBinder.Eval(Container.DataItem, "complaint_text") %></p>
<asp:panel id="response_panel" visible='<%# DataBinder.Eval(Container.DataItem, "response_date") == null ? false : true %>' runat="server">
<h3>Response date</h3>
<p><%# DataBinder.Eval(Container.DataItem, "response_date", "{0:yyyy-MM-dd}") %></p>
<h3>Response text</h3>
<p><%# DataBinder.Eval(Container.DataItem, "response_text") %></p>
</asp:panel>
<asp:panel id="reply_panel" visible='<%# DataBinder.Eval(Container.DataItem, "response_date") == null ? true : false %>' runat="server">
<h3>Reply to this complaint</h3>
<p><asp:textbox id="complaint_response" textmode="multiline" runat="server" /></p>
<p><asp:button id="send_response" text="Send response" onclick="SendResponse" runat="server" /></p>
</asp:panel>
</itemtemplate>
</asp:repeater>
當用戶單擊send_response
按鈕時,我需要更新相應的Escalation
,而我對如何確定正確的Escalation
感到send_response
。
protected void SendResponse(Object sender, EventArgs e)
{
// ?!?!
}
獲取complaint_id
不是一個大問題,我可以將其保存在ViewState
因為每個Escalation
相同。
如果Escalation
就已經確定建在一列主鍵,我可以指定一個CommandArgument
上send_response
按鈕,但是因為主鍵是由形成complaint_id
和escalation_date
列,這是不可能做到這一點。
我什至在解決方案中都涉及到將字段綁定到諸如System.Web.UI.WebControls.Label
或System.Web.UI.WebControls.Literal
類的控件,但是由於我需要傳遞的字段是DateTime
,所以我不需要知道如何處理。
問題:
在SendResponse
方法中,何時可以識別正確的Escalation
? 如果是,我該怎么辦? 如果這不可行,如何更改顯示數據的方式?
如果有人渴望閱讀真實的源代碼,那么這里是ASPX代碼和C#代碼 ,但是我必須警告您:自定義類和屬性,方法名稱等都是意大利語。
在此先感謝Andrea。
您可以按照描述的那樣使用Button的Command事件和CommandArgument屬性,但是在升級對象上創建一個只讀屬性,該屬性返回聯合主鍵的一些串聯。 將此屬性綁定到按鈕的CommandArgument。
如果您不能或不想創建一個只讀屬性,則可以在后面的代碼中編寫一個簡單的方法來構造連接,然后從Eval方法中調用它,例如
<asp:Button ...
CommandArgument='<%# ConcatenatePrimaryKeys(Eval("complaint_id"), Eval("escalation_date")) %>'
runat="server" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.