My Button_click event is not firing in datalist - asp.net

Back End(Aspx.CS) This Event is Not Firing
protected void btnCart_Click(object sender, EventArgs e)
{
Button b = sender as Button;
if(b!=null)
{
name = b.CommandArgument;
Response.Redirect("/Cart.aspx");
}
}
Front End(Aspx Code) :
<asp:DataList ID="ItemList" runat="Server" CaptionAlign="Right" RepeatColumns="4" Width="100%" RepeatDirection="Horizontal">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%#Bind("Image")%>' alt='Image Not Visible' Style="height: 200px; width: 200px;" />
<div class="caption">
<h4>
<asp:Label ID="ufname" runat="server" Text='<%#Bind("Name") %>'>l</asp:Label></h4>
<p class="text-success">Rs:<asp:Label ID="ufprice" runat="server" Text='<%#Bind("Money") %>'></asp:Label></p>
<p>
<asp:Button ID="btnCart" CssClass="btn btn-primary" runat="Server" Text="Buy Now" CommandArgument='<%# Eval("Id") %>' OnClick="btnCart_Click"/>
<asp:Button ID="Veiw" runat="server" CssClass="btn btn-danger" Text="Details.." CommandArgument='<%# Eval("Id") %>' OnClick="btnCart_Click" />
</p>
</div>
</ItemTemplate>

Did you set EnableEventValidation to false?
Do you use any kind of UpdatePanel?

Related

opening modalpopup with gridview delete

I am trying to open a modal popup window when user clicks on the delete image button. when I click on the delete image button, I want the user to go to grdShoppingCart_RowDeleting when Ok is clicked. I am not sure what I am doing wrong. Below is my gridview code:
<asp:UpdatePanel ID="updPnl" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:GridView ID="grdShoppingCart" runat="server" AutoGenerateColumns="false" class="ui-responsive table-stroke ss-table ui-search-result-table" DataKeyNames="CartID" AllowPaging="false" PageSize="5" GridLines="None" OnRowDataBound="grdShoppingCart_RowDataBound" OnRowDeleting="grdShoppingCart_RowDeleting" >
<Columns>
<!-- other clumns here-->
<asp:TemplateField ShowHeader="False" HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center" ItemStyle-Width="150px" ControlStyle-CssClass="ss-row" >
<ItemTemplate>
<asp:ImageButton ID="imgbtnDelete" runat="server" ImageUrl="~/Images/delete1.png" ToolTip="Click To Delete" AlternateText="Click To delete" CommandName="Delete"/>
<asp:Panel ID="pnlPopUp" runat="server" Style="display: none" CssClass="modalPopup">
<asp:Panel ID="pnlDragPopUp" runat="server" Style="cursor: move;background-color:#DDDDDD;border:solid 1px Gray;color:Black">
<div>
<p>Are you sure you want to delete this item?</p>
</div>
</asp:Panel>
<div>
<p style="text-align: center;">
<asp:Button ID="OkButton" runat="server" Text="Yes" />
<asp:Button ID="CancelButton" runat="server" Text="Cancel" />
</p>
</div>
</asp:Panel>
<ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender" runat="server"
TargetControlID="imgbtnDelete"
PopupControlID="pnlPopUp"
BackgroundCssClass="modalBackground"
CancelControlID="CancelButton"
DropShadow="true"
PopupDragHandleControlID="pnlDragPopUp" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Code behind:
protected void grdShoppingCart_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int cartId = (int)grdShoppingCart.DataKeys[e.RowIndex]["CartID"];
ShoppingCartData scd = new ShoppingCartData();
scd.DeleteShoppingCartData(cartId);
}
These are the changes that I did to fix the code. I passed the CartID to the code behind
<div >
<p style="display:flex;align-items:center;justify-content:space-between;width:220px;">
<asp:Button ID="OkButton" runat="server" Text="Remove" CommandName="delete" CommandArgument='<%#Eval("CartID") %>'/>
<asp:Button ID="CancelButton" runat="server" Text="Cancel" />
</p>
</div>
and these are the changes I did in code behind:
protected void grdShoppingCart_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// Get current grid view row
GridViewRow row = grdShoppingCart.Rows[e.RowIndex];
// Get command argument from button, also you could use another approach to get parameters
Button btn = (Button)row.FindControl("OkButton");
int cartId = Convert.ToInt32(btn.CommandArgument);
label1.Text = "The row is going to be deleted: " + cartId;
}
that did the trick.

updatepanel for radiobuttonlist

I have a a couple of radiobuttonlists & checkboxlist that is currently set to AutoPostback=true inside an updatepanel. The async postback of updatepanel is working for checkboxlist but not for radiobuttonlists.
This is my code:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="topBar$odsMainProduct" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
<div id="<%# Eval("main_product_id") %>" class="menu"></div>
<br></br>
<br></br>
<br></br>
<div class="divMainProduct">
<h2 style="font-weight: bold; font-stretch: ultra-expanded; margin-bottom: -4px"><%# Eval("name") %></h2>
<div style="margin-bottom: 10px">
<asp:Label ID="lblMainProductDescription" Font-Size="10px" EnableViewState="false" runat="server" Text='<%# Eval("description") %>'></asp:Label>
</div>
</div>
<div class="accordion" id="acc">
<asp:Repeater ID="Repeater2" runat="server" DataSourceID="odsMainProduct0" OnItemDataBound="Repeater2_ItemDataBound">
<ItemTemplate>
<div class="card">
<div class="card-header" id="h<%# Eval("product_id") %>" aria-expanded="false" data-toggle="collapse" data-target="#c<%# Eval("product_id") %>">
<asp:HiddenField ID="hfProductId" runat="server" Value='<%# Eval("product_id") %>' />
<asp:HiddenField ID="hfMainProductId" runat="server" Value='<%# Eval("main_product_id") %>' />
<table style="width: 100%;">
<tr>
<td style="width: 70%">
<asp:Label runat="server" Text='<%# Eval("name") %>' CssClass="linkbutton"></asp:Label>
<br />
<asp:Label runat="server" Text='<%# Eval("description") %>' Font-Size="8px" CssClass="linkbutton"></asp:Label>
</td>
<td style="width: 30%">
<asp:Button ID="btnHeaderSave" runat="server" ForeColor="White" Font-Bold="true" OnClick="btnHeaderSave_Click" BackColor="DarkRed" Font-Size="15px" Width="100px" Height="30px" Text="Button" />
</td>
</tr>
</table>
</div>
<div id="c<%# Eval("product_id") %>" class="collapse" aria-labelledby="h<%# Eval("product_id") %>" data-parent="#acc">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div class="card-body">
<asp:Panel runat="server" CssClass="pnl" GroupingText="Keuze grootte" Visible="false" ID="pnlSizes">
<asp:RadioButtonList ID="rdblSize" AutoPostBack="true" CssClass="checklist" OnSelectedIndexChanged="rdblSize_SelectedIndexChanged" runat="server"></asp:RadioButtonList>
</asp:Panel>
<asp:Panel runat="server" CssClass="pnl" GroupingText="Keuze bijgerecht" Visible="false" ID="pnlExtraOption">
<asp:RadioButtonList ID="rdblExtraOption" CssClass="checklist" runat="server"></asp:RadioButtonList>
</asp:Panel>
<asp:Panel runat="server" CssClass="pnl" GroupingText="Keuze garnering" Visible="false" ID="pnlGarnish">
<asp:CheckBoxList ID="chkGarnish" AutoPostBack="true" CssClass="checklist" OnSelectedIndexChanged="chkGarnish_SelectedIndexChanged" runat="server" RepeatColumns="4"></asp:CheckBoxList>
</asp:Panel>
<asp:Panel runat="server" CssClass="pnl" GroupingText="Keuze saus" Visible="false" ID="pnlSauce">
<asp:RadioButtonList ID="rdblSauce" AutoPostBack="true" CssClass="checklist" runat="server"></asp:RadioButtonList>
</asp:Panel>
<asp:Panel runat="server" CssClass="pnl" GroupingText="Keuze extra saus" Visible="false" ID="pnlExtraSauce">
<asp:RadioButtonList ID="rdblExtraSauce" AutoPostBack="true" CssClass="checklist" runat="server"></asp:RadioButtonList>
</asp:Panel>
<asp:Panel runat="server" CssClass="pnl" GroupingText="Keuze drank" Visible="false" ID="pnlDrink">
<asp:CheckBoxList ID="chklExtraDrink" AutoPostBack="true" CssClass="checklist" runat="server"></asp:CheckBoxList>
</asp:Panel>
<asp:Panel runat="server" CssClass="pnl" GroupingText="Keuze extra's" Visible="false" ID="pnlExtras">
<asp:CheckBoxList ID="chkExtras" CssClass="checklist" runat="server"></asp:CheckBoxList>
</asp:Panel>
<div style="margin-top: 10px; margin-bottom: 5px; text-align: center">
<asp:Button ID="btnSave" runat="server" ForeColor="White" Font-Bold="true" OnClick="btnSave_Click" BackColor="DarkRed" Font-Size="15px" Width="80%" Height="30px" Text="Button" />
<%--<input type="button" id="btnSave2" onclick="getid();" style="color: white; font-weight: bold; background-color: DarkRed; font-size: 15px; width: 100px; height: 30px" value="Button" />--%>
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
<asp:ObjectDataSource ID="odsMainProduct0" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetProductsByMainProductId" TypeName="ProductBLL">
<SelectParameters>
<asp:Parameter Name="mainProductId" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
</ItemTemplate>
</asp:Repeater>
Is this a limitation of UpdatePanel or am I doing something wrong here ?
This is the server-side code for radiobuttonlist rdblSize:
protected void rdblSize_SelectedIndexChanged(object sender, EventArgs e)
{
RepeaterItem row = ((RepeaterItem)((RadioButtonList)sender).NamingContainer);
Button btnSave = (Button)row.FindControl("btnSave");
btnSave.Text = "€" + getPrice(row);
}
protected Decimal getPrice(RepeaterItem ai)
{
HiddenField hfProductId = (HiddenField)ai.FindControl("hfProductId");
RadioButtonList rdbl = (RadioButtonList)ai.FindControl("rdblSize");
Decimal price = 0;
foreach (Product product in productList)
{
if (product.productId == Convert.ToInt32(hfProductId.Value))
{
if (rdbl != null && rdbl.SelectedValue != "")
{
if (product.priceId == Convert.ToInt32(rdbl.SelectedValue))
{
price = product.value;
}
}
else
{
if (product.priceId == Global.nvtPriceId)
{
price = product.value;
}
}
}
}
return price;
}
Seems like you have not set "AutoPostBack=true" for the radiobuttonlist. Set this property and try, should work.
Working for me with below code
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div class="card-body">
<asp:Panel runat="server" CssClass="pnl" GroupingText="Keuze extra saus" Visible="true" ID="pnlExtraSauce">
<asp:RadioButtonList ID="rd" AutoPostBack="true" CssClass="checklist" runat="server"></asp:RadioButtonList>
</asp:Panel>
<asp:Panel runat="server" CssClass="pnl" GroupingText="Keuze extra's" Visible="true" ID="pnlExtras">
<asp:CheckBoxList ID="chk" CssClass="checklist" AutoPostBack="true" runat="server"></asp:CheckBoxList>
</asp:Panel>
<div style="margin-top: 10px; margin-bottom: 5px; text-align: center">
<asp:Button ID="btnSave" runat="server" ForeColor="White" Font-Bold="true" OnClick="btnSave_Click" BackColor="DarkRed" Font-Size="15px" Width="80%" Height="30px" Text="Button" />
</div>
</div>
</ContentTemplate>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
rd.Items.Add(new ListItem("One", "1"));
rd.Items.Add(new ListItem("Two", "2"));
rd.Items.Add(new ListItem("Three", "3"));
chk.Items.Add(new ListItem("One", "1"));
chk.Items.Add(new ListItem("Two", "2"));
chk.Items.Add(new ListItem("Three", "3"));
}
}

ASP.NET Label Inside UpdatePanel Not Updating on Selected Index Changed

I am new to ASP.NET and I'm trying to get a Label to update with some information that is grabbed when I hit On selected index changed. The On selected index changed function is called and returns just fine (I've debugged and stepped through the whole thing). The only thing that doesn't work is where I set the text of the Labels I'm trying to update.
This is the function that gets called on the On selected index changed click:
protected void OnClosetIndexChanged(object sender, EventArgs e)
{
{
UpdatePanel updatePanel1 = Row.FindControl("UpdatePanel1") as UpdatePanel;
Label oldSourceQuantity = (Label)Row.FindControl("lblQuantity");
oldSourceQuantity.Text = "0";//Trying to force a value
updatePanel1.Update();
}
}
I know it goes into the if and tries to set the text but nothing happens on the client side.
This is the UpdatePanel I have:
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="100%" style="background:#F5F5F5" >
<div id="div<%# Eval("componente_id") %>" style="overflow:auto; display:none; position: relative; left: 15px; overflow: auto">
<div class="ExpandTableHeader">
Cambiar la cantidad
</div>
<div class="body">
<label for="validationOfTypeID">Armario:</label>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:DropDownList ID="drCloset" AppendDataBoundItems="True" runat="server" Width="20%" Height="30px" AutoPostBack="true" OnSelectedIndexChanged = "OnClosetIndexChanged"></asp:DropDownList>
<br/>
<label for="validationOfTypeID" visible="false" >cajon</label> <br/>
<asp:DropDownList ID = "drDrawer" AutoPostBack="true" runat="server" Width="20%" Height="30px" >
</asp:DropDownList>
<asp:Label ID="lblQuantity" runat="server" Text=""></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostbackTrigger ControlID="drCloset" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
<label for="validationOfTypeID"></label>
<asp:DropDownList Height="30px" ID="drOperation" runat="server" >
<asp:ListItem>+</asp:ListItem>
<asp:ListItem>-</asp:ListItem>
</asp:DropDownList>
<asp:TextBox width="50px" ID="txtChangeQuantity" runat="server" TextMode="Number" min="0" step="1" Value="0" ></asp:TextBox>
<asp:Label ID="lblTotal" runat="server" Text=""></asp:Label>
<br/>
</br>
<asp:Button class="btn btn-primary" ID="btnConfirmPurchases" runat="server" Text="Validar" AutoPostback="true" width="20%" />
</div>
<asp:DetailsView id="DetailsView1" DataKeyNames="componente_id" Runat="server" Width="300px" Font-Names="Calibri"/>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>

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.

how to find the control OfferID?

<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
}
}

Resources