在帖子评论显示在所有帖子.. [英] in post comment is displaying in all post..

查看:79
本文介绍了在帖子评论显示在所有帖子..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想像facebook一样制作墙贴,但问题很少。我想对帖子发表评论,以便在评论中显示评论



i我给我的代码和正面代码



我在使用2列表视图来显示帖子然后发表评论。





这是我的aspx页面设计代码



i am tring to make wall post like facebook but there is few issue. whenewere i m comment on post so that comment show in al post

i am giving my backcode and front code

there i am using 2 list view to show post and then comment.


this my aspx page design code

<div class="blog_Div">
    <asp:TextBox ID="TextBox1" placeholder="Write Your Comment.." class="blog"

        runat="server" TextMode="MultiLine" Rows="2"></asp:TextBox><br />


     <telerik:RadSearchBox ID="RadSearchBox1" class="TextBox2" runat="server" Width="400"

        DataSourceID="SqlDataSource1"  DataTextField="name"

            MaxResultCount="2" RenderMode="Lightweight">

            <DropDownSettings  Height="50">
           <HeaderTemplate>
                    <ul>
                        <li class="col1"></li>

                    </ul>
                </HeaderTemplate>
                <ItemTemplate>
                    <ul>
                        <li class="col1">

                            <%# DataBinder.Eval(Container.DataItem, "name") %></li>
                    </ul>
                </ItemTemplate>

            </DropDownSettings>
             </telerik:RadSearchBox>
             <br />
    <asp:Button ID="Button2"  class="Button1" runat="server" Text="Assign" onclick="Button2_Click" />
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label><asp:Label ID="Label2"

        runat="server" Text=""></asp:Label>
</div><br /><br />
<div>

</div>

<div class="postview" >


     <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1"

        ViewStateMode="Disabled"  OnItemDataBound="use"

        >
        <AlternatingItemTemplate><br>
        <div class="postback">
            <span>
            <asp:Label ID="id_Label" runat="server" Text='<%# Eval("id") %>'/>
            <span class="heding_color">Assigned to </span>
            <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>'   />
            <br />
            <br />
            <span class="heding_color">Description</span><br />
            <asp:Label ID="messageLabel" runat="server" Text='<%# Eval("message") %>' />
            <br />

            <br />


            <br />


            <br />
            <span class="heding_color">Posted by : </span>
            <asp:Label ID="asigntask_nameLabel" runat="server"

                Text='<%# Eval("asigntask_name") %>'  />



            <span class="asgndate "><span class="heding_color"> Posted On </span>
            <asp:Label ID="curnt_dateLabel" runat="server"

                Text='<%# Eval("curnt_date") %>'  /></span><br />





           <asp:ListView id="rply" runat="server" DataSourceID="SqlDataSource2">
           <ItemTemplate>
           <ul>
           <li>
           <asp:Label ID="comment_Label" runat="server" Text='<%# Eval("comment") %>' />
           </li>
           </ul>
           </ItemTemplate>

           </asp:ListView>

                <div class="cust_summary replaybtn">Replay</div>

                <div id="cust_details" class="cust_details" style="display:none; ">
                <br />

                <asp:TextBox ID="TextBoxL"    placeholder="Write Your Comment.." class="commentTextArea"  runat="server" TextMode="MultiLine" Rows="2"></asp:TextBox><br /> <br />

                    <asp:Button ID="Button3" CommandName="insert" runat="server" class="btncomment" Text="Post" onclick="Button3_Click" /> </div> <br />

        </span></div>
        </AlternatingItemTemplate>


        <EditItemTemplate>
        <div class="postback">
            <span style="">
            <asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>'/>
             <span class="heding_color">Assigned to </span>
              <asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>'   />
            <br />
            <span class="heding_color">Description</span><br />
            <asp:TextBox ID="messageTextBox" runat="server" Text='<%# Bind("message") %>' />
            <br />
            <br />

            <br />

            <br /><br />
           <span class="heding_color"> Posted by : </span>
              <asp:TextBox ID="asigntask_nameTextBox" runat="server"

                Text='<%# Bind("asigntask_name") %>' />



            <asp:TextBox ID="curnt_dateTextBox" runat="server"

                Text='<%# Bind("curnt_date") %>' />
            <br /><br />

         <asp:ListView id="rply" runat="server" DataSourceID="SqlDataSource2">
           <ItemTemplate>
           <ul>
           <li>
           <asp:Label ID="comment_Label" runat="server" Text='<%# Eval("comment") %>' />
           </li>
           </ul>
           </ItemTemplate>

           </asp:ListView>
            <br />
                <div class="cust_summary replaybtn">Replay</div>

              <br />
                <div id="cust_details" class="cust_details" style="display:none; ">
                <br />

                <asp:TextBox ID="TextBoxL"  EnableViewState="true"   placeholder="Write Your Comment.." class="commentTextArea"  runat="server" TextMode="MultiLine" Rows="2"></asp:TextBox><br /> <br />
                    <asp:Button ID="Button3" CommandName="insert" runat="server" class="btncomment" Text="Post" onclick="Button3_Click" /> </div> <br />

            <asp:Button ID="UpdateButton" runat="server" CommandName="Update"

                Text="Update" />
            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel"

                Text="Cancel" />
            <br />
            <br />
            </span></div>
        </EditItemTemplate>
        <EmptyDataTemplate>


            <span>No data was returned.</span>
        </EmptyDataTemplate>
     <InsertItemTemplate><br>
        <div class="postback">
            <span>
            <asp:Label ID="id_Label" runat="server" Text='<%# Eval("id") %>'/>
             <span class="heding_color">Assigned to </span>
             <asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>'   />
            <br />
            <span class="heding_color">Description</span><br />
            <asp:TextBox ID="messageTextBox" runat="server" Text='<%# Bind("message") %>' />
            <br />
            <br />

            <br />

            <br />



            <br />
            <span class="heding_color"> Posted by : </span>
            <asp:TextBox ID="asigntask_nameTextBox" runat="server"

                Text='<%# Bind("asigntask_name") %>' />

            <asp:TextBox ID="curnt_dateTextBox" runat="server"

                Text='<%# Bind("curnt_date") %>' />
            <br /><br />

           <asp:ListView id="rply" runat="server" DataSourceID="SqlDataSource2">
           <ItemTemplate>
           <ul>
           <li>
           <asp:Label ID="comment_Label" runat="server" Text='<%# Eval("comment") %>' />
           </li>
           </ul>
           </ItemTemplate>

           </asp:ListView>
                <div class="cust_summary replaybtn">Replay</div>

                <div id="cust_details" class="cust_details" style="display:none; ">
                <br />

                <asp:TextBox ID="TextBoxL"    placeholder="Write Your Comment.." class="commentTextArea"  runat="server" TextMode="MultiLine" Rows="2"></asp:TextBox><br /> <br />
                    <asp:Button ID="Button3" CommandName="insert" runat="server" class="btncomment" Text="Post" onclick="Button3_Click" /> </div> <br />

            <asp:Button ID="InsertButton" runat="server" CommandName="Insert"

                Text="Insert" />
            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel"

                Text="Clear" />
            <br />
            <br />
            </span></div>
        </InsertItemTemplate>
    <ItemTemplate><br>
    <div class="postback">
        <span>
        <asp:Label ID="id_Label" runat="server" Text='<%# Eval("id") %>'/>
        <span class="heding_color">Assigned to </span>
         <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>'  />
        <br /><br />
        <span class="heding_color">Description</span><br />
        <asp:Label ID="messageLabel" runat="server" Text='<%# Eval("message") %>'   />
        <br />
        <br />

        <br />

        <br />




        <br />
        <span class="heding_color"> Posted by : </span>
        <asp:Label ID="asigntask_nameLabel" runat="server"

            Text='<%# Eval("asigntask_name") %>' />


        <span class="asgndate"><span class="heding_color"> Posted On </span>
        <asp:Label ID="curnt_dateLabel" runat="server"

            Text='<%# Eval("curnt_date") %>' /></span><br /><br />



        <asp:ListView id="rply" runat="server" DataSourceID="SqlDataSource2">
           <ItemTemplate>
           <ul>
           <li>
           <asp:Label ID="coment_Label" runat="server" Text='<%# Eval("comment") %>' />
           </li>
           </ul>
           </ItemTemplate>

           </asp:ListView>
            <div class="cust_summary replaybtn">Replay</div>

                <div id="cust_details" class="cust_details" style="display:none; "><br />

                <asp:TextBox ID="TextBoxL" Enabled="true"   placeholder="Write Your Comment.." class="commentTextArea"  runat="server" TextMode="MultiLine" Rows="2"></asp:TextBox><br />
                <br />
                    <asp:Button ID="Button3" runat="server" class="btncomment" Text="Post" onclick="Button3_Click" />
                    </div>
        <br />


        </span></div>
    </ItemTemplate>
        <LayoutTemplate>


        <br>
            <div ID="itemPlaceholderContainer" runat="server">


                <span runat="server" id="itemPlaceholder" />
            </div>
            <div style="">
            </div>
        </LayoutTemplate>
        <SelectedItemTemplate>
        <div class="postback">
            <span >
            <asp:Label ID="id_Label" runat="server" Text='<%# Eval("id") %>'/>
            <span class="heding_color">Assigned to </span>
              <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>'   />
            <br />
            <span class="heding_color">Description</span><br />
            <asp:Label ID="messageLabel" runat="server" Text='<%# Eval("message") %>' />
            <br />
            <br />


            <br />

                   <span class="heding_color"> Posted by : </span>

            <asp:Label ID="asigntask_nameLabel" runat="server"

                Text='<%# Eval("asigntask_name") %>' />
            <span class="asgndate"><span class="heding_color">Posted On </span>
            <asp:Label ID="curnt_dateLabel" runat="server"

                Text='<%# Eval("curnt_date") %>' /></span><br /><br />


             <asp:ListView id="rply" runat="server" DataSourceID="SqlDataSource2">
           <ItemTemplate>
           <ul>
           <li>
           <asp:Label ID="comment_Label" runat="server" Text='<%# Eval("comment") %>' />
           </li>
           </ul>
           </ItemTemplate>

           </asp:ListView>

                <div class="cust_summary replaybtn">Replay</div>

                <div id="cust_details" class="cust_details" style="display:none; "><br />

                <asp:TextBox ID="TextBoxL" placeholder="Write Your Comment.." class="commentTextArea"  runat="server" TextMode="MultiLine" Rows="2"></asp:TextBox><br />
                <br />
                    <asp:Button ID="Button3" class="btncomment" runat="server" Text="Post" onclick="Button3_Click" />
                    </div>
            <br />
           <br />

            </span></div>
        </SelectedItemTemplate>


    </asp:ListView>


       <asp:SqlDataSource ID="SqlDataSource1" runat="server"

        ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"

        SelectCommand="SELECT [message], [name], [curnt_date], [asigntask_name],[id] FROM [task] ORDER BY [id] DESC">


    </asp:SqlDataSource>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server"

        ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"





        SelectCommand="select task.id ,rply.name,rply.comment,rply.rpl_date from task join rply on task.id=rply.id where task.id=rply.id  ORDER BY task.id DESC
">
    </asp:SqlDataSource>










string s2 = System.Web.HttpContext.Current.User.Identity.Name;

protected void Page_Load(object sender, EventArgs e)
{
   // string s2 = System.Web.HttpContext.Current.User.Identity.Name;

    Session["name"] = s2;

}

protected void Button2_Click(object sender, EventArgs e)
{
    try
    {
    string s2 = System.Web.HttpContext.Current.User.Identity.Name;

    SqlConnection a = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);



   SqlCommand cmd = new SqlCommand("insert into task(message,name,curnt_date,asigntask_name) values('" + TextBox1.Text + "','" + RadSearchBox1.Text + "','" + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + "','" + s2 + "') ", a);

     a.Open();


       cmd.ExecuteNonQuery();
       a.Close();
       TextBox1.Text = "";
       RadSearchBox1.Text = "";

       this.Response.Redirect("TaskManager.aspx?user="+s2);
   }
    catch (System.Data.SqlClient.SqlException ex)
   {
       Label1.Text = ex.Message.ToString();

   }


}
protected void use(object sender, ListViewItemEventArgs e)
{
    Label id = (Label)e.Item.FindControl("id_Label");
    ViewState["id"] = id.Text;

}


protected void Button3_Click(object sender, EventArgs e)
{
    SqlConnection c = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);



    int d = int.Parse((String)ViewState["id"]);
    var btn = (Button)sender;
   // if (btn.Text == "btnComent")
    //{

        var item = (ListViewItem)btn.NamingContainer;
        // find other controls:
        var btnModify = (TextBox)item.FindControl("TextBoxL");

        try
        {

            string s2 = System.Web.HttpContext.Current.User.Identity.Name;

            SqlCommand cmd = new SqlCommand("insert into rply(id,comment,name,rpl_date)values('"+d+"','" + btnModify.Text + "','" + s2 + "','" + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + "')", c);
            c.Open();
            cmd.ExecuteNonQuery();
            c.Close();
            btnModify.Text = "";
            Response.Redirect("TaskManager.aspx");

            /*
                     SqlConnection c = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);

                          SqlCommand cmd = new SqlCommand();


                          cmd.Connection = c; //assigning connection to command
                          cmd.CommandType = CommandType.Text; //representing type of command

                          cmd.CommandText ="update task set post=@post  where id=@id";


                          cmd.Parameters.AddWithValue("@post", btnModify.Text);
                          cmd.Parameters.AddWithValue("@id", d);
                          c.Open();
                          cmd.ExecuteNonQuery();
                          c.Close();
                          btnModify.Text = "";*/

        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            Label1.Text = ex.Message.ToString();
        }



    }
}







PLZ HELP ME TO GET OUT OF THIS ISSUE




PLZ HELP ME TO GET OUT OF THIS ISSUE

推荐答案

ConnectionStrings:ApplicationServices %>\"

SelectCommand=\"SELECT [message], [name], [curnt_date], [asigntask_name],[id] FROM [task] ORDER BY [id] DESC\">


</asp:SqlDataSource>
<asp:SqlDataSource ID=\"SqlDataSource2\" runat=\"server\"

ConnectionString=\"<%
ConnectionStrings:ApplicationServices %>" SelectCommand="SELECT [message], [name], [curnt_date], [asigntask_name],[id] FROM [task] ORDER BY [id] DESC"> </asp:SqlDataSource> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%


ConnectionStrings:ApplicationServices %>\"





SelectCommand=\"select task.id ,rply.name,rply.comment,rply.rpl_date from task join rply on task.id=rply.id where task.id=rply.id ORDER BY task.id DESC
\"
>
</asp:SqlDataSource>
ConnectionStrings:ApplicationServices %>" SelectCommand="select task.id ,rply.name,rply.comment,rply.rpl_date from task join rply on task.id=rply.id where task.id=rply.id ORDER BY task.id DESC "> </asp:SqlDataSource>










string s2 = System.Web.HttpContext.Current.User.Identity.Name;

protected void Page_Load(object sender, EventArgs e)
{
   // string s2 = System.Web.HttpContext.Current.User.Identity.Name;

    Session["name"] = s2;

}

protected void Button2_Click(object sender, EventArgs e)
{
    try
    {
    string s2 = System.Web.HttpContext.Current.User.Identity.Name;

    SqlConnection a = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);



   SqlCommand cmd = new SqlCommand("insert into task(message,name,curnt_date,asigntask_name) values('" + TextBox1.Text + "','" + RadSearchBox1.Text + "','" + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + "','" + s2 + "') ", a);

     a.Open();


       cmd.ExecuteNonQuery();
       a.Close();
       TextBox1.Text = "";
       RadSearchBox1.Text = "";

       this.Response.Redirect("TaskManager.aspx?user="+s2);
   }
    catch (System.Data.SqlClient.SqlException ex)
   {
       Label1.Text = ex.Message.ToString();

   }


}
protected void use(object sender, ListViewItemEventArgs e)
{
    Label id = (Label)e.Item.FindControl("id_Label");
    ViewState["id"] = id.Text;

}


protected void Button3_Click(object sender, EventArgs e)
{
    SqlConnection c = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);



    int d = int.Parse((String)ViewState["id"]);
    var btn = (Button)sender;
   // if (btn.Text == "btnComent")
    //{

        var item = (ListViewItem)btn.NamingContainer;
        // find other controls:
        var btnModify = (TextBox)item.FindControl("TextBoxL");

        try
        {

            string s2 = System.Web.HttpContext.Current.User.Identity.Name;

            SqlCommand cmd = new SqlCommand("insert into rply(id,comment,name,rpl_date)values('"+d+"','" + btnModify.Text + "','" + s2 + "','" + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + "')", c);
            c.Open();
            cmd.ExecuteNonQuery();
            c.Close();
            btnModify.Text = "";
            Response.Redirect("TaskManager.aspx");

            /*
                     SqlConnection c = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);

                          SqlCommand cmd = new SqlCommand();


                          cmd.Connection = c; //assigning connection to command
                          cmd.CommandType = CommandType.Text; //representing type of command

                          cmd.CommandText ="update task set post=@post  where id=@id";


                          cmd.Parameters.AddWithValue("@post", btnModify.Text);
                          cmd.Parameters.AddWithValue("@id", d);
                          c.Open();
                          cmd.ExecuteNonQuery();
                          c.Close();
                          btnModify.Text = "";*/

        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            Label1.Text = ex.Message.ToString();
        }



    }
}







PLZ HELP ME TO GET OUT OF THIS ISSUE




PLZ HELP ME TO GET OUT OF THIS ISSUE


1. You use onitemdatabound to set the id in your ViewState. OnItemDataBound fires for each record. Therefore, ViewState will always contain the Id of the last task in the list.

2. From your insert query, it seems you are using an identity column in your database for the id of the task. Can you check if it is actually filled in the database?

3. Because if it is not filled correctly, or all tasks get the same id, then all replies will also get the same if, and the join will return all replies for all tasks.



So first check to see if the data in the database is actually correct (all tasks have a unique id, and all replies actually have the right task id stored with them). I have a strong feeling, based on the observations above, that your PK and FK fields are not filled correctly.



UPDATE: Check your select query for the replies:
1. You use onitemdatabound to set the id in your ViewState. OnItemDataBound fires for each record. Therefore, ViewState will always contain the Id of the last task in the list.
2. From your insert query, it seems you are using an identity column in your database for the id of the task. Can you check if it is actually filled in the database?
3. Because if it is not filled correctly, or all tasks get the same id, then all replies will also get the same if, and the join will return all replies for all tasks.

So first check to see if the data in the database is actually correct (all tasks have a unique id, and all replies actually have the right task id stored with them). I have a strong feeling, based on the observations above, that your PK and FK fields are not filled correctly.

UPDATE: Check your select query for the replies:
select task.id ,rply.name,rply.comment,rply.rpl_date from task join rply on task.id=rply.id where task.id=rply.id  ORDER BY task.id DESC



How does that query limit the results at all. It will give you all replies for all tasks.



If you move the sqldatasource2 inside the itemtemplate of the outer listview, you can use a control parameter pointing to id_Label to filter the replies to one task.


How does that query limit the results at all. It will give you all replies for all tasks.

If you move the sqldatasource2 inside the itemtemplate of the outer listview, you can use a control parameter pointing to id_Label to filter the replies to one task.


这篇关于在帖子评论显示在所有帖子..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆