Repeater Item hides after ItemCommand - c#

Hi I don't know what to title but my problem is that I am showing posts inside a repeater and I have a like button inside it when I press like button, the post hides.
Following is the itemCommand Event.
protected void rptrPosts_ItemCommand(object source, RepeaterCommandEventArgs e)
{
RepeaterItem item = e.Item;
TextBox txtComment = item.FindControl("txtComment") as TextBox;
Label lblID = (Label)item.FindControl("lblID");
LinkButton btnLike = (LinkButton)item.FindControl("btnLike");
int userid = Convert.ToInt32(Session["USERID"]);
int postid = Convert.ToInt32(lblID.Text);
if (e.CommandName == "like")
{
InstagramEntity insta = new InstagramEntity();
int count = (from like in insta.likes where like.like_from == userid & like.like_post== postid select like).Count();
if (count == 0)
{
like lk = new like();
lk.like_from = userid;
lk.like_post = postid;
insta.likes.Add(lk);
insta.SaveChanges();
this.DataBind();
}
}
I also tried rptrposts.DataBind(). PS: I need to referesh the panel after like button is clicked.
Page Load:
if (!IsPostBack)
{
loadpage();
}
LoadPage()
If I remove the (!IsPostBack) condition the item updates, and but doesn't hide but after update, duplicate items are shown.
public void loadpage()
{
InstagramEntity insta = new InstagramEntity();
rptrPosts.DataSource = (from post in insta.posts
join user in insta.users on post.post_by equals user.user_id
orderby post.post_time descending
select new { user.user_name, post.post_time, post.post_image, post.post_caption, post.post_id, post.post_by }).ToList();
rptrPosts.DataBind();
}
Markup:
<asp:Repeater ID="rptrPosts" OnItemCreated="rptrPosts_ItemCreated" OnItemCommand="rptrPosts_ItemCommand" runat="server">
<ItemTemplate>
<div>
<div class="panel panel-default post">
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<div class="panel-heading">
<asp:Label ID="lblID" Visible="false" runat="server" Text='<%# Eval("post_id") %>'></asp:Label>
<img src="Images/homepageimage.jpg" class="img-circle avator" /><a style="margin-left:10px;" href="account.aspx?id=2" ><%# Eval("user_name") %></a><label class="pull-right">Posted On <%# Eval("post_time") %></label>
<asp:HiddenField ID="imagePath" Value='<%# Eval("post_image") %>' runat="server" />
</div>
<div class="panel-body">
<img src='Images/<%# Eval("post_image") %>' class="PostImage img-responsive" />
<div class="well well-sm">
<asp:LinkButton ID="btnLike" CssClass='btn btn-link' CommandName="like" runat="server"><span style="color:red;font-size:x-large" class='<%# getLikeClass(Eval("post_id")) %>'></span></asp:LinkButton><%# getPostlikes(Eval("post_id")) %><br />
<%# Eval("post_caption") %><br />
<ul class="list-group">
<li class="list-group-item"><b>Comments:</b></li>
<asp:LinqDataSource ID="dsComments" runat="server" EnableInsert="true" ContextTypeName="instagram.InstagramEntity" EntityTypeName="" TableName="view_comments" Where="comment_post == #comment_post" OrderBy="comment_time">
<WhereParameters>
<asp:ControlParameter ControlID="lblID" PropertyName="Text" Name="comment_post" Type="Int32"></asp:ControlParameter>
</WhereParameters>
</asp:LinqDataSource>
<asp:Repeater ID="rptrComments" runat="server">
<ItemTemplate>
<li class="list-group-item"><%# Eval("user_name") %> <%# Eval("comment_text") %> </li>
</ItemTemplate>
</asp:Repeater>
</ul>
</div>
</div>
<div class="panel-footer"><asp:LinkButton ID="btnDelete" CssClass="pull-right" Visible='<%# DelVisiblily(Eval("post_by")) %>' CommandName="delete" ToolTip="Delete this Post" runat="server"><span class="glyphicon glyphicon-remove label-danger"></span>Delete</asp:LinkButton> </div>
</div>
</ContentTemplate> </asp:UpdatePanel>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
I am new to asp.net and this is my Semester Project. A help would be nice.

Try loadpage(); instead for this.DataBind(); in rptrPosts_ItemCommand(..);
if (e.CommandName == "like")
{
//code here
if (count == 0)
{
//code here
loadpage();
}
}

Related

How to fix validator from firing on all items in ListView

I am trying to have a validator set up to only fire on the item(car in this instance) that the user wants to add to his or her cart. I am using ListView to display the cars I have for sale. When the user selects the car they want and enters in the quantity, the validators fire for every entry. How would I fix this?
Thank you!!
Cars.apx
<asp:Content ID="Content2" ContentPlaceHolderID="div2" runat="server">
<div class="row">
<asp:ListView ID="ListView1" runat="server" class="custom-class" DataSourceID="SqlDataSource1" OnItemCommand="ListView1_ItemCommand" DataKeyNames="CarID" OnSelectedIndexChanged="ListView1_SelectedIndexChanged">
<ItemTemplate>
<div class="col-sm-4">
<div class="custom_class">
<a href='/Shop/Order/<%# Eval("CarID") %>'>
<img src='/Images/<%# Eval("ImageCar") %>'
alt='<%# Eval("Name") %>' /></a>
<div class="caption">
<h3>
<%# Eval("Name") %>
</h3>
<p>
<b>Price: <%# Eval("CarPrice", "{0:c}") %></b>
<br>
<br>
<%# Eval("Long_Description") %>
</p>
</div>
<div class="row">
<div class="form-group">
<label class="col-sm-1">Quantity:</label>
</div>
</div>
<div class="col-sm-8">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" CssClass="text-danger"
runat="server" ControlToValidate="txtQuantity1" Display="Dynamic"
ErrorMessage="Quantity is a required field."></asp:RequiredFieldValidator>
<asp:RangeValidator ID="RangeValidator1" runat="server" CssClass="text-danger"
ControlToValidate="txtQuantity1" Display="Dynamic"
ErrorMessage="Quantity must range from 0 to 2."
MaximumValue="2" MinimumValue="0" Type="Integer"></asp:RangeValidator>
</div>
<div class="col-sm-3">
<asp:TextBox ID="txtQuantity1" runat="server"
CssClass="form-control"></asp:TextBox>
</div>
<div class="form-group">
<div class="col-sm-12">
<div class="col-sm-12">
<asp:Button ID="btnAdd" runat="server" Text="Add to Cart"
OnClick="btnAdd_Click" CssClass="btn" />
<asp:Button ID="btnCart" runat="server" Text="Go to Cart"
PostBackUrl="~/BuyCars.aspx" CausesValidation="False" CssClass="btn" />
</div>
</div>
</div>
<div>
<asp:Label ID="lblMessage" runat="server" EnableViewState="false"
CssClass="text-info col-sm-12"></asp:Label>
</div>
</div>
</div>
</ItemTemplate>
</asp:ListView>
<div class="col-sm-3">
<asp:TextBox ID="txtQuantity1" runat="server"
CssClass="form-control"></asp:TextBox>
</div>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [CarID], [Name], [Long_Description], [CarPrice], [ImageCar] FROM [Table] ORDER BY [Name]" OnSelecting="SqlDataSource1_Selecting"></asp:SqlDataSource>
</asp:Content>
Cars.apsc.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace CarSales_REAL
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;
if (!IsPostBack)
{
ListView1.DataBind();
}
}
private Product GetSelectedProduct(object id)
{
DataView productsTable = (DataView)
SqlDataSource1.Select(DataSourceSelectArguments.Empty);
//productsTable.RowFilter = "CarID = '" + ListView1.SelectedValue + "'";
DataRowView row = productsTable[1];
Product p = new Product();
p.CarID = row["CarID"].ToString();
p.Name = row["Name"].ToString();
//p.Short_Description = row["Short_Description"].ToString();
p.Long_description = row["long_Description"].ToString();
p.CarPrice = (decimal)row["CarPrice"];
//p.ImageFile = row["ImageFile"].ToString();
return p;
}
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
var product = GetSelectedProduct(ListView1.SelectedValue);
CartItemList cart = CartItemList.GetCart();
CartItem cartItem = cart[product.CarID];
if (cartItem == null)
{
cart.AddItem(product,
Convert.ToInt32(txtQuantity1.Text));
}
else
{
cartItem.AddQuantity(Convert.ToInt32(txtQuantity1.Text));
}
Response.Redirect("BuyCars.aspx");
}
}
protected void btnCart_Click(object sender, EventArgs e)
{
}
protected void ListView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
}
}
}
The trick is to create a unique ValidationGroup per Item. You can use The ClientID of the ListView for that along with the DataItemIndex. See the snippet for a siple demo.
<asp:ListView ID="ListView1" runat="server">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1"
ValidationGroup='<%# ListView1.ClientID + "_" + Container.DataItemIndex %>'></asp:RequiredFieldValidator>
<br />
<asp:Button ID="Button1" runat="server" Text="Button"
ValidationGroup='<%# ListView1.ClientID + "_" + Container.DataItemIndex %>' />
</ItemTemplate>
</asp:ListView>

How to update values in a ListView with ajax in asp.net web form

I'm using a listview and ajax in an asp.net web form. In part of that form, I display comments, which readers can rate, either positive or negative.
This value isn't updated unless the page is refreshed, is there a way to update the value without the need of refreshing the page?
<asp:ScriptManager ID="ScriptManager" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel" runat="server">
<ContentTemplate>
<asp:ListView ID="ListView1" runat="server" OnItemCommand="ListView1_ItemCommand">
<ItemTemplate>
<div class="row comm_info_bar ">
<div class="col-md-5 RightDisplay"><%# Eval("name") %></div>
<div class="col-md-5 comm_info_date"><%# Eval("date") %></div>
<asp:LinkButton ID="negBtn" class="glyphicon glyphicon-minus voteCommentIcon voteContNeg text-danger smallGlyph" runat="server" CommandName="negative" CommandArgument='<%#Eval("id")%>' />
<asp:Label ID="lblnegative" name="lblnegative" class=" voteNumNeg" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "negative") %>'></asp:Label>
<asp:LinkButton ID="posBtn" class="glyphicon glyphicon-plus voteCommentIcon voteContNeg text-success smallGlyph" runat="server" CommandName="positive" CommandArgument='<%#Eval("id")%>' />
<asp:Label ID="lblpositive" class="voteNumPos" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "positive") %>'></asp:Label>
</div>
<div class="row">
<div class="col-md-12 comments"><%# Eval("text") %></div>
</div>
</ItemTemplate>
</asp:ListView>
</ContentTemplate>
</asp:UpdatePanel>
and in code:
static List<Int64> commentsUser = new List<long>();
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
string name = e.Item.DataItemIndex.ToString();
long commentId = Convert.ToInt64(e.CommandArgument);
ArticleCommentsDataClass ArticleComment = new ArticleCommentsDataClass();
if (e.CommandName == "positive")
{
if (!searchcomments(commentId))
{
ArticleComment.Comments_positive(commentId);
commentsUser.Add(commentId);
}
}
else
{
if (!searchcomments(commentId))
{
ArticleComment.Comments_negative(commentId);
commentsUser.Add(commentId);
}
}
}
Is there anyone who has an idea on how to do this?
This sounds like you are binding (reading the data of your grid) before executing the command. Since the command changes the data, you should rebind the grid or update it in any other way.
Check out this article that talks about when each event is raised

How to hide a column inside a repeater when checkbox is checked?

What I am trying to do is access a column (which has the header title "Add to Shopping List") inside a repeater (areaRepeater) that is inside another repeater (locationRepeater). I want to hide it if a checkbox is checked. However, despite how things are setup, the column is never hidden. I'm not sure what I'm missing here...or maybe I'm going about it the wrong way?
Here is the piece of code that I am trying to use to hide the column in the areaRepeater table. I can hide the submitBtn button successfully, but that button is not inside a repeater.
Sitecore.Data.Fields.CheckboxField checkBox = ProductGroup.Fields["Shopping Disabled"];
if (checkBox.Checked)
{
submitBtn.Visible = false;
Repeater rpt1 = (Repeater)FindControl("locationRepeater");
Response.Write("a ");
foreach (RepeaterItem rep in rpt1.Items)
{
Response.Write("1 ");
Repeater areaRepeater = (Repeater)rep.FindControl("areaRepeater");
foreach (RepeaterItem areaRep in areaRepeater.Items)
{
Response.Write("2 ");
if (showField() == false)
{
Label lbl1 = (Label)areaRep.FindControl("litCol");
CheckBox check = (CheckBox)areaRep.FindControl("LineQuantity");
lbl1.Visible = false;
check.Visible = false;
}
}
}
}
This is the designer code for both repeaters. I tried setting the visibility of the label for the header and the checkbox with a function called showField() but it is never called, even though it does return the correct bool value:
<asp:Repeater ID="locationRepeater" runat="server" OnItemDataBound="SetInner">
<ItemTemplate>
<div class="LocationName">
<%# Eval("SecOpen") %><%# Eval("LocationName")%> <%# Eval("SecClose") %>
</div>
<asp:Repeater ID="areaRepeater" runat="server">
<HeaderTemplate>
<div class="headerRow">
<div class="header">
<div class="thumb"><p></p></div>
<div class="headerField name"><p class="hField">Product</p></div>
<div class="headerField sku"><p class="hField">SKU</p></div>
<div class="headerField size"><p class="hField">Size</p></div>
<div class="headerField case"><p class="hField">Case Pack</p></div>
<div class="headerField use"><p class="hField">Use With</p></div>
<div id="shoppingHeader" class="headerField qty" runat="server"><p class="headerfield qty hField"><asp:Label id="listCol" runat="server" visible='<%# showField() %>' Text="Add To Shopping List" /> </p></div>
</div>
</div>
</HeaderTemplate>
<ItemTemplate>
<asp:placeholder id="LocationAreaHeader" runat="server" visible='<%# (Eval("AreaName").ToString().Length == 0 ? false : true) %>' ><h3> <%# Eval("AreaName") %></h3></asp:placeholder>
<asp:placeholder id="ProductTable" runat="server" visible='<%# (Eval("ProductName").ToString().Length == 0 ? false : true) %>' >
<div class="table">
<div class="row">
<div class="thumb"><%# Eval("Charm") %></div>
<div class="field name"><p class="pField"> <%# Eval("ThumbOpen") %><%# Eval("ProductName") %><%# Eval("ThumbClose") %></p> </div>
<div class="field sku"><p class="pField"> <%# Eval("Sku") %> </p></div>
<div class="field size"><p class="pField"> <%# Eval("Size") %></p></div>
<div class="field case"><p class="pField"> <%# Eval("CasePack") %> </p></div>
<div class="field use"><p class="pField"> <%# Eval("UseWith") %> </p></div>
<div id="shopping" class="field qty" runat="server"><p class="pField"> <asp:checkbox visible='<%# showField() %>' id="LineQuantity" runat="server" /></p></div>
</div>
</div>
<asp:Label id="productID" text='<%# Eval("productID") %>' visible="false" runat="server" />
</asp:placeholder>
<!-- Stored values -->
<asp:Label id="SkuID" runat="server" text='<%# Eval("SkuID") %>' visible="true" />
<asp:Label id="masterSku" runat="server" text='<%# Eval("masterSku") %>' visible="false" />
<asp:Label id="masterName" runat="server" text='<%# Eval("masterName" ) %>' visible="false" />
<asp:Label ID="test" visible="false" runat="server" text='<%# Eval("AreaID") %>' />
</ItemTemplate>
</asp:Repeater>
<asp:Label ID="refID" visible="false" runat="server" text='<%# Eval("LocationID") %>' />
</ItemTemplate>
</asp:Repeater>
This is the showField() function:
protected bool showField()
{
bool retVal = true;
Item CurrentItem = Sitecore.Context.Item;
Item HomeItem = ScHelper.FindAncestor(CurrentItem, "Market");
if (HomeItem != null)
{
Item ProductGroup = HomeItem.Axes.SelectSingleItem(#"child::*[##templatename='MarketOfficeBuildigProductMap']");
if (ProductGroup != null)
{
Sitecore.Data.Fields.CheckboxField checkBox = ProductGroup.Fields["Shopping Disabled"];//curently returns true
ShoppingDisabled.Value = checkBox.Checked.ToString();
if (checkBox.Checked == true)
{
retVal = false;
}
}
}
return retVal;
}
I think you could approach this from a different angle which could make your code a lot simpler.
First off try not to use the OnDataBound event. I prefer to use the DataBinding event for the specific controls as it localizes the code better and you never have search for controls. I am not exactly sure how your code is working but take a look at how I would implement the two controls you are trying to hide to use the DataBinding event.
e.g. for the Checkbox:
<asp:CheckBox id="LineQuantity" runat="server" OnDataBinding="LineQuantity_DataBinding" />
protected void LineQuantity_DataBinding(object sender, System.EventArgs e)
{
CheckBox chk = (CheckBox)sender;
chk.Checked = (bool)(Eval("SomeField")); // Note you can use Eval here...
chk.Visible = showField();
}
This should help you localize the problem at least. I typically use this method of doing any custom work while databinding. The DataBound event is not great because it happens after it is all done. The OnDataBinding event lets you inject whatever type of logic you want at the time the databinding is occurring.

How to get click event using Jquery when control is in UpdatePanel in asp.net?

I have three div div1,div2div3 containing DataList control in each div. When i click on item in DataList1 then DataList2 will be loaded asynchronously which in UpdatePanel of div2.
Problem:
When i click on control in DataList2 and try to access the Clicked Control Class using jquery it wont fire the Click event. Any idea?
Sample output:
Jquery Script:
$('.imgOpaLevel2').click(function() {
alert("hi");
//not able to get this message when clicked on DataList2 in div2 which is in updatepanel
}
Here is the ASP.NET Markup:
<div id="divLevel1" class="banner-sec">
<div class="banner-bg-strip">
<asp:DataList ID="dtDevice" runat="server" DataKeyField="DeviceID" DataSourceID="sqlDeviceList"
RepeatColumns="5" RepeatDirection="Horizontal" ItemStyle-VerticalAlign="Top"
OnItemCreated="dtDevice_ItemCreated" OnItemCommand="dtDevice_ItemCommand">
<ItemTemplate>
<asp:HiddenField ID="hdnDeviceID" runat="server" Value='<%# Eval("TronixDeviceID") %>' />
<ul class="unstyled clearfix iphone-banner-img-sec">
<li><a href="#url">
<div class="banner-img-sec">
<asp:ImageButton ID="ImgDeviceImage" ImageUrl='<%# Eval("DeviceImage") %>' CommandName="CategoryID" CommandArgument='<%# Eval("CategoryID") %>' class="imgOpaLevel1"
alt="" RowID='<%# Container.ItemIndex + 1 %>' title='<%# Eval("DeviceName") %>'
runat="server" Width="109" Height="196" />
</div>
<div class="banner-pro-img-txt">
<asp:Label ID="lblDisplayText" runat="server" Text='<%# Eval("DeviceName") %>' Width="114"
Height="20" />
</div>
</a></li>
</ul>
</ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="sqlDeviceList" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnString %>"
SelectCommand="p_GetDeviceList" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter DefaultValue="1" Name="CategoryID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</div>
<div class="container position-relative margintop_25">
<div id="divLevel2" class="iphone-tab-main-sec" style="display: none;">
<div id="divtopArrow" class="input-tab-arrow iPhones">
</div>
<div id="fadediv" class="iphone-tab-sec iphone5-2-sec">
<asp:UpdatePanel runat="server" ID="updatePanelLevel2">
<Triggers>
</Triggers>
<ContentTemplate>
<asp:DataList id="dlCarreirList" runat="server" DataKeyField="CarrierID"
RepeatColumns="5" RepeatDirection="Horizontal"
ItemStyle-VerticalAlign="Top" onitemcreated="dlCarrerList_ItemCreated">
<ItemTemplate>
<asp:HiddenField ID="hdnCarrerID" runat="server" Value='<%# Eval("CarrierID") %>' />
<ul class="unstyled clearfix" style="height:164px;">
<li><a href="#url">
<div class="iphone5-2-pro-img">
<asp:ImageButton ID="ImageButton1" ImageUrl='<%# Eval("CarrierImg") %>' class="imgOpaLevel2"
alt="" RowID='<%# Container.ItemIndex + 1 %>' title='<%# Eval("Carrier") %>'
runat="server" />
<div class="iphone-pro-img-txt">
<asp:Label ID="lblDisplayText" runat="server" Text='<%# Eval("Carrier") %>'/>
</div>
</a></li>
</ul>
</ItemTemplate>
</asp:DataList>
<asp:Label ID="lbltest" runat="server" Text="Hello" Width="114" Height="20" Visible="false" />
</ContentTemplate>
</asp:UpdatePanel>
<div id="Level3">
</div>
In .cs file:
protected void dtDevice_ItemCreated(object sender, DataListItemEventArgs e)
{
var control = e.Item.FindControl("ImgDeviceImage");
ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(control);
}
protected void dtDevice_ItemCommand(object source, DataListCommandEventArgs e)
{
lbltest.Text = "hi";
int categoryid = 0;
if (e.CommandName != null)
{
if (e.CommandName == "CategoryID")
{
categoryid = Convert.ToInt32(e.CommandArgument);
GetCarrerMasterList(categoryid);
}
}
}
protected void dlCarrerList_ItemCreated(object sender, DataListItemEventArgs e)
{
var control = e.Item.FindControl("ImageButton1");
ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(control);
}
protected void GetCarrerMasterList(int categoryid)
{
iPhoneBiz Objiphone = new iPhoneBiz();
Objiphone.CarrierID = categoryid;
DataTable dtCarrierList = Objiphone.GetTronixCarrierList();
if (dtCarrierList != null && dtCarrierList.Rows.Count > 0)
{
dlCarreirList.DataSource = dtCarrierList;
dlCarreirList.DataBind();
}
}
Updated:
$(document).on('mouseenter', '.imgOpaLevel2', function() {
alert("iElement ID:"+this);
$('.imgOpaLevel2').stop().animate({ opacity: 1.0 }, 200);
$(this).hover(
function() {
$('.imgOpaLevel2').stop().animate({ opacity: 0.5 }, 200);
$(this).stop().animate({ opacity: 1.0 }, 200);
if (selectedImgIDLevel2 != null) {
$('.imgOpaLevel2').stop().animate({ opacity: 0.5 }, 200);
$("#" + selectedImgIDLevel2).stop().animate({ opacity: 1.0 }, 200);
}
else {
//nothing
}
},
function() {
if (selectedImgIDLevel2 == null || selectedImgIDLevel2 == undefined) {
$('.imgOpaLevel2').stop().animate({ opacity: 1.0 }, 200);
} //this will reset Opacity on Mouseout
})
});
when you dynamicly loading new Controls then you need to bind your click Event as live event with jQuery http://api.jquery.com/live/
like:
$('#divLevel2').on('click', '.imgOpaLevel2', function (e) {
alert("hi");
});

How to refresh a single item inside Repeater control using updatepanel

I'm making a social network website, the problem I'm facing here is that I used an update panel inside the repeater and I want to refresh the contents of the updatepanel when the user clicks on any linkbutton and ofcourse this updatepanel will be repeated foreach (div-post), but when I click on any linkbutton inside the repeater control and the updatepanel it doesn't refresh. How do I force it to refresh ?. Here is the code I'm using ...
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Repeater ID="Repeater1" runat="server"
OnItemCommand="MyButtonCommandEvent">
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
<div class="post">
<asp:HiddenField ID="postID_hidden" runat="server"
Value='<%# Eval("posts_ID") %>' />
<div class="Thumb">
<img src="thumbs/abdo_thumb.jpg"> </img></div>
<span class="user"><%#Eval("poster_name")%>
</span>
<div class="post-body">
<p>
<%#Eval("description")%>
</p>
</div>
<div class="post-options" style=" height:22px; ">
<span class="first"><%#Eval("post_date")%></span>
<ul style="display:inline; list-style-type: none;">
<li>
<div class="tooltip">
Comments
<img class="tool-img" src="Images/comments.png"> : <%#Eval("comment_num") %>
</img>
</div>
</li>
<li>
<div class="tooltip">
<asp:LinkButton ID="like_linkbtn" runat="server" CommandName="Like" OnClick="Like_Click"><%#(Eval("name_like").ToString() == "") ? "Like" : Eval("name_like")%></asp:LinkButton>
<img class="tool-img" src="images/likes.png"> : <%#Eval("like_counter") %>
</img></div>
</li>
<li>
<div class="tooltip">
<asp:LinkButton ID="hate_linkbtn" runat="server" CommandName="Hate"><%#(Eval("name_hate").ToString() == "") ? "Hate" : Eval("name_hate")%></asp:LinkButton>
<img class="tool-img" src="images/hate.png"> : <%#Eval("hate_counter") %>
</img></div>
</li>
</ul>
</div>
<div class="finish">
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:Repeater>
And the server side code
protected void Like_Click(object sender, EventArgs e)
{
LinkButton1.Text = DateTime.Now.ToString();
}
protected void MyButtonCommandEvent(object source, RepeaterCommandEventArgs e)
{
var postID = e.Item.FindControl("postID_hidden") as HiddenField;
NewUser user = new NewUser();
if (e.CommandName == "Like")
{
user.like_post(int.Parse(postID.Value), (int)Session["accountID"]);
}
else
{
if (e.CommandName == "Hate")
{
user.hate_post(int.Parse(postID.Value), (int)Session["accountID"]);
}
}
// repeater_databind();//This will refresh all the contents of the repeater(bad way)
}
So as can see databind is not working for me because I want to refresh only one item in the repeater control and not the whole items, can anyone help me please ?
Edit :
Problem solved by wrapping the whole repeater in another updatepanel and setting the trigger like this
<asp:UpdatePanel ID="UpdatePanel4" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Repeater1" EventName="MyButtonCommandEvent" />
</Triggers>
You should try to set the UpdateMode propery of your update panel to Conditional

Categories