find hidden field value when link button is inside nested repeater - asp.net

<asp:Repeater ID="rptHotels" runat="server" OnItemDataBound="rptHotels_ItemDataBound">
<ItemTemplate>
<div class="hotel-box">
<div class="hotel-img">
<asp:HiddenField ID="hdnHotelCode" runat="server" Value='<%#Eval("HotelCode")%>' />
<a class="preview" href='<%#Eval("ImageURL_Text") %>' title='<%#Eval("HotelName")%>' target="_blank">
<img src='<%#Eval("ImageURL_Text") %>' alt='<%#Eval("HotelName")%>' height="75px"
width="100px" />
</a>
</div>
<div class="hotel_heeading_content">
<div class="hotel_heading">
<h2>
<asp:LinkButton ID="lnkHotelDetail" runat="server" OnClick="lnkHotelDetail_Click">
<%#Eval("HotelName")%>
(
<%#Eval("boardType")%>)
</asp:LinkButton>
</h2>
</div>
<div class="stars">
<span class="stars">
<%#Eval("StarRating")%></span>
</div>
<div class="hotel_text">
<%#Eval("HotelAddress")%>,
<%#Eval("Destination")%>
,<%#Eval("Country")%>
<img src="images/ico_point2.png" alt="" id="mapicon" class="mapicon" />
<input type="hidden" id="hdnLatitude" class="hdnLatitude" runat="server" value='<%#Eval("Latitude")%>' />
<input type="hidden" id="hdnLongitude" class="hdnLongitude" runat="server" value='<%#Eval("Longitude")%>' />
<input type="hidden" id="hdnInfoWindow" class="hdnInfoWindow" runat="server" />
</div>
</div>
<p>
<asp:Literal ID="ltDes" runat="server"></asp:Literal>
</p>
<p>
more info
</p>
<div class="btn">
<asp:LinkButton ID="lnkPrice" runat="server" Text=' <%#Eval("totalPrice")%>' OnClick="lnkHotelDetail_Click" ></asp:LinkButton>
</div>
<div class="roominfo">
<asp:Repeater ID="rptRooms" runat="server">
<HeaderTemplate>
<div class="rooms">
<div class="roominfoheader">
<div class="roomheaderlbl">
Room Name</div>
<div class="roomheaderlbl">
Total Room Rate</div>
<div class="roomheaderlbl">
Book Now</div>
</div>
</div>
</HeaderTemplate>
<ItemTemplate>
<div class="rooms">
<div class="roominforow">
<div class="roominforowlbl">
<asp:Label ID="lblRoomName" runat="server" Text='<%#Eval("roomCategory") %>'></asp:Label></div>
<div class="roominforowlbl">
$
<asp:Label ID="Label1" runat="server" Text='<%#Eval("totalRoomRate") %>'></asp:Label></div>
<div class="roominforowlbl">
<asp:LinkButton ID="lnkBookNow" runat="server" Text="Book Now" OnClick="lnkBookNow_Click"></asp:LinkButton></div>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
I have above HTML for nested repeater .I am able to find the hidden field value which contain hotel Code by following method
protected void lnkHotelDetail_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
var item = (RepeaterItem)btn.NamingContainer;
HiddenField hdnHotelCode = (HiddenField)item.FindControl("hdnHotelCode");
}
but the problem is now i have to find the hidden field value when a nested repeater item template link button is clicked .You can check that lnkBookNow is link button which is inside the rptRooms repeater.
protected void lnkBookNow_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
var item = (RepeaterItem)btn.NamingContainer;
HiddenField hdnHotelCode = (HiddenField)item.FindControl("hdnHotelCode");
}
I tried something like this but its not finding hidden field.

The problem here is that lnkBookNow.NamingContainer is rptRooms. This control obviously does not contain hdnHotelCode.
I think you should be able to do this with:
protected void lnkBookNow_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
var item = (RepeaterItem)btn.NamingContainer.NamingContainer.NamingContainer;
HiddenField hdnHotelCode = item.FindControl("hdnHotelCode") as HiddenField;
}
btn.NamingContainer is a RepeaterItem in rptRooms. The NamingContainer of that is the Repeater itself. Finally, the NamingContainer of rptRooms is the RepeaterItem of rptHotels, in which you want to find your HiddenField.
Note my use of the as keyword instead of an explicit cast - this will protect you from NullReferenceExceptions if FindControl returns null. Of course, you should explicitly check that hdnHotelCode isn't null before you try to access it.

Related

FindControl not working in Listview Null exception

protected void PassSessionVariable_Click(object sender, EventArgs e)
{
String strLocationID = livTour.FindControl("lblLocationID").ToString();
}
For some reason, the FindControl is getting a null exception. Any particular reasons?
Here is the code for my Listview. The find control is not finding the LocationID label.
<%--Create datasource for ListView for Tour Locations.--%>
<asp:SqlDataSource runat="server" ID="sdsListViewTour"
ConnectionString="<%$ConnectionStrings:2020LJCDT %>"
OldValuesParameterFormatString="original_{0}"
SelectCommand="SELECT LocationID, Location, Image
FROM Location
Order BY City;">
</asp:SqlDataSource>
<%--Listview--%>
<asp:ListView runat="server" ID="livTour"
DataKeyNames="Location"
DataSourceID="sdsListViewTour">
<ItemTemplate>
<div class="container p-1 bg-light">
<asp:LinkButton runat="server" ID="PassSessionVariable" OnClick="PassSessionVariable_Click">
<div class="row border-top border-bottom border-secondary" style="padding-top: 5px; padding-bottom: 5px; padding-left: 20px;">
<asp:Label runat="server" ID="lblLocationID" Text='<%# Eval("LocationID") %>' />
<div class="col text-center" style="margin: auto; color: #2699FB;">
<asp:Label runat="server" CssClass="font-weight-bold" Text='<%# Eval("Location") %>' />
</div>
<div class="col text-center">
<asp:Image runat="server" CssClass="rounded" ImageUrl='<%# "~/Image/Location/" + Eval("Image") %>' />
</div>
</div>
</asp:LinkButton>
</div>
</ItemTemplate>
</asp:ListView>
You can find location label via BindingContainer of sender as follow:
protected void PassSessionVariable_Click(object sender, EventArgs e)
{
var locationLabel = (((Control)sender).BindingContainer.FindControl("lblLocationID") as Label);
String strLocationID = locationLabel.Text;
}
In this case BindingContainer indicates the row of ListItem that you click. So that you can find location label in this row.

How to access the id of a TextBox in the repeater code behind

I have a repeater below;
<asp:Repeater ID="RptRE" runat="server">
<ItemTemplate>
<div id="headingCollapse2" runat="server">
<a data-toggle="collapse" href="#link<%#Eval("IDmessage")%>" aria-expanded="false" aria-controls="link<%#Eval("IDmessage")%>">
<asp:TextBox ID="getid" runat="server"></asp:TextBox>
<div class="media">
<span class="avata">
<img class="media" src="../pics/<%#Eval("Picsender")%>"></span>
</div>
<div class="media">
<h6 class="list"><%#Eval("SubjectMessage")%></h6>
<p class="ltext">
<span><%#Eval("DateSendMessage")%></span>
</p>
</div>
</a>
</div>
<div id="link<%#Eval("IDmessage")%>" role="tabpanel" aria-labelledby="headingCollapse2" class="card-collapse collapse" aria-expanded="false" style="">
<p><%#Eval("ContentMessage")%></p>
</div>
</ItemTemplate>
</asp:Repeater>
In line 5 I have a textbox
<asp:TextBox ID="getid" runat="server"></asp:TextBox>
I can't get the ID of the TextBox code-behind. Is there any specific reason? Does anybody know how to get it?
Thanks
I've made a simple demo with 2 examples of how to get the data out of the Repeater. One example is per row and the other all rows at once. Just make sure you bind data in an IsPostBack check.
<asp:Repeater ID="RptRE" runat="server" OnItemCommand="RptRE_ItemCommand">
<ItemTemplate>
<asp:TextBox ID="getid" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="This Item" CommandName="Show" />
<hr />
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="Button1" runat="server" Text="All Items" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
Code behind
protected void RptRE_ItemCommand(object source, RepeaterCommandEventArgs e)
{
//get the correct textbox from the ItemIndex
var textbox = (TextBox)RptRE.Items[e.Item.ItemIndex].FindControl("getid");
//show result
Label1.Text = textbox.Text;
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in RptRE.Items)
{
var textbox = (TextBox)item.FindControl("getid");
Label1.Text += textbox.Text + "<br>";
}
}

Why is linkbutton OnClick not firing?

This is driving me crackers, it's very basic code, just a very basic three textbox contact form and a linkbutton.
<div class="form-group has-feedback">
<asp:Label ID="lblYourName" AssociatedControlID="txtYourName" CssClass="col-sm-3 control-label" runat="server" Text="Your name"></asp:Label>
<div class="col-sm-6">
<asp:TextBox ID="txtYourName" TextMode="SingleLine" CssClass="form-control" runat="server" placeholder="Your name"></asp:TextBox>
<span class="glyphicon glyphicon-user form-control-feedback"></span>
<asp:Label ID="lblNoName" runat="server" Visible="false" Text="Please enter your name"></asp:Label>
</div>
</div>
<div class="form-group has-feedback">
<asp:Label ID="lblEmail" runat="server" AssociatedControlID="txtEmail" Text="Email" CssClass="col-sm-3 control-label"></asp:Label>
<div class="col-sm-6">
<asp:TextBox ID="txtEmail" CssClass="form-control" runat="server" placeholder="Email" TextMode="Email"></asp:TextBox>
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
<asp:Label ID="lblNoEmail" runat="server" Visible="false" Text="Please enter your email"></asp:Label>
</div>
</div>
<div class="form-group">
<asp:Label ID="lblMessage" AssociatedControlID="txtMessage" CssClass="col-sm-3 control-label" runat="server" Text="Your message"></asp:Label>
<div class="col-sm-6">
<asp:TextBox ID="txtMessage" CssClass="form-control" TextMode="MultiLine" placeholder="Your message" runat="server"></asp:TextBox>
</div>
</div>
<div class="row">
<div class="col-sm-offset-3 col-sm-6">
<asp:LinkButton ID="lnkSubmit" runat="server" OnClick="lnkSubmit_Click" CssClass="btn standard-hover-effect bg-red btn-lg btn-block">
<span class="text">Contact us <i class="fa fa-arrow-right"></i></span>
</asp:LinkButton>
</div>
</div>
The OnClick of the linkbutton points to this simple MailMessage method where it checks name and email boxes are filled in and then sends an email.
protected void lnkSubmit_Click(object sender, EventArgs e)
{
lblNoName.Visible = false;
lblNoEmail.Visible = false;
if (string.IsNullOrEmpty(txtYourName.Text) || string.IsNullOrEmpty(txtEmail.Text))
{
if (!string.IsNullOrEmpty(txtYourName.Text))
{
lblNoName.Visible = false;
}
else
{
lblNoName.Visible = true;
}
if (!string.IsNullOrEmpty(txtEmail.Text))
{
lblNoEmail.Visible = false;
}
else
{
lblNoEmail.Visible = true;
}
}
else
{
MailMessage mm = new MailMessage(txtEmail.Text, "foo#bar.com");
mm.Subject = "Feedback from website";
mm.Body = "Email from " + txtYourName.Text + "<br /><br />" + txtMessage.Text;
SmtpClient smtp = new SmtpClient();
smtp.Host = "mail.websitelive.net";
smtp.Send(mm);
panContactThanks.Visible = true;
}
}
But when I click the submit button, nothing happens. At all. The OnClick doesn't even fire so the breakpoint in the code behind doesn't even get called. It's as if the OnClick even isn't even in the code and it's just a dummy button. What have I missed out?
Please try CauseValidation="false" in link button.
Like:
<asp:LinkButton ID="lnkSubmit" runat="server" OnClick="lnkSubmit_Click"
CauseValidation="false" CssClass="btn standard-hover-effect bg-red btn-lg btn-block">
Wrap your html inside form tag
<form id="someForm" runat="server">
<!--your html-->
</form>
You are using .net server controls, so you need to wrap them inside form. Don't forget to add runat="server"
http://forums.asp.net/t/1463877.aspx?Does+an+aspx+must+have+a+form+tag+
Probably you forgot to add the isPostBack condition in your Page_load
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
//Control initialization here
}
}
Take a look at ASP.NET page life cycle: https://msdn.microsoft.com/en-us/library/ms178472.aspx
IsPostBack: https://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback(v=vs.110).aspx

Find nested repeater in 3 level repeater

I have a 3-level asp.net repeater, and I need to find the 3rd level's items. The code-behind is posted below as well.
Here is my aspx code:
<div class="container">
<asp:Repeater runat="server" ID="rptGrp0" OnItemDataBound="rptGrp0_ItemDataBound">
<HeaderTemplate>
<div id="Grp0" class="rptParent">
</HeaderTemplate>
<ItemTemplate>
<div id="rptParent <%# Eval("Name") %>">
<div class="row-fluid">
<div class="span12">
<h5 class="parentTitle"><%# Eval("Name") %></h5>
</div>
</div>
</div>
<div class="group">
<div id="rptChild <%# Eval("Name") %>">
<div>
<asp:Repeater runat="server" ID="rptGrp1" OnItemDataBound="rptGrp1_ItemDataBound">
<HeaderTemplate>
<div id="Grp1" class="rptChild">
</HeaderTemplate>
<ItemTemplate>
<div class="group">
<div id="rptGrandChildHeader <%# Eval("Name") %>">
<div class="content">
<div class="container">
<div class="row-fluid">
<div class="span12">
<p class="blue">
<asp:Label runat="server" ID="lblChildName" Text='<%# Eval("Name") %>'></asp:Label>
</p>
</div>
</div>
</div>
</div>
</div>
<div id="rptGrandChild <%# Eval("Name") %>">
<asp:Repeater runat="server" ID="rptContent">
<HeaderTemplate>
<div class="c_module">
<div class="container">
<div class="row-fluid">
<div class="span12">
<div class="data_controls">
<div class="clearfix"></div>
</div>
<div class="d_table_module">
<table border="0" class="display" id="tblContent">
<thead>
<tr>
<th>Product</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="width:100px;">
<asp:Label runat="server" ID="lblProduct" Text='<%# Eval("Product") %>'></asp:Label>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</FooterTemplate>
</asp:Repeater>
</div>
</div>
</ItemTemplate>
<FooterTemplate>
</div>
</FooterTemplate>
</asp:Repeater>
</div>
</div>
</div>
</ItemTemplate>
<FooterTemplate>
</div>
</FooterTemplate>
</asp:Repeater>
</div>
Here is my code-behind.
protected void lnkButton_Click(object sender, EventArgs e)
{
Repeater primary = (Repeater)this.FindControl("rptGrp0");
if (primary != null)
{
// Items.Count = 0.
foreach (RepeaterItem item in primary.Items)
{
Repeater secondary = (Repeater)item.FindControl("rptGrp1");
if (secondary != null)
{
foreach (RepeaterItem b in secondary.Items)
{
Repeater target = (Repeater)b.FindControl("rptContent");
if (target != null)
{
foreach (RepeaterItem c in target.Items)
{
}
}
}
}
}
}
}
The problem is that my repeater.Items.Count is 0.
Please advise, thanks.
Repeater will display only if it is bounded with DataSource. Make sure your repeaters are bounded with a proper DataSource.
Finding the items of the 3rd repeater depends on how you databind it to begin with. If you are binding data in your Page_Load event, then the data is bound after the click event is processed. On your lnkButton_Click event the contents of the repeaters have not yet been bound. The easiest way to access those items is to have an Item_DataBound event to access those items.
Sample code:
<asp:Repeater runat="server" ID="rptContent" OnItemDataBound="rptContent_ItemDataBound">
...
</asp:Repeater>
Code Behind:
protected void rptContent_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
e.Item; // This current item
Repeater rpt = (Repeater)(sender);
rpt.Items; // All items
//Do some condition here to test on the item for whatever you want to do
if(e.Item == someCondition)
// some operation here
}
Okay I found the solution. It has to do with the ASP.NET page lifecycle.
After the lnkButton_Click event, a postback occurred, but I needed to "rebind" the repeater datasource on postback, not just initial page_load.
e.g.
protected void Page_Load()
{
if (!IsPostBack)
{
// bind control
}
else
{
// rebind control
}
}

AJAX popup control extender - how do I put on a Cancel button?

Normally with an AJAX popup control extender, you simply select the item and your selection will populate the associated control.
However if I want to populate the control either directly or by a drop down list, then I would like to have a submit button and a cancel button.
I have found how to put in a Submit button. But how do I put in a cancel button?
<asp:TextBox runat="server" ID="txtWeek1MonAMTimeIn" Width="40px" />
<cc1:PopupControlExtender
ID="PopupControlExtenderWeek1TimeIn" runat="server"
PopupControlID="pnlWeek1MonAMTimeIn"
Position="Bottom"
TargetControlID="txtWeek1MonAMTimeIn"
>
</cc1:PopupControlExtender>
<!-- Panel for editing data -->
<asp:UpdatePanel runat="server" ID="UDPWeek1MonAMTimeIn">
<ContentTemplate>
<asp:Panel runat="server" ID="pnlWeek1MonAMTimeIn" CssClass="popupControl"
onprerender="pnlWeek1MonAMTimeIn_PreRender">
<div class="span-7" style="padding:10px;">
<div>
<div class="span-2">
Time In
</div>
<div class="span-5">
<lib:input runat="server" id="libWeek1MonAMTimeIn" DataType="Time" />
</div>
</div>
<div>
<div class="span-2">
Time Out
</div>
<div class="span-5">
<lib:input runat="server" id="libWeek1MonAMTimeOut" DataType="Time" />
</div>
</div>
<div>
<div class="span-2">
Not in
</div>
<div class="span-5">
<asp:DropDownList runat="server" ID="ddlLeaveWeek1MonAM" />
</div>
</div>
<div>
<div class="span-2">
<asp:Button runat="server" ID="btnCancelWeek1MonAMTimeIn" UseSubmitBehavior="false" Text="Cancel" onclick="btnCancelWeek1MonAMTimeIn_Click" />
</div>
<div class="span-5">
<asp:Button runat="server" ID="btnSubmitWeek1MonAMTimeIn" Text="Submit"
UseSubmitBehavior="false" onclick="btnSubmitWeek1MonAMTimeIn_Click" /></div>
</div>
</div>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
protected void btnSubmitWeek1MonAMTimeIn_Click(object sender, EventArgs e)
{
PopupControlExtender.GetProxyForCurrentPopup(this.Page).Commit(string.Empty);
}
use ModalPopupExtender,
it has CancelControlId as property
http://www.asp.net/AJAX/AjaxControlToolkit/Samples/ModalPopup/ModalPopup.aspx

Resources