how to find the control OfferID? - asp.net

<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1"
DataKeyNames="OfferID" GroupItemCount="2" >
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table ID="groupPlaceholderContainer" runat="server" border="0" style="">
<tr ID="groupPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style="">
</td>
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<td runat="server" style="">
<div id="wrapper">
<div id="ResImage">
<div id="slideshow">
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval ("Image1") %>' Width="250px" Height="190px" CssClass="active" />
<asp:Image ID="Image5" runat="server" ImageUrl='<%# Eval ("Image2") %>' Width="250px" Height="190px" />
<asp:Image ID="Image4" runat="server" ImageUrl='<%# Eval ("Image3") %>' Width="250px" Height="190px" />
</div>
</div>
<div id="ResDesc">
<asp:Label ID="lblDesc" runat="server" Width="290px" Height="190px" BackColor="White" Text='<%# Eval("Offer") %>'></asp:Label>
</div>
<div id="ResPrice1">
<asp:Label ID="lblValue" runat="server" Text="Value" CssClass="ResValue"></asp:Label>
<asp:Label ID="lblDiscount" runat="server" Text="Discount" CssClass="ResDiscount"></asp:Label>
<asp:Label ID="lblYouPay" runat="server" Text="You Pay" CssClass="ResYouPay"></asp:Label>
<div id="ResPrice2">
<asp:Label ID="lblValueAmt" runat="server" Text='<%# Eval("Value") %>' CssClass="ResValueAmt"></asp:Label>
<asp:Label ID="lblDiscountAmt" runat="server" Text='<%# Eval("Discount") %>' CssClass="ResDiscountAmt"></asp:Label>
<asp:Label ID="lblYouPayAmt" runat="server" Text='<%# Eval("YouPay") %>' CssClass="ResYouPayAmt"></asp:Label>
</div>
<asp:Label ID="lblRestaurantName" runat="server" Text='<%# Eval("RestaurantName") %>'></asp:Label><br />
<asp:LinkButton ID="lnkGetCoupon" runat="server">Get Discount Coupon</asp:LinkButton>
</div>
<div id="HowItWorks">
<asp:Label ID="lblHowItWorks" runat="server" Text="How It Works?" Font-Bold="True" Font-Size="Small" ForeColor="Red"></asp:Label>
<ul>
<li><asp:Label ID="Label3" runat="server" Text="1.Click on the 'Get Discount Coupon' button" Font-Size="10px"></asp:Label></li>
<li><asp:Label ID="Label4" runat="server" Text="2.Get a print of your Voucher and carry it during your visit to the outlet." Font-Size="10px"></asp:Label></li>
<li><asp:Label ID="Label5" runat="server" Text="3.Show your Voucher and pay the amount directly to the merchant. " Font-Size="10px"></asp:Label></li>
</ul>
</div>
<asp:Label ID="OfferID" runat="server" Text='<%# Eval("OfferID") %>' Visible="false"></asp:Label>
</div>
</td>
</ItemTemplate>
How to find the label control with the id=OfferID...how to use findcontrol here??
i want to find the OfferID of the row on which i click...i have a linkbutton lnkGetCoupon..when i click on the link button...i want to pass the OfferID in the query string to the next page.
i am a new user so they do not let me post answer to my own question
heres the answer...
i added CommandArgument='<%# Eval("OfferID") %> to the link button.
<asp:LinkButton ID="lnkGetCoupon" CommandArgument='<%# Eval("OfferID") %>' runat="server">Get Discount Coupon</asp:LinkButton>
and used the ListView1_ItemCommand
Protected Sub ListView1_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ListView1.ItemCommand
Dim offer As String
offer = e.CommandArgument.ToString()
Dim url As String = "~/RestaurantDedicatedPage.aspx?offerID=" + offer
Response.Redirect(url, True)
End Sub

You don't need the Label at all, you can get the OfferID from the DataKeys collection.
First, add a CommandName to your LinkButton:
<asp:LinkButton ID="lnkGetCoupon" runat="server" CommandName="GetCoupon">Get Discount Coupon</asp:LinkButton>
Then use it in the ItemCommand handler:
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "GetCoupon")
{
ListViewDataItem item = (ListViewDataItem)e.Item;
int index = item.DataItemIndex;
string offerID = ((ListView)sender).DataKeys[index]["OfferID"].ToString();
Response.Redirect("yourUrl" + offerID);
}
}

Attach to the ListView.ItemCommand event and within that event you can search on the Item in the ListViewCommandEventArgs to find the control you need to alter.
Update your ListView in your ASPX to hook up the ItemCommand event:
<asp:ListView ... OnItemDataBound="ListView1_ItemCommand">
<ItemTemplate>
...
<asp:LinkButton id="lnkGetCoupon" CommandName="View" CommandArgument="<%# Eval("OfferID") %>" />
...
</ItemTemplate>
</asp:ListView>
The ItemCommand event will be fired when a Button or LinkButton (or some other button-esque control) is clicked. To handle this event, add the following
code to your *.aspx.cs (code-behind) file:
protected void ListView1_ItemDataBound(object sender, ListViewCommandEventArgs e)
{
//Check if the lnkGetCoupon button was clicked.
if (string.Equals("View", e.CommandName))
{
//Get the offerID from the CommandArgument.
int offerID = int.Parse(e.CommandArgument);
//Perform your logic using the offerID
}
}

Related

How to maintain DataList item index across Postbacks?

I'm trying to retain the index of the Datalist item on page reload. I'm trying to do this by storing the index into a Session and then setting the index of DataList object equals to the Session. Here's the HTML code:
<asp:DataList ID="MyList" Width="100%" RepeatDirection="Vertical" runat="server"
OnItemDataBound="MyList_ItemDataBound" OnItemCommand="MyList_ItemCommand">
<ItemTemplate>
<table class="table">
<tr>
<td style="width: 5%;">
<asp:Label ID="lbl_id" runat="server" Text='<%# Eval("id") %>' Visible="false" />
<asp:Label ID="lbl_isread" runat="server" Text='<%# Eval("isread") %>' Visible="false" />
<input type="checkbox" id="chk_vd" runat="server" class="chkitem" />
</td>
<td style="width: 15%;">
<asp:HyperLink ID="usr" runat="server" Text='<%# Eval("from") %>' />
</td>
<td style="width: 62%;">
<asp:LinkButton ID="sub" Text='<%# Eval("subject") %>' CommandName="select" runat="server"></asp:LinkButton>
</td>
<td style="width: 18%;">
<asp:Label ID="dt" runat="server" Text='<%# Eval("Date_Added") %>' />
</td>
</tr>
</table>
</ItemTemplate>
<SelectedItemTemplate>
<table class="table">
<tr>
<td style="width: 5%;">
<asp:Label ID="lbl_id" runat="server" Text='<%# Eval("id") %>' Visible="false" />
<asp:Label ID="lbl_isread" runat="server" Text='<%# Eval("isread") %>' Visible="false" />
<asp:Label ID="lbl_messagetype" runat="server" Text='<%# Eval("messagetype") %>'
Visible="false" />
<asp:Label ID="lbl_groupid" runat="server" Text='<%# Eval("groupid") %>' Visible="false" />
<asp:Label ID="lbl_content_id" runat="server" Text='<%# Eval("content_id") %>' Visible="false" />
<asp:Label ID="lbl_sendertype" runat="server" Text='<%# Eval("sendertype") %>' Visible="false" />
<input type="checkbox" id="chk_vd" runat="server" class="chkitem" />
</td>
<td style="padding: 5px; text-align: left; vertical-align: top; width: 15%;">
<uc1:avator ID="avt" runat="server" UserName='<%# Eval("from") %>' PhotoName="" Width="65"
Height="65" />
<br />
<asp:HyperLink ID="usr" runat="server" Text='<%# Eval("from") %>' />
</td>
<td style="padding: 5px; text-align: left; vertical-align: top; width: 62%;">
<div class="item_pad_4">
<h3 id="s_sub" runat="server">
<%# Eval("subject") %>
</h3>
</div>
<asp:Label ID="msg" runat="server" Text='<%# Eval("body") %>'></asp:Label>
<div class="bx_br_tp item_pad_4">
<div class="btn-group" id="<%# Eval("id") %>">
<asp:LinkButton ID="a" CssClass="btn btn-primary btn-xs" runat="server" />
<asp:LinkButton ID="r" CssClass="btn btn-primary btn-xs" runat="server" />
<%-- <asp:LinkButton ID="lnk_spam" CssClass="btn btn-primary btn-xs" runat="server"
Text="spam" CommandName="spam"></asp:LinkButton>--%>
</div>
</div>
</td>
<td style="padding: 5px; text-align: left; vertical-align: top; width: 18%;">
<asp:Label ID="dt" runat="server" Text='<%# Eval("Date_Added") %>' />
</td>
</tr>
</table>
</SelectedItemTemplate>
</asp:DataList>
On the server side, I'm trying to restore the index of the DataList item as follows:
if (!Page.IsPostBack)
{
if (Session["datalist"] != null)
{
((DataList)Session["datalist"]).SelectedIndex = (int)Session["index"];
}
Here's the function which does data binding :
protected void MyList_ItemCommand(object source, DataListCommandEventArgs e)
{
Session["datalist"] = source;
string cmd = ((LinkButton)e.CommandSource).CommandName;
long messageid = long.Parse(((Label)e.Item.FindControl("lbl_id")).Text);
string username = ((HyperLink)e.Item.FindControl("usr")).Text;
// approve / ignore action related values
long content_id = 0;
long groupid = 0;
int messagetype = 0;
int index = e.Item.ItemIndex;
Session["index"] = index;
index = (int)Session["index"];
switch (cmd)
{
case "select":
((DataList)source).SelectedIndex = e.Item.ItemIndex;
int isread = int.Parse(((Label)e.Item.FindControl("lbl_isread")).Text);
// mark message as read it its unread message
if (isread == 0)
{
isread = 1; // message is read
MailBoxBLL.Update_isRead(messageid, isread, Page.User.Identity.Name);
}
string _cache = "usr_msg_cnt_" + UserName;
if (HttpContext.Current.Cache[_cache] != null)
{
Cache.Remove(_cache);
}
break;
This doesn't work and Datalist item does not get selected when the page reloads. How can I make it work?
Any help is appreciated.
Add this directly in Page Load:-
protected void Page_Load(object sender, EventArgs e)
{
if (Session["datalist"] != null)
((DataList)Session["datalist"]).SelectedIndex = (int)Session["index"];
}
The probelm with your code is that the Page.IsPostBack property will be false in the initial get request and for every page reload (actual postback) it will be true. Thus !Page.IsPostBack will become actually false and your code will never execute.

ImageButton Visibility not getting changed in aspx.cs page

I'm using a gridview to display some data. There is an image button to display a flag. Within the GridView RowDataBound Event i'm changing the visibility of that button. But the visibility didn't get changed. Here is the code:
bool status = true; // getting this via dataset
ImageButton imgTest = (ImageButton)e.Row.FindControl("ImageButton1");
imgTest.Visible = status;
But still visibility is false.
EDIT:
Here is my HTML Code;
<asp:GridView runat="server" ID="gvScheduleView" DataKeyNames="ControlID,IsPrinted, PrintDate, DueDate"
Width="100%" ShowHeader="true" ShowFooter="true" AutoGenerateColumns="false"
OnRowDataBound="gvScheduleView_rowDataBound" Height="100%" CssClass="removePaddingRight">
<Columns>
<asp:TemplateField HeaderText="Type" HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<%# Eval("Type") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="120">
<ItemTemplate>
<%# Eval("Name") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=" Actions" HeaderStyle-HorizontalAlign="Center"
ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<div style="text-align: center;">
<table style="margin-left: 28px;">
<tr style="text-align: center;">
<td style="width: 20px">
<asp:ImageButton ID="ibDelete" runat="server" ImageUrl="~/common/images/delete.gif"
OnClick="ibDelete_click" OnClientClick="return confirm('Are you sure you wish to delete this schedule?');"
CommandArgument='<%# Eval("ControlID") %>' ToolTip="Delete Schedule" />
</td>
<td style="width: 20px">
<asp:ImageButton ID="ibRollback" runat="server" ImageUrl="~/common/images/icons/arrow_undo.png"
OnClick="ibRollback_click" CommandArgument='<%# Eval("ControlID") %>' ToolTip="Rollback print generation"
OnClientClick="return confirm('Are you sure you wish to rollback this schedule?');" />
</td>
<td style="width: 20px">
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/common/images/icons/email_go.png"
OnClick="ibGenerate_click" CommandArgument='<%# Eval("ControlID") %>' />
<asp:Image ImageUrl="~/common/images/icons/cancel.png" ID="imInfo" runat="server"
Visible="false" ToolTip="DUE DATE IS INVALID - Please change to a future date" />
</td>
<td style="width: 20px">
<asp:ImageButton ID="ibEditProps" runat="server" ImageUrl="~/common/images/icons/building_edit.png"
OnClick="ibEditProps_click" CommandArgument='<%# Eval("ControlID") %>' CommandName='<%# Eval("Type") %>'
ToolTip="Edit Properties" />
</td>
</tr>
</table>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<div id="DivBtnImgCopy" runat="server" visible = "<% =ShowHideButton() %>">
<asp:ImageButton ID="ibEditProps" runat="server"ImageUrl="~/common/images/icons/building_edit.png"
OnClick="ibEditProps_click"CommandArgument='<%# Eval("ControlID") %>' CommandName='<%# Eval("Type") %>' ToolTip="Edit Properties" />
</div>
Code Behind
protected bool ShowHideButton()
{
bool bStatus = false;
try
{
if (sCondition == "false")
{
bStatus = false;
}
else if (sCondition == "true")
{
bStatus = true;
}
return bStatus;
}
catch { }
}

ASP.NET delete item in a paged container

I am currently building a CRUD operations on a entity called Payment.
When are click the pages, for instance, page 2, and try to delete the 9th item on page 2,
it will delete the 9th item on page 1 for me.
Here is my code:
protected void btnDelete_Click(object sender, CommandEventArgs e)
{
int paymentId = Convert.ToInt32(e.CommandArgument.ToString());
try
{
using (BillingApplicationDataContext dc = new BillingApplicationDataContext())
{
BLLPayment bllCorporation = new BLLPayment(dc);
bllCorporation.DeletePayment(paymentId);
}
Response.RedirectToRoute("ViewPaymentsRoute");
}
catch
{
Response.RedirectToRoute("ErrorPageRoute", new {
ErrorMsg = "Unable to delete Payment with Id " +
paymentId.ToString() + "." });
}
}
<asp:ListView ID="lstPayment" runat="server" OnPagePropertiesChanging="lstPayment_PagePropertiesChanging"
EnableViewState="false">
<LayoutTemplate>
<fieldset>
<legend>View Payments</legend>
<table cellpadding="0" cellspacing="0">
<tr>
<th>Id</th>
<th>Corporation</th>
<th>Service Contract</th>
<th>Payment Date</th>
<th>Payment Amount</th>
</tr>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</table>
</fieldset>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Id")%>' EnableViewState="false"/></td>
<td><asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CorpName")%>' EnableViewState="false"/></td>
<td><asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ServiceContractName")%>' EnableViewState="false"/></td>
<td><asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PaymentDate")%>' EnableViewState="false" /></td>
<td><asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Amount")%>' EnableViewState="false" /></td>
<td>
<%--<asp:Button runat="server" Text="Detail"
OnCommand="btnDetail_Click"
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Id")%>' EnableViewState="true"/>--%>
<asp:Button ID="btnDelete" Text="Delete" runat="server"
oncommand='btnDelete_Click' CommandArgument='<%# Eval("Id") %>'
OnClientClick="return ConfirmDelete();"/></td>
</tr>
</ItemTemplate>
</asp:ListView>
<asp:DataPager ID="dataPager" runat="server" PagedControlID="lstPayment" EnableViewState="true">
<Fields>
<asp:NumericPagerField ButtonType="Link" />
</Fields>
</asp:DataPager>
<asp:Button runat="server"
ID="btnNew"
oncommand='btnNew_Click'
Text="New"/>
I think there may be something wrong with the page life cycle, but I do not know how to trace.
Why have you commented out
<asp:Button runat="server" Text="Detail"
OnCommand="btnDetail_Click"
**CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Id")%>'** EnableViewState="true"/>
You are trying to retreive the paymentId but you are passing Id as the command argument. I think the line above is absolutely correct.

update gridview field without dataBind

i have a gridview that show comments. i want that if a person likes a comment, press a like button and i increse the sum of likes , but i do not want to connect to database and bind. i want do that using ajax. how can i do that?
here is my code:
<asp:UpdatePanel runat="server" id="UpdatePanel" >
<ContentTemplate>
<asp:GridView ID="grdCommentsForLoginnedUser"
runat="server"
AutoGenerateColumns="False"
Width="100%"
OnRowCommand="grdCommentsForLoginnedUser_RowCommand"
ViewStateMode="Inherit">
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:HiddenField ID="hdnCommentID" runat="server"
Value='<%# Bind("CommentID") %>'/>
<table border="2" dir="rtl">
<tr>
<td rowspan="2" style="background-color: #6699FF" width="200px">
<asp:HyperLink ID="lnkCommenterName"
runat="server"
NavigateUrl='<%# Bind("CommenterProfile") %>'
Text='<%# Bind("CommenterName") %>' />
</td>
<td width="700px">
<br />
<asp:Label ID="lblCommentText" runat="server"
Text='<%# Bind("CommentText") %>' />
<br />
</td>
</tr>
<tr>
<td width="700px">
<asp:Label ID="lblPreviousAcceptOrNonAccept" runat="server"
Text='<%# Bind("PersonPreviousAcceptOrNonAccept") %>' />
<asp:LinkButton ID="lnkBtnRemovePreviousAcceptOrNonAccept"
runat="server"
Text='<%# Bind("RemovePersonPreviousAcceptOrNonAccept") %>'
CommandName="RemovePreviousAcceptOrNonAccept"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
<br />
<asp:LinkButton ID="lnkBtnUpRate" runat="server" Text="Like"
Visible='<%# Bind("isLikeAndUnlikeButtonVisible") %>'
CommandName="LikeComment"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
<br />sum of likes
<asp:Label ID="lblCommentLikes" runat="server"
Text='<%# Bind("CommentLikes") %>' />
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Add a client side onClick javascript to each button. When clicked get the parent td tag and then getElementById("lblCommentLikes") to set the .innerText property of the "lblCommentLikes" span.
+(theTD.getElementById("lblCommentLikes").innerText)+=1;
or
+(theTD.getElementById("lblCommentLikes").innerText)-=1;
Tip: If you are using IE then press F12 to inspect the DOM of a running page

DataList with button linked to a method (Vb.Net)

This my data list:
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1"
RepeatLayout="Flow">
<ItemTemplate>
Titre:
<asp:Label ID="TitreLabel" runat="server" Text='<%# Eval("Titre") %>' />
<br />
Description:
<asp:Label ID="DescriptionLabel" runat="server"
Text='<%# Eval("Description") %>' />
<br />
<asp:Image ID="Image1" runat="server"
ImageUrl='<%# Eval("ID", "Handler.ashx?ID={0}") %>' Width="200" Height="200"/>
<br />
comments:
<asp:Label ID="commentsLabel" runat="server" Text='<%# Eval("comments") %>' />
<br />
Ajouter commentaire
<asp:button ID="btnAjouter" runat="server" Text="Ajouter" />
<br/>
<br/>
</ItemTemplate>
</asp:DataList>
In the Vb.aspx code I create a method:
public Sub updateComments()
.......
End Sub
And I want to add an event to my DataList button and excute the method.
I don't know how to do it correctly.
This is in Vb.net.
Thanks
Frank
You just need to add a CommandName to your button and handle the DataList's ItemCommand.
For example(in ItemTemplate)
<asp:button ID="btnAjouter" CommandName="Ajouter" runat="server" Text="Ajouter" />
In Codebehind:
Sub Item_Command(sender As Object, e As DataListCommandEventArgs)Handles DataList1.ItemCommand
If e.CommandName = "Ajouter"
' do something '
End If
End Sub

Resources