I have a Repeater binded with database data. I need to find out the Product ID binded to Label but am unable to fetch it.
Here is my Aspx page
<asp:Repeater ID="rpProducts" runat="server" OnItemCommand ="add_click" >
<ItemTemplate>
<div style="visibility: hidden">
<asp:Label ID="Label1" runat="server" Text='<%# Eval("id")%>' ></asp:Label>
</div>
<div class="col-sm-4 prdcts">
<h3>
<%# Eval("productName")%></h3>
<div class="col-sm-12 prdctbox">
<span class="AddToCrt">
<div title="Add to Cart">
<%-- <em class="fa fa-plus"></em>--%>
<asp:ImageButton ID="ImageButton1" runat="server" onclick="add_click" Height="22px"
ImageUrl="~/static/uploads/images/1_1-128.png" Width="24px"/>
</div>
</span>
<div class="imgs">
<%# Eval("productDescription")%>
</div>
<%# Eval("listingHTML")%>
<div class="row">
More
</div>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
my codding in c# is
//void rpProducts(object sender, RepeaterItemEventArgs e)
//{
// Label l = (Label)e.Item.FindControl("Label1");
// string s = l.Text;
//}
//protected void add_Click(object sender, RepeaterCommandEventArgs e)
//{
// Label l = (Label)e.Item.FindControl("Label1");
// string s = l.Text;
//}
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
Label l = (Label)e.Item.FindControl("Label1");
string s = l.Text;
}
protected void Button1_Click(object sender, EventArgs e)
{
}
Is there any way to fetch the label value?
You don't need the hidden label at all. You just need to change markup for your button a little bit.
<asp:ImageButton ID="ImageButton1" runat="server" CommandArgument='<%# Eval("id")%>' ... />
And in you codebehind you can reference the id like:
protected void Button1_Click(object sender, EventArgs e)
{
var button = (IButton)sender;
// assuming id is Int32
int id = int.Parse(button.CommandArgument);
}
Your Label1 is inside ItemTemplate so correct way to fetch the control value is
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item|| e.Item.ItemType == ListItemType.AlternatingItem)
{
Label l = (Label)e.Item.FindControl("Label1");
string s = l.Text;
}
}
Related
I've tried to create a datalist with itemcommand function altough it seems that the program doesn't get into the function of the itemcommand for some reason.
aspx
<asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand" DataKeyField="jobID">
<ItemTemplate>
<div class="jobContainer">
<div class="jobDetails">
<span class="jobName"><%# Eval("jobName") %></span><br />
<hr class="style13">
<a class="Details"> <b>Requirments: </b><span ><%# Eval("jobRequirments") %> WPM</span> </a>
<a class="Details"> <b>Salary: </b><span ><%# Eval("jobSalary")%> Shekel per hour</span> </a>
<a class="Details"> <b>City: </b><span ><%# Eval("jobCity")%></span>
<asp:Button ID="Button1" runat="server" CommandName="Details" Text="Show Details" />
</div>
</div>
</ItemTemplate>
</asp:DataList>
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
// getjobs returns a dataset
Service.Service a = new Service.Service();
DataList1.DataSource = a.getjobs();
DataList1.DataBind();
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "Details")
{
Response.Redirect("Home.aspx");
}
}
I suspect you are having an issue with Page.IsPostBack, so make sure that you bind your DataList when Page is not PostBack in Page_Load as following:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Bind your DataList
Service.Service a = new Service.Service();
DataList1.DataSource = a.getjobs();
DataList1.DataBind();
}
}
See more info about Page.IsPostBack
I create a listview, and put button in every row of a table cell. in button click event , how to get the value in table , like Aitline, ArrCity or the index of items.
// in aspx
<asp:ListView ID="ListView1" runat="server">
<LayoutTemplate>
<ul>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<table class="table table-condensed table-striped table-hover">
<th><%#Eval("Airline")%></th>
<th><%#Eval("DepCity")%></th>
<th><%#Eval("DepTime")%></th>
<th><%#Eval("FlyTime")%> 分鐘</th>
<th><%#Eval("ArrTime")%></th>
<th><%#Eval("ArrCity")%></th>
<th class="success"><%#Eval("TotalFare")%></th>
<th><asp:Button type="submit" class="btn btn-primary btn-lg" id="PayButton" runat="server" Text="Pay" OnClick="PayButton_OnClick"/></th>
</table>
</li>
</ItemTemplate>
<EmptyDataTemplate>
<p>Nothing here.</p>
</EmptyDataTemplate>
</asp:ListView>
//in C# click event in PayButton
protected void Page_Load(object sender, EventArgs e)
{
List<Ticket> TicketList = new List<Ticket>();
this.ListView1.DataSource = TicketList;
this.ListView1.DataBind();
}
protected void PayButton_OnClick(object sender, EventArgs e)
{
// how to get table cell value here?
}
You can use the DataKeyNames attribute. For example:
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ID, Name" >
and get the value this way in code-behind:
protected void PayButton_OnClick(object sender, EventArgs e)
{
ListViewItem item = (sender as LinkButton).NamingContainer as ListViewItem;
int id = (int)ListView1.DataKeys[item.DataItemIndex].Values["ID"];
string name = (string)ListView1.DataKeys[item.DataItemIndex].Values["Name"];
}
I am trying to get the text of my label which is inside a repeater, but I keep getting a NullPointerException.
All of the data is coming from database and it is coming correctly.
When I click on the LinkButton, I want to use the Label Text for next bit code.
Aspx page:
<asp:Repeater ID="RepeaterDepartmentParent" runat="server">
<ItemTemplate>
<div id="outerDiv" class="col-lg-3 col-xs-6" runat="server">
<!-- small box -->
<div>
<div class="inner">
<p>
<%# DataBinder.Eval(Container.DataItem, "Department_Namestr")%>
</p>
</div>
<asp:Label ID="lblDepartmentId" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Department_Idint")%>' Visible="true"></asp:Label>
<asp:LinkButton ID="linkChildDepartment" CommandName="Click" runat="server" CssClass="small-box-footer" OnClick="linkChildDepartment_Click">More info<i class="fa fa-arrow-circle-right"></i></asp:LinkButton>
</div>
</div><%--<%-- ./col -->--%>
</ItemTemplate>
</asp:Repeater>
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
DataSet dsParentDepartment = null;
dsParentDepartment = objDepartmentBL.viewDepartmentparent();
RepeaterDepartmentParent.DataSource = dsParentDepartment.Tables[0];
RepeaterDepartmentParent.DataBind();
}
protected void linkChildDepartment_Click(Object sender, EventArgs e)
{
//what to write here??
//i have tried the bellow code but it gives me every data in that loop but i
//want the single data for a link button click.
//foreach (RepeaterItem item in RepeaterDepartmentParent.Items)
// {
// Label myLabel = (Label)item.FindControl("lblDepartmentId");
// myLabel.Text = Id;
//}
//edited code that works properly
LinkButton linkChildDepartment = (LinkButton)sender;
RepeaterItem item = (RepeaterItem)linkChildDepartment.NamingContainer;
Label myLabel = (Label)item.FindControl("lblDepartmentId");
}
How can I correctly reference the Link Button Label Text?
You can use the NamingContainer property to get the reference of the RepeaterItem. From there it's a short way to your label:
protected void linkChildDepartment_Click(Object sender, EventArgs e)
{
LinkButton linkChildDepartment = (LinkButton) sender;
RepeaterItem item = (RepeaterItem) linkChildDepartment.NamingContainer;
Label myLabel = (Label)item.FindControl("lblDepartmentId");
// ...
}
I have a repeater control as listed below. It has a textbox control. When a save button is clicked, I need to get the updated text from the textbox. I have the following code; but it gives me the old value when I take the textbox text.
How can we get the updated text?
Code Behind
protected void Save_Click(object sender, EventArgs e)
{
foreach (RepeaterItem item in repReports.Items )
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem )
{
string updatedEmail = ((TextBox)item.Controls[5]).Text;
string originalEmail = ((HiddenField)item.Controls[7]).Value;
}
}
}
Control Markup
<div class="repeaterTableBorder">
<asp:Repeater ID="repReports" runat="server">
<ItemTemplate>
<div id="repeaterIdentifier" class="repeaterIdentifier">
<div class="reportTitle">
<%# Eval("ReportName") + ":"%>
<asp:HiddenField ID="hdnLastChangeTime" runat="server" Value= '<%# ((DateTime)Eval("RecordSelectionTime")).ToString("MM/dd/yyyy hh:mm:ss.fff tt")%>' />
<asp:HiddenField ID="hdnReportID" runat="server" Value='<%# Eval("ReportTypeCode")%>' />
</div>
<div class="reportFrequency">
<%# " Frequency - Weekly" %>
</div>
</div>
<div class="reportContent">
<div class="repeaterLine">
<asp:TextBox ID="txtEmailRecipients" runat="server" class="textEdit"
Text='<%# Eval("ExistingRecipients") %>'
TextMode="MultiLine"></asp:TextBox>
<asp:HiddenField ID="hdnOriginalRecipients" runat="server" Value='<%# Eval("ExistingRecipients")%>' />
</div>
</div>
</ItemTemplate>
</asp:Repeater>
</div>
I assume that you are binding the Repeater to it's DataSource also on postbacks. You should do that only if(!IsPostBack). Otherwise the values will be overwritten.
protected void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
// databinding code here
}
}
I need to check every row of a datalist on a button click to check if the checkbox inside the each row is checked or not. I put my buttons inside the FooterTemplate of the DataList but I couldn't find a way yet. This is my ItemCommand method;
protected void DataList1_ItemCommand(object sender, DataListCommandEventArgs e) {
if (e.Item.ItemType == ListItemType.Footer) {
if (e.CommandName == "AddContinue") {
} else if (e.CommandName == "SkipContinue") {
}
}
}
here is my footer;
<FooterTemplate>
<div class="top-margin-25">
<div class="left-floathy">
<asp:Button runat="server" ID="btnPreviousStep" Text="<<< Previous Page"
class="blueButtonSmall boxShadow" onclick="btnPreviousStep_Click" />
</div>
<div class="right-floathy">
<asp:Button runat="server" ID="btnAddContinue" Text="Add & Contuniue >>>"
class="blueButtonSmall boxShadow" CommandName="AddContinue" /><br />
</div>
<div class="clarFix"></div>
<div class="right-floathy">
<asp:Button runat="server" ID="btnSkipContinue" Text="Skip & Continue >>>"
class="blueButtonSmall boxShadow" CommandName="SkipContinue" />
</div>
<div class="clarFix"></div>
</div>
</FooterTemplate>
Ok, apparently I was a little careless for not seeing DataList.Items thing. Answer is sitting here;
http://blog.ysatech.com/post/2011/06/03/ASPNET-Get-selected-checkbox-value-in-DataList.aspx
EDIT
For others who has the same problem, here is the code;
protected void DataList1_ItemCommand(object sender, DataListCommandEventArgs e) {
if (e.Item.ItemType == ListItemType.Footer) {
if (e.CommandName == "AddContinue") {
foreach (DataListItem item in DataList1.Items) {
CheckBox extraCheck
= item.FindControl("extraCheck") as CheckBox;
if (extraCheck != null) {
if (extraCheck.Checked) {
Response.Write(item.ItemIndex);
}
}
}
} else if (e.CommandName == "SkipContinue") {
}
}
}