簡體   English   中英

使用LinkBut​​ton更新數據和標簽

[英]Use LinkButton to Update data and label

我正在嘗試創建一個常見問題解答,用戶可以在該問題上對有用或不有用的主題進行排名,而我正在努力使其正常運行,就像在腦海中將其可視化一樣。

這是當前的外觀(出於某種原因,堆棧不會讓我插入此圖像): https : //dl.dropboxusercontent.com/u/9446763/code/faq.JPG

我希望用戶能夠單擊鏈接“ Helpful”,然后立即更新頁面為數據庫增加價值,並更新文本以顯示增加的內容,例如:“ Helpful(1)”,另一次單擊將為“ Helpful” (2)”等等。

現在,我可以執行sql更新了,但是我唯一的問題是要在頁面上發回郵件后更改數字。 現在,當我單擊“幫助”鏈接按鈕時,鏈接按鈕只是完全消失,剩下“((|無用(0))”

碼:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:DataList ID="DataList1" RepeatColumns="1" CellPadding="5" runat="server">
                        <ItemTemplate>
                            <dl>
                                <dt>
                                    <asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Question") %>'></asp:Label></dt>
                                <dd>
                                    <asp:Label ID="Label2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Answer") %>'></asp:Label>
                                    (<asp:LinkButton ID="Helpful" CommandName='<%# DataBinder.Eval(Container.DataItem, "Helpful") %>'
                                        CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID") %>' OnCommand="Submit_Helpful"
                                        runat="server">
                                        Helpful (<asp:Label ID="HelpfulLbl" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Helpful") %>'></asp:Label>)</asp:LinkButton>
                                    | <a href="#">Not Helpful (0)</a> )</dd>
                            </dl>
                        </ItemTemplate>
                    </asp:DataList>
                 </ContentTemplate>
</asp:UpdatePanel>

背后的代碼:

protected void Submit_Helpful(object sender, CommandEventArgs e)
    {
        int currentamt = Convert.ToInt32(e.CommandName);
        int newamt = currentamt + 1;
        using (SqlConnection conn = new SqlConnection(""))
        {
            SqlCommand cmd = new SqlCommand(@"UPDATE FAQ set Helpful=@f1 where ID = '" + e.CommandArgument + "'", conn);
            conn.Open();
            cmd.Parameters.Add("@f1", SqlDbType.Int).Value = newamt;
            cmd.ExecuteNonQuery();
        }
     }
switch (e.CommandName)
{
    case "Helpful":
        ((sender as LinkButton).FindControl("HelpfulLbl") as Label).Text = "Helpful (" + newamt.ToString() + ")" ;
        break;
    case "Not Helpful":
        // The "Not Helpful" is not part of the LinkButton.
        break;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM