Africa Treasury

Africa Treasury

  • NA
  • 2
  • 1.3k

loop through to find the control and apply to class

Mar 7 2018 4:16 AM
i need to fetch the record from database and in itemdatabound event loop through each item and find the control and apply the class that is in database.
 
If you run this code you will see that users can pick any icon of their choice on modal and it will appear on the datalist, the challenge am facing here is if i refresh the page the result displayed on datalist disappears, how can i fetch the record from database and in itemdatabound event loop through each item and find the control and apply the class that is in database.
  1. protected void Page_Load(object sender, EventArgs e)    
  2.     {  
  3.         if (!this.IsPostBack)    
  4.         {    
  5.             this.Populatebooks();    
  6.     
  7.             string username5 = this.Page.User.Identity.Name;    
  8.             //Path.GetFileName(Request.Url.AbsolutePath);    
  9.             //}    
  10.         }    
  11.     }  
  12.     private void Populatebooks()    
  13.     {  
  14.         string Populatebooks = "GetUserPOSTSEXAMPLE";    
  15.         using (SqlConnection con = new SqlConnection(constring4))    
  16.         {    
  17.             con.Open();    
  18.             using (SqlCommand cmd = new SqlCommand(Populatebooks, con))    
  19.             {    
  20.                 cmd.CommandType = CommandType.StoredProcedure;    
  21.                 // cmd.Parameters.AddWithValue("@Name", Request.QueryString["Id"].ToString());    
  22.                 cmd.Parameters.AddWithValue("@UserName", Session["userName"]);    
  23.                 // cmd.Parameters.AddWithValue("@Id", Id);    
  24.                 //lblpost.Text = TextBixcomment.Text.Replace(Environment.NewLine, "<BR />");    
  25.                 SqlDataAdapter sda = new SqlDataAdapter(cmd);    
  26.                 DataTable dt = new DataTable();    
  27.                 sda.Fill(dt);    
  28.                 ViewState["DataTable"] = dt;    
  29.                 Getpost.DataSource = dt;    
  30.                 Getpost.DataBind();  
  31.             }  
  32.         }  
  33.     }  
  34.     [WebMethod]    
  35.     public static string INSERTUSERSTATUS(string userName, string icon, string postId)    
  36.     {    
  37.         string constr = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;    
  38.         using (SqlConnection con = new SqlConnection(constr))    
  39.         {    
  40.             using (SqlCommand cmd = new SqlCommand("INSERT INTO [UserPICKS](UserName,statuscount,Iconbook,IconCup,IconPen,PostId6) VALUES(@UserName,@statuscount,@Iconbook,@IconCup,@IconPen,@PostId6)"))    
  41.             {    
  42.                 cmd.Connection = con;    
  43.                 cmd.CommandType = CommandType.Text;    
  44.                 cmd.Parameters.AddWithValue("@UserName", userName);    
  45.     
  46.                 cmd.Parameters.AddWithValue("@statuscount""1");    
  47.                 if (icon.ToLower() == "fa fa-book")    
  48.                 {    
  49.                     cmd.Parameters.AddWithValue("@Iconbook""book");    
  50.                     cmd.Parameters.AddWithValue("@IconCup""");    
  51.                     cmd.Parameters.AddWithValue("@IconPen""");    
  52.     
  53.                 }    
  54.                 if (icon.ToLower() == "fa fa-coffee")    
  55.                 {    
  56.                     cmd.Parameters.AddWithValue("@Iconbook""");    
  57.                     cmd.Parameters.AddWithValue("@IconCup""cup");    
  58.                     cmd.Parameters.AddWithValue("@IconPen""");    
  59.                 }    
  60.                 if (icon.ToLower() == "fa fa-pencil")    
  61.                 {    
  62.                     cmd.Parameters.AddWithValue("@Iconbook""");    
  63.                     cmd.Parameters.AddWithValue("@IconCup""");    
  64.                     cmd.Parameters.AddWithValue("@IconPen""pen");    
  65.                 }    
  66.                 cmd.Parameters.AddWithValue("@PostId6", postId);    
  67.                 con.Open();    
  68.                 cmd.ExecuteNonQuery();    
  69.                 con.Close();    
  70.             }    
  71.         }    
  72.         return "posted";    
  73.     }  
  74.  <form id="form1" runat="server">  
  75.         <div class="wrapper">  
  76.             </div>    
  77.           <br /><br />  
  78.           <div class="">    
  79.         <div class="container">    
  80.     
  81.              <asp:DataList ID="Getpost" runat="server" DataKeyName="Id" Font-Bold="True" Width="100%" OnItemDataBound="Getpost_ItemDataBound">    
  82.         <ItemTemplate>    
  83.             <div class="well col-lg-6">    
  84.                 <asp:Label ID="Label1" Text='<%# Eval("UserName") %>' runat="server" />    
  85.                 <div class="" style="margin-top: 0; margin-left: 9px; margin-bottom: 8px">    
  86.                     <asp:Label ID="lblpost" runat="server" Text='<%# Eval("ContentPost").ToString().Length >180 ? Eval("ContentPost").ToString().Substring(0,180)+"...(Read More)" : Eval("ContentPost").ToString()%>'    
  87.                         Font-Bold="False" Font-Strikeout="False" ForeColor="#333333" CssClass="" Font-Names="Arial"    
  88.                         Font-Size="" />    
  89.                 </div>    
  90.                 <asp:Label ID="lbCOMlId" Text='<%#Eval("ID") %>' CssClass="" runat="server" Font-Size="0px"    
  91.                     Visible="False" />    
  92.                 <asp:Label ID="lbCOMlId2" Text='<%#Eval("ID") %>' CssClass="" runat="server" />    
  93.                 <div class="clearfix">    
  94.                 </div>    
  95.                 <asp:LinkButton ID="btnPick" Text="" runat="server">    
  96.                     <asp:Panel ID="Panel1" runat="server">    
  97.                     <asp:Label ID="lblpickdefaultshow" runat="server" Text="" CssClass=" fa fa-shopping-cart" Font-Size="Larger"></asp:Label>    
  98.                     <asp:Label ID="Label688" runat="server" Text="" Font-Size="Larger"></asp:Label>    
  99.                           <asp:Label ID="Label277" runat="server" Text="Pick" Font-Size="Larger"></asp:Label>    
  100.                         </asp:Panel>    
  101.                 </asp:LinkButton>    
  102.             </div>    
  103.         </ItemTemplate>    
  104.     </asp:DataList>    
  105.     <asp:HiddenField ID="hfUserName" runat="server" />    
  106.     <asp:HiddenField ID="hfPostId" runat="server" />       
  107.     <br />  
  108.         <div class="modal fade" id="pickermodal" tabindex="-1" role="dialog" aria-labelledby="pickerModalLabel"    
  109.         aria-hidden="true">    
  110.         <div class="">    
  111.             <div class="modal-dialog">    
  112.                 <div class="modal-content">    
  113.                     <div class="modal-header">    
  114.                         <button type="button" class="close" data-dismiss="modal" aria-label="Close">    
  115.                             <span aria-hidden="true">×</span></button>    
  116.                         <h4 class="modal-title">    
  117.                             Pick Item Icons</h4>    
  118.                     </div>    
  119.                     <div class="modal-body">  
  120.                         ....... <span style="margin-right: 29px">    
  121.                             <asp:LinkButton ID="btnpickbook" Text="" runat="server">    
  122.                                 <asp:Label ID="Label59" runat="server" Text="" Font-Size="42px" CssClass="fa fa-book"></asp:Label>    
  123.                             </asp:LinkButton>    
  124.                             <a href="#" id=""></a></span><span style="margin-right: 29px"><a href="#" id="btnpickcup">    
  125.                                 <asp:Label ID="Label7" runat="server" Text="" Font-Size="42px" CssClass="fa fa-coffee"    
  126.                                     ForeColor="Red"></asp:Label>    
  127.                             </a></span><span style="margin-right: 29px"><a href="#" id="btnpen">    
  128.                                 <asp:Label ID="Label8" runat="server" Text="" Font-Size="42px" CssClass="fa fa-pencil "></asp:Label>    
  129.                             </a></span>    
  130.                     </div>    
  131.                     <div class="modal-footer">    
  132.                     </div>    
  133.                 </div>    
  134.                 <!-- /.modal-content -->    
  135.             </div>    
  136.             <!-- /.modal-dialog -->    
  137.         </div></div>    
  138.         <!-- /.modal -->    
  139. </div>    
  140.               </div>    
  141.   </form>    
  142.     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>    
  143.    <script src="../Script/default%20css/AdminLTE-2.2.0/bootstrapDefault/js/bootstrap.min.js"></script>    
  144.     
  145. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />    
  146.   <script type='text/javascript'>    
  147.         $(function () {    
  148.             var lblPick;    
  149.             var lbliconam;    
  150.             $('[id*=btnPick]').on('click', function () {    
  151.                 lblPick = $(this).closest('tr').find('[id*=lblpickdefaultshow]');    
  152.                 lbliconam = $(this).closest('tr').find('[id*=Label277]');    
  153.                 var username = $(this).closest('tr').find('[id*=Label1]').text();    
  154.                 var postId = $(this).closest('tr').find('[id*=lbCOMlId2]').text();    
  155.                 //var postId = "106";    
  156.                 $('[id*=hfUserName]').val(username);    
  157.                 $('[id*=hfPostId]').val(postId);    
  158.                 $('[id*=pickermodal]').modal('show');    
  159.                 return false;    
  160.             });    
  161.     
  162.             $('[id*=pickermodal] a').click(function () {    
  163.                 var icon = '';    
  164.                 var iconName = '';    
  165.                 var userName = $('[id*=hfUserName]').val();    
  166.                 var postId = $('[id*=hfPostId]').val();    
  167.                 if ($(this).find('[id*=Label59]').length > 0) {    
  168.                     icon = 'fa fa-book';    
  169.                     iconName = 'Book';    
  170.                 }    
  171.                 else if ($(this).find('[id*=Label7]').length > 0) {    
  172.                     icon = 'fa fa-coffee';    
  173.                     iconName = 'Cup';    
  174.                 }    
  175.                 else if ($(this).find('[id*=Label8]').length > 0) {    
  176.                     icon = 'fa fa-pencil';    
  177.                     iconName = 'Pen';    
  178.                 }    
  179.                 $.ajax({    
  180.                     type: "POST",    
  181.                     url: "CS.aspx/INSERTUSERSTATUS",    
  182.                     data: '{icon:"' + icon + '",iconName:"' + iconName + '",userName:"' + userName + '",postId:"' + postId + '"}',    
  183.                     contentType: "application/json; charset=utf-8",    
  184.                     dataType: "json",    
  185.                     success: function (response) {    
  186.                         $('#pickermodal').modal('hide');    
  187.                         $('[id*=hfUserName]').val('');    
  188.                         $('[id*=hfPostId]').val('');    
  189.                         $(lblPick).attr('class', icon);    
  190.                         $(lbliconam).html(iconName);    
  191.     
  192.                     },    
  193.                     failure: function (response) {    
  194.                         alert(response.d);    
  195.                     },    
  196.                     error: function (response) {    
  197.                         alert(response.d);    
  198.                     }    
  199.                 });    
  200.                 return false;    
  201.             });    
  202.             });    
  203.            
  204. </script>    
  205. protected void Getpost_ItemDataBound(object sender, DataListItemEventArgs e)    
  206.     {    
  207. //display the result

Answers (1)