Total Pageviews

Saturday 27 February 2016

Datalist and bootstrap modal in asp.net



Pre requests - Use bootstrap CDN links

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

Objective- Add comment from bootstrap modal pop up

Write html outside datalist code

where datalist id- post_datalist

Bootstrap Modal code (we are using a asp hidden field control in modal)
------------------------------
<div class="modal fade" id="add_comment" tabindex="-1" role="dialog"
                aria-labelledby="myModalLabel" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span></button>
                            <h4 class="modal-title" id="H2">Comment</h4>

                        </div>

                        <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                            <ContentTemplate>
                                <div class="modal-body" style="height: 150px;">


                                    <div class="form-group">

                                        <div class="col-xs-12">
                                            <asp:TextBox ID="comment_txt" runat="server" TextMode="MultiLine" Style="width: 100%; min-height: 90px;"></asp:TextBox>
                                            <asp:HiddenField ID="post_id" runat="server" />
                                        </div>
                                    </div>

  </div>
                                <div class="modal-footer">
                                    <asp:Label ID="Label2" Visible="false" runat="server"></asp:Label>
                                    <asp:Button ID="post_comment" runat="server" Text="Post comment" CssClass="btn btn-info" OnClick="post_comment_Click" />
                                    <button class="btn btn-info" data-dismiss="modal" aria-hidden="true">Close</button>
                                </div>

                            </ContentTemplate>
                            <Triggers>
                                <asp:AsyncPostBackTrigger ControlID="post_datalist" EventName="ItemCommand" />
                                <asp:AsyncPostBackTrigger ControlID="post_comment" EventName="Click" />
                            </Triggers>
                        </asp:UpdatePanel>
                    </div>
                </div>
            </div>


Now in code behind inside post_datalist_ItemCommand -- 


    //add_comment
        if ((e.CommandName == "comment") && (e.CommandArgument != null))
        {

            int id = Convert.ToInt32(e.CommandArgument);
            string code = Convert.ToString(id);
            post_id.Value = code;

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(@"<script type='text/javascript'>");
            sb.Append("$('#add_comment').modal('show');");
            sb.Append(@"</script>");
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "CommentModalScript", sb.ToString(), false);

 }




Now when the modal opens you can write the comment and click button- here is the button click function 

    protected void post_comment_Click(object sender, EventArgs e)
    {
        string postid = post_id.Value;

        Int32 post_id_value = Convert.ToInt32(postid);
        comment_save(post_id_value); // here you can call function to save the data
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append(@"<script type='text/javascript'>");

        sb.Append("$('#add_comment').modal('hide');");
        sb.Append(@"</script>");
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "CommentModalScript", sb.ToString(), false);
        BindParentDataList();// bind data /refresh

    }

Friends pending request,confirm request & friends list table in MSSQL

Create Two tables

User(id, Username)

Friends(friend_one ,friend_two,status)

Here status 01 and 2 values references to Pending Friend RequestConfirm Friend Request and You

Add friend

INSERT INTO friends
(friend_one,friend_two) 
VALUES 
(@user_id,@
friend_id);

Confirm friend

UPDATE friends
SET status="1"
WHERE 
(friend_one=@user_id OR friend_two=
@user_id
AND 
(friend_one=@friend_id OR friend_two=@
friend_id);

Checking friend

SELECT 'friend_one','friend_two','status' FROM friends
WHERE
(friend_one=@user_id OR friend_two=@user_id)
AND
(friend_one=@user_id OR friend_two=@user_id)

List friends

SELECT * FROM user U
LEFT JOIN Friends F
ON U.id = F.friend_two
WHERE F.friend_one = 1 and status=1
UNION
SELECT * FROM user U
LEFT JOIN friends F
ON U.id = F.friend_one
WHERE F.friend_two = 1 and status=1