[英]Can I inject into an ASP.NET MVC controller, some strings (eg. configuration values) using StructureMap?
[英]How can i insert particular comment on particular post? (for eg. facebook) in asp.net using c#
如何在特定帖子中插入特定評論? (如Facebook)我在SQL中有兩個表
當我手動插入Wall ID時,效果很好,但是我希望Wall ID應該自動生成到我輸入評論的帖子中。
在設計部分
<div style="text-align: center">
<asp:TextBox ID="TextBox1" runat="server" Height="61px" TextMode="MultiLine" Width="271px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Height="37px" Text="Submit" Width="91px" OnClick="Button1_Click" />
</div>
<div>
<br />
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<div style="border: solid 2px brown; width: 400px; margin-bottom: 20px">
<span style="">Id:
<asp:Label ID="IdLabel" runat="server" Text='<%# Eval("wallId") %>' />
<br />
post:
<asp:Label ID="postLabel" runat="server" Text='<%# Eval("post") %>' />
<br />
username:
<asp:Label ID="usernameLabel" runat="server" Text='<%# Eval("username") %>' />
<br />
<br />
</span>
<br />
<hr style="height: 3px; background-color: blue" />
<asp:ListView ID="ListView2" runat="server" DataSourceID="SqlDataSource2" DataKeyNames="commentID" InsertItemPosition="LastItem">
<AlternatingItemTemplate>
<span style="">commentID:<asp:Label Text='<%# Eval("commentID") %>' runat="server" ID="commentIDLabel" />
<br />
wallID:<asp:Label Text='<%# Eval("wallID") %>' runat="server" ID="wallIDLabel" />
<br />
comment:<asp:Label Text='<%# Eval("comment") %>' runat="server" ID="commentLabel" />
<br />
<br />
</span>
<br />
<hr style="height: 3px; background-color: blue" />
</AlternatingItemTemplate>
<EditItemTemplate>
<span style="">commentID:<asp:Label Text='<%# Eval("commentID") %>' runat="server" ID="commentIDLabel1" />
<br />
wallID:<asp:TextBox Text='<%# Bind("wallID") %>' runat="server" ID="wallIDTextBox" />
<br />
comment:<asp:TextBox Text='<%# Bind("comment") %>' runat="server" ID="commentTextBox" />
<br />
<asp:Button runat="server" CommandName="Update" Text="Update" ID="UpdateButton" /><asp:Button runat="server" CommandName="Cancel" Text="Cancel" ID="CancelButton" /><br />
<br />
</span>
<br />
<hr style="height: 3px; background-color: blue" />
</EditItemTemplate>
<EmptyDataTemplate>
<span>No data was returned.</span>
</EmptyDataTemplate>
<InsertItemTemplate>
<span style="">wallID:<asp:TextBox Text='<%# Bind("wallID") %>' runat="server" ID="wallIDTextBox" />
<br />
comment:<asp:TextBox Text='<%# Bind("comment") %>' runat="server" ID="commentTextBox" />
<br />
<asp:Button runat="server" CommandName="Insert" Text="Insert" ID="InsertButton" /><asp:Button runat="server" CommandName="Cancel" Text="Clear" ID="CancelButton" /><br />
<br />
</span>
<br />
<hr style="height: 3px; background-color: blue" />
</InsertItemTemplate>
<ItemTemplate>
<span style="">commentID:<asp:Label Text='<%# Eval("commentID") %>' runat="server" ID="commentIDLabel" />
<br />
wallID:<asp:Label Text='<%# Eval("wallID") %>' runat="server" ID="wallIDLabel" />
<br />
comment:<asp:Label Text='<%# Eval("comment") %>' runat="server" ID="commentLabel" />
<br />
<br />
</span>
<br />
<hr style="height: 3px; background-color: blue" />
</ItemTemplate>
<LayoutTemplate>
<div runat="server" id="itemPlaceholderContainer" style=""><span runat="server" id="itemPlaceholder" /></div>
<div style="">
</div>
</LayoutTemplate>
<SelectedItemTemplate>
<span style="">commentID:<asp:Label Text='<%# Eval("commentID") %>' runat="server" ID="commentIDLabel" />
<br />
wallID:<asp:Label Text='<%# Eval("wallID") %>' runat="server" ID="wallIDLabel" />
<br />
comment:<asp:Label Text='<%# Eval("comment") %>' runat="server" ID="commentLabel" />
<br />
<br />
</span>
<br />
<hr style="height: 3px; background-color: blue" />
</SelectedItemTemplate>
</asp:ListView>
<asp:SqlDataSource runat="server" ID="SqlDataSource2" ConnectionString='<%$ ConnectionStrings:ConnectionString %>' SelectCommand="SELECT * FROM [postcomment]" ConflictDetection="CompareAllValues" DeleteCommand="DELETE FROM [postcomment] WHERE [commentID] = @original_commentID AND [wallID] = @original_wallID AND (([comment] = @original_comment) OR ([comment] IS NULL AND @original_comment IS NULL))" InsertCommand="INSERT INTO [postcomment] ([wallID], [comment]) VALUES (@wallID, @comment)" OldValuesParameterFormatString="original_{0}" UpdateCommand="UPDATE [postcomment] SET [wallID] = @wallID, [comment] = @comment WHERE [commentID] = @original_commentID AND [wallID] = @original_wallID AND (([comment] = @original_comment) OR ([comment] IS NULL AND @original_comment IS NULL))">
<DeleteParameters>
<asp:Parameter Name="original_commentID" Type="Int32"></asp:Parameter>
<asp:Parameter Name="original_wallID" Type="Int32"></asp:Parameter>
<asp:Parameter Name="original_comment" Type="String"></asp:Parameter>
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="wallID" Type="Int32"></asp:Parameter>
<asp:Parameter Name="comment" Type="String"></asp:Parameter>
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="wallID" Type="Int32"></asp:Parameter>
<asp:Parameter Name="comment" Type="String"></asp:Parameter>
<asp:Parameter Name="original_commentID" Type="Int32"></asp:Parameter>
<asp:Parameter Name="original_wallID" Type="Int32"></asp:Parameter>
<asp:Parameter Name="original_comment" Type="String"></asp:Parameter>
</UpdateParameters>
</asp:SqlDataSource>
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:ConnectionString %>' SelectCommand="SELECT * FROM [wall]"></asp:SqlDataSource>
</div>
您的架構應如下所示
1. Post -> PostId,WallId,PostedBy, Post,PostedDateTime
2. Comments -> CommentId,PostId,Comments,CommentedBy,CommentedDateTime
Post和Wall之間的關系應為1 Wall and Many Posts,即1:M關系,注釋相似,為1:M。
請執行下列操作:
將參數方向設為InputOutput參數。
<asp:Parameter Name="wallID" Type="Int32" Direction="InputOutput"></asp:Parameter>
那么您可以在Inserted事件上使用:
protected void ListView_Inserted(object sender, SqlDataSourceSelectingEventArgs e)
{
var wallID = e.Command.Parameters["@wallID"].Value ;
}
因為您希望將wallid作為身份值自動插入。 您必須選擇,以便您可以收獲它。 嘗試這個
DECLARE @wallID AS INT = -1
INSERT INTO [postcomment]
([comment])
VALUES (@comment)
select @wallID =SCOPE_IDENTITY();
也不是ListView_Inserted事件,它是SqlDataSource2_Inserted
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.