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"];
}
Related
I am binding an ASP Repeater from List & trying to add paging in the repeater. Each Page would have 5 Rows. when The Page is Run, 5 items are visible fine but when I click on "2" link button, nothing is visible. When I again Click on "1" the items of Page 2 are visible.
I've gone through this to implement Paging.
Here is my aspx Code:
<asp:Repeater ID="rp_Order" runat="server" OnItemDataBound="rp_Order_ItemDataBound">
<HeaderTemplate>
<table class="table table-responsive">
<tr >
<th style="width:20%;">Data</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<h5 style="font-weight:bold; "><%# Eval("NewsSubject") %></h5>
<p><%# Eval("NewsDate") %></p>
<img src="ImageHandler.ashx?newsid=<%# Eval("NewsID") %>" visible="<%#Eval("ImageAttachment")!=null && Eval("ImageAttachment").ToString()!=""? "true": "false" %>" class="images image-responsive myimg" />
<p ><%# System.Web.HttpUtility.HtmlDecode(Eval("NewsDescription").ToString()) %></p>
<iframe src="<%#GetUrl(Eval("youtubeurl").ToString()) %>" width="400" frameborder="0" visible="<%#Eval("youtubeurl")!=null? "true": "false" %>'"></iframe>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
<ItemTemplate>
<asp:LinkButton ID="lnkPage"
Style="padding: 8px; margin: 2px; background: lightgray; border: solid 1px #666; color: black; font-weight: bold"
CommandName="Page" CommandArgument="<%# Container.DataItem %>" runat="server" Font-Bold="True"><%# Container.DataItem %>
</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
Here is my Cs Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
public void BindData()
{
List<tblLatestNew> lst_news = new List<tblLatestNew>();
lst_news = obj_news.GetLatestNews();
DataTable dtData = Utilities.ToDataTable(lst_news);
PagedDataSource pdsData = new PagedDataSource();
DataView dv = new DataView(dtData);
pdsData.DataSource = dv;
pdsData.AllowPaging = true;
pdsData.PageSize = 5;
if (ViewState["PageNumber"] != null)
pdsData.CurrentPageIndex = Convert.ToInt32(ViewState["PageNumber"]);
else
pdsData.CurrentPageIndex = 0;
if (pdsData.PageCount > 1)
{
Repeater1.Visible = true;
ArrayList alPages = new ArrayList();
for (int i = 1; i <= pdsData.PageCount; i++)
alPages.Add((i).ToString());
Repeater1.DataSource = alPages;
Repeater1.DataBind();
}
else
{
Repeater1.Visible = false;
}
rp_Order.DataSource = pdsData;
rp_Order.DataBind();
}
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
ViewState["PageNumber"] = Convert.ToInt32(e.CommandArgument);
BindData();
}
Please Help me resolving this!
Currently my table has 7 Rows, so Page 1 should have 5 rows and Page 2 must have 2 Rows
Thanks in Advance
The PagedDataSource requires a page index, which starts at zero for page 1. When you change the page in Repeater1_ItemCommand the command argument is the page number, not the page index.
To get the page index just change your code in Repeater1_ItemCommand so that you subtract 1 from whatever number is passed in via e.CommandArgument, that way the proper page will display
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
ViewState["PageNumber"] = Convert.ToInt32(e.CommandArgument) - 1;
BindData();
}
I am working on a project in which I am trying to have an empty label populate with the numbers that are being clicked. However, I am having trouble with my code behind my web form and cannot get it to work. My code behind:
public partial class WebForm : System.Web.UI.Page
{
ArrayList phoneNumber = new ArrayList() { };
int counter = 0;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnNum1_Click(object sender, EventArgs e)
{
counter++;
phoneNumber[counter - 1] = 1;
lblNumbers.Text = phoneNumber.ToString();
}
protected void btnNum2_Click(object sender, EventArgs e)
{
counter++;
phoneNumber[counter - 1] = 1;
lblNumbers.Text = phoneNumber.ToString();
}
My web form:
<form id="form1" runat="server">
<div class="container">
<asp:Image class="img-fluid d-block mx-auto" ID="imgLogo" runat="server" ImageUrl="~/Images/logo.png"/>
<div style="text-align:center; font-size:x-large; font-weight:800">
<asp:Label ID="lblNumbers" runat="server" Text=""></asp:Label>
</div>
<table class="table table-bordered" style="margin:auto; width:250px; height:342px; background-image:url(Images/Telephone-keypad.png); background-repeat:no-repeat; background-position:center;" >
<tbody>
<tr>
<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
<td><asp:Button ID="btnNum1" runat="server" OnClick="btnNum1_Click"/></td>
<td><asp:Button ID="btnNum2" runat="server" OnClick="btnNum2_Click"/></td>
<td><asp:Button ID="btnNum3" runat="server" OnClick="btnNum3_Click"/></td>
</tr>
<tr>
<td><asp:Button ID="btnNum4" runat="server" OnClick="btnNum4_Click"/></td>
<td><asp:Button ID="btnNum5" runat="server" OnClick="btnNum5_Click"/></td>
<td><asp:Button ID="btnNum6" runat="server" OnClick="btnNum6_Click"/></td>
</tr>
<tr>
<td><asp:Button ID="btnNum7" runat="server" OnClick="btnNum7_Click"/></td>
<td><asp:Button ID="btnNum8" runat="server" OnClick="btnNum8_Click"/></td>
<td><asp:Button ID="btnNum9" runat="server" OnClick="btnNum9_Click"/></td>
</tr>
<tr>
<td><asp:Button ID="btnStar" runat="server" OnClick="btnStar_Click"/></td>
<td><asp:Button ID="btnNum0" runat="server" OnClick="btnNum0_Click"/></td>
<td><asp:Button ID="btnPound" runat="server" OnClick="btnPound_Click"/></td>
</tr>
</tbody>
</table>
</div>
</form>
When each button is clicked, I want the numbers that are clicked to display in the label as they are clicked. Currently, when I click on a button, it gives me an error page that states:
Index was out of range. Must be non-negative and less than the size of the collection.
It also includes this:
Source Error:
Line 21: {
Line 22: counter++;
Line 23: phoneNumber[counter - 1] = 1;
Line 24: lblNumbers.Text = phoneNumber.ToString();
Line 25: }
Can someone please help?
Asp.net Pages are stateless, meaning your variables like "int counter" does not maintain its value between post backs. Because of this it will always be zero, you are then subtracting 1, which results in -1 which will result in the error that you are getting. Try saving the value in a session variable, or for even better results you could use javascript instead.
You need to append the value of the clicked number to the value in the label. Since you're in Webforms you can store it in ViewState instead of using a session variable (unless the user can come and go and your requirement is to preserve it).
I'd try something like this:
protected void btnNum1_Click(object sender, EventArgs e)
{
appendNumber(1);
}
.
.
.
private void appendNumber(int number)
{
counter++;
phoneNumber[counter - 1] = number;
ViewState["phoneNumber"] = phoneNumber.ToString();
}
protected void Page_Load(object sender, EventArgs e)
{
if(!String.IsNullOrEmpty(ViewState["phoneNumber"].ToString()))
{
lblNumbers.Text = ViewState["phoneNumber"].ToString()
}
}
static int counter = 0;
use this ...and it will hold values between postbacks
I am trying to set the text of the label control which is inside detailsview but it's not working. But it's showing error "Object reference not set to an instance of an object."
can anyone guide me please.. ??
My front end code is:
<asp:Panel ID="sub_question_panel" runat="server">
<asp:DetailsView ID="DetailsView1" runat="server" CellPadding="6" ForeColor="#333333" AutoGenerateRows="false" GridLines="None" >
<Fields>
<asp:TemplateField>
<ItemTemplate>
<table id="Question_view_table">
<tr>
<td style="font-family:Arial Rounded MT;">
<label id="Question_no"><span style="font-size:20px;">Question</span>:</label>
<asp:Label ID="Ques_id_label" runat="server" Text="Label"></asp:Label></td>
</tr>
<tr>
<td style="height:20px"></td>
</tr>
<tr>
<td style="font-family:'Times New Roman'; font-size:18px; ">
<label id="Question_detail"><%# Eval ("Question") %></label>
</td>
</tr>
<tr>
<td style="font-family:'Times New Roman'; font-size:18px;">
<ol style="list-style:upper-alpha">
<li>
<label id="optn1">   <%# Eval ("Option1") %></label></li>
<li>
<label id="optn2">   <%# Eval ("Option2") %></label></li>
<li>
<label id="optn3">   <%# Eval ("Option3") %></label></li>
<li>
<label id="optn4">   <%# Eval ("Option4") %></label></li>
</ol>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
</asp:Panel>
My back end code is:
protected void Page_Load(object sender, EventArgs e)
{
int question_id = 1;
Label Question_Id = DetailsView1.FindControl("Ques_id_label") as Label;
Question_Id .Text = Convert.ToString(question_id);
}
You must use the FindControl for a row, not DataListView
You want to find your label by id, But which one? For each row you have a label with id 'Ques_id_label' . So to find a specific label you must specify the intended row. I did not work with DataLisView but I know that it is logically similar to Asp:Repeater . To find a control in a row of a Repeater when a command is sent from a row:
protected void SaveAnswer(Object Sender, RepeaterCommandEventArgs e)
{
Label Ques_id_label = (Label)e.Item.FindControl("Ques_id_label");
Which with e.item you specify the intended row.
You use FindControl to find Ques_id_label, but then reference it normally anyway: Ques_id_label.Text =
It should be Question_Id.Text = Convert.ToString(question_id);, with the ID you assigned with FindControl.
But did it even compile? Do you use an editor like Visual Studio? Because when I tried your snippet it gave the error The name 'Ques_id_label' does not exist in the current context, as it is supposed to.
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;
}
}
I have a list view which retrieves the data from sql data source. I am trying to make two buttons(Yes and No) and a label outside the list view visible only if the list view is not empty. The process is: a person enter the information into text boxes and click the button retrieve, if the entered data exists in the database, the list view shows certain information.
I have the following code:
protected void btnExistingRetrive_Click(object sender, EventArgs e)
{
if (lstExisting.Items.Count>0 )
{
lblIsITYou.Visible = true;
btnYes.Visible = true;
btnNo.Visible = true;
}
}
By default buttons and the label are not visible.
The problem is when i click on retrieve button it shows me the list view with the information but buttons a the label are still not visible. They became visible only when i double click the retrieve button. Please tell me what is my mistake?
Thank you
Use the ListView EmptyDataTemplate
<asp:ListView ID="ContactsListView"
DataSourceID="ContactsDataSource"
runat="server">
<LayoutTemplate>
<table runat="server" id="tblProducts">
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Label ID="FirstNameLabel" runat="Server" Text='<%#Eval("FirstName") %>' />
</td>
<td>
<asp:Label ID="LastNameLabel" runat="Server" Text='<%#Eval("LastName") %>' />
</td>
</tr>
</ItemTemplate>
<EmptyDataTemplate>
<table class="emptyTable" cellpadding="5" cellspacing="5">
<tr>
<td>
<asp:Image ID="NoDataImage"
ImageUrl="~/Images/NoDataImage.jpg"
runat="server"/>
</td>
<td>
No records available.
</td>
</tr>
</table>
</EmptyDataTemplate>
</asp:ListView>
do you bind listview before checking the items count?
Do this on postback instead of in the event.
In your Page_Load do something like this:
protected void Page_Load(object sender, EventArgs e)
{
bool visible = (lstExisting.Items.Count > 0); // assuming it's never null
lblIsITYou.Visible = visible;
btnYes.Visible = visible;
btnNo.Visible = visible;
}
If the above creates complications then do as I said first with postback:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
bool visible = (lstExisting.Items.Count > 0); // assuming it's never null
lblIsITYou.Visible = visible;
btnYes.Visible = visible;
btnNo.Visible = visible;
}
}