Frage

I am trying to create a FAQ where the user could rank the topic either helpful or not helpful and I'm struggling getting this to function like I am visualizing it in my head.

This is how it currently looks (stack won't let me insert this image for some reason): https://dl.dropboxusercontent.com/u/9446763/code/faq.JPG

I want the user to be able to click on the link "Helpful" and then it instantly updates the page adding value to the database and updating the text to show the increase like: "Helpful (1)", another click would be "Helpful (2)" and so on..

Right now I have the sql update working but my only problem is to get the number to change after the postback on the page. Right now when I click on the "Helpful" linkbutton the linkbutton just disappears entirely and I'm left with "( | Not Helpful (0) )"

Code:

<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>

Code Behind:

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();
        }
     }
War es hilfreich?

Lösung

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;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top