I have the following PagerTemplate. I need to hide the Previous link when user is viewing the first page and hide the Next link if user is viewing the Last page.
<PagerTemplate>
<table border="0" style="width: 100%;">
<tbody>
<tr>
<td style="float: right;">
<asp:LinkButton CommandName="Page" CommandArgument="Prev" ID="LinkButton2" runat="server"><< Previous</asp:LinkButton>
<asp:LinkButton CommandName="Page" CommandArgument="Next" ID="LinkButton3" runat="server" >Next >></asp:LinkButton>
</td>
<td style="clear: both"></td>
</tr>
</tbody>
</table>
</PagerTemplate>
Grid's PageIndex and PageCount properties will help you here - Prev link should be shown/enabled only when PageIndex > 0 while Next link should be shown/enabled only when PageIndex < PageCount - 1.
Use row created event to find controls and alter the visibility. For example,
protected void GridView_RowCreated(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.Pager)
{
var prev = (LinkButton)e.Row.FindControl("LinkButton2");
prev.Visible = Grid.PageIndex > 0;
var next = (LinkButton)e.Row.FindControl("LinkButton3");
next.Visible = Grid.PageIndex < grid.PageCount - 1;
}
}
I am not sure of you need a custom template for current UI. You can use pager settings - for example,
<pagersettings mode="NextPrevious"
nextpagetext="Next >>"
previouspagetext="<< Previos"
position="Bottom"/>
And use PagerStyle for styling the UI.
<PagerStyle CssClass="myPager" />
Related
The ListView is displaying properly and the DataPager is displaying the buttons at the bottom, but when I click the NEXT button, the DataPager is not going to the next page of data.
ASPX page:
<div id="EventListing">
<asp:ListView ID="ListEvents" runat="server">
<LayoutTemplate>
<div ID="itemPlaceholder" runat="server">
</div>
<div id="pagerFormat">
<asp:DataPager ID="ListingDataPager" runat="server" PageSize="5" PagedControlID="ListEvents" QueryStringField="page" >
<Fields>
<asp:NextPreviousPagerField
FirstPageText="First"
LastPageText="Last"
NextPageText="Next"
PreviousPageText="Back" />
</Fields>
</asp:DataPager>
</div>
</LayoutTemplate>
<ItemTemplate>
<table id="tblEvents">
<tr>
<td rowspan="4" id="Col1Events"></td>
<td rowspan="4" id="Col2Events"></td>
<td rowspan="4" id="Col3Events"></td>
<td class="tdEvents"><span style="font-weight:bold; font-size: 1em;"><%#Eval("EVENT_DATERANGE") %>: <%#Eval("EVENT_NAME") %></span></td>
</tr>
<tr>
<td class="tdEvents"> <span style="font-size: .9em;"><%#Eval("EVENT_DESC") %></span></td>
</tr>
<tr>
<td class="tdEvents"><span style="font-size: .9em;"><%#Eval("EVENT_STREET") %>, <%#Eval("CITY.CITY_NAME") %></span></td>
</tr>
<tr>
<td class="tdEvents"><span style="font-size: .9em;"><%#Eval("EVENT_PHONE") %></span></td>
</tr>
<tr>
<td colspan="4" id="tdEmpty"></td>
</tr>
</table>
</ItemTemplate>
</asp:ListView>
</div>
Code Behind Page:
protected void btnFindEvents_Click(object sender, EventArgs e)
{
DateTime StartDt;
string EventType = ddlEventType.SelectedValue;
string dt = Request.Form["DatePickername"];
if (ddlEventType.SelectedIndex == 0)
{
EventType = "";
}
if (dt != "")
{
StartDt = Convert.ToDateTime(Request.Form["DatePickerName"]);
}
else
{
StartDt = DateTime.Now;
}
string CityName= ddlEventCity.SelectedValue;
if (ddlEventCity.SelectedIndex == 0)
{
CityName = "";
}
if ((ddlEventType.SelectedIndex == 0) && (ddlEventCity.SelectedIndex == 0))
{
//(1) ALL EVENTS
BLgetEvents obj = new BLgetEvents();
var EventList = obj.getAllEvents(StartDt);
ListEvents.DataSource = EventList;
ListEvents.DataBind();
}
}
you have to bind data again on the ListView when you page by DataPager. So, you need to implement this bind on PagePropertiesChanging event of your ListView control. Something like:
// 1 - add a method to bind ListView, add some parameters if you need
protected void BindListView()
{
var data = // get data from database
ListEvents.DataSource = data;
ListEvents.DataBind();
}
// 2 - call this method on the button
protected void btnFindEvents_Click(object sender, EventArgs e)
{
BindListView();
}
// 3 - call this method on the PagePropertiesChanging event of the ListView
protected void ListEvents_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
//set current page startindex, max rows and rebind to false
YourDataPagerControlId.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
//rebind List View
BindListView();
}
And on your asp.net webform, you need to set the event on the listview tag:
<asp:ListView ID="ListEvents" runat="server" OnPagePropertiesChanging="ListEvents_PagePropertiesChanging">
...
</asp:ListView>
When you set the datasource from code behine, the same principle is valid for other databound controls like GridView, DataList, Repeater etc (you need to bind again on some paging event). An alternative way to solve this, you could add a datasource control (like objectdatasource, linqdatasource, etc...) and set it on the ListView's DataSourceID property and the DataPager will work automatically.
For more details, look at this link: http://weblogs.asp.net/hajan/archive/2011/09/08/paging-listview-using-datapager-without-using-datasource-control.aspx
I know this is probably something so simple that I am just not able to see it. I have an aspx form that has a usercontrol in an updata panel. The user control is a people picker the searches on users from a corporate database.
What I want to see happen is:
The user clicks on a pick user button
The update panel with people picker becomes visible
They search for a user and then select the one they want.
When they make the selection and click Done, I get the user id of the user and look them up in our user table.
The user information should show up in a the form in label fields.
I can step through the code and see that I am getting the user information and that the label text is being set to the values but the page never updates the labels. It is a postback so I would think they would update.
<tr>
<td colspan="4">
<asp:UpdatePanel ID="CollapseDelegate" runat="server">
<ContentTemplate>
<asp:Panel ID="pDelegateHeader" runat="server">
<div style="padding: 10px 0 10px 20px; height:10px; text-align: left; background-color:#ffffff; color:#000000; border: 2px solid #ccc;" >
<asp:Label ID="ShowDelegate" runat="server" /><br />
</div>
</asp:Panel>
<asp:Panel ID="pDelegateBody" runat="server">
<PP:PeoplePicker ID="PP" runat="server" />
<asp:Button ID="btnOk" runat="server" Text="Done" CssClass="Buttons" onclick="btnOk_Click" />
</asp:Panel>
<asp:CollapsiblePanelExtender ID="CollapsiblePanelExtender3" runat="server" TargetControlID="pDelegateBody" CollapseControlID="pDelegateHeader" ExpandControlID="pDelegateHeader" Collapsed="true" TextLabelID="ShowDelegate" CollapsedText="Pick User..." ExpandedText="Close..." CollapsedSize="0"></asp:CollapsiblePanelExtender>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
<tr>
<td><asp:Label ID="DelegateNameLabel" runat="server" Text="Name:" CssClass="indentedText" /></td>
<td><asp:Label ID="DelegateNameValueLabel" runat="server" CssClass="indentedText" Visible="true"></asp:Label></td>
<td><asp:Label ID="DelegateEmailLabel" runat="server" Text="Email:" CssClass="indentedText" /></td>
<td><asp:Label ID="DelegateEmailValueLabel" runat="server" CssClass="indentedText" Visible="true"></asp:Label></td>
</tr>
<tr>
<td><asp:Label ID="DelegatePhoneLabel" runat="server" Text="Phone:" CssClass="indentedText" /></td>
<td><asp:Label ID="DelegatePhoneValueLabel" runat="server" CssClass="indentedText" Visible="true"></asp:Label></td>
<td><asp:Label ID="DelegateVerifiedLabel" runat="server" Text="Last Verified Date:" CssClass="indentedText" /></td>
<td><asp:Label ID="DelegateVerifiedValueLabel" runat="server" CssClass="indentedText" /></td>
</tr>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string PassedDelegateID = string.Empty;
string Mode = string.Empty;
int delegateId = 0;
if (Request.QueryString["Id"] != null)
{
PassedDelegateID = Request.QueryString["Id"].ToString();
}
else
{
PassedDelegateID = "0";
}
if (Request.QueryString["mode"] != null)
{
Mode = Request.QueryString["mode"].ToString();
}
else
{
Mode = "add";
}
if (Mode == "add")
{
pnlUdpateDelegateText.Text = UIConstants.ADDDELEGATETEXT.ToString();
}
else
{
pnlUdpateDelegateText.Text = UIConstants.UPDATEDELEGATETEXT.ToString();
if (int.TryParse(PassedDelegateID, out delegateId))
{
loadDelegateData(delegateId);
}
}
}
}
protected void btnOk_Click(object sender, EventArgs e)
{
TextBox txtSearchValue = (TextBox)PP.FindControl("txtSearchResults");
string LanId = txtSearchValue.Text;
User user = BusinessUtility.GetUser(LanId);
DelegateNameValueLabel.Text = user.Name;
DelegateEmailValueLabel.Text = user.Email;
DelegatePhoneValueLabel.Text = user.Phone;
DelegateVerifiedValueLabel.Text = DateTime.Now.ToShortDateString();
}
Thanks,
Rhonda
Because the labels are outside the update panel, only the content inside the update panel is updated from an ajax post-back, that's the whole point of an update panel.
You will need to either move the labels inside the update panel's content area, or have another update panel for the labels and make it's update mode "always"
Your lables are outside of the UpdatePanel.
Under the hood, ASP.Net performes a full postback, but only the part that pertains to your UpdatePanel is transfered back down to the client. Some JavaScript then takes this bit of HTML and replaces the existing <div> element that is your UpdatePanel.
Since your labels are outside of that <div> they never get updates.
I have table on my asp.net page, and I want to insert new rows.I try to add on every click on button new row which contain cell with FileUpload, but it works only for first time. When I click next time, in my code behind table.
<asp:Panel ID="pnlImages" runat="server" BackColor="Gray" Height="500">
<table id="tblImages" runat="server" width="100%">
<tr>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
</tr>
<tr>
<td align="right" width="100">
<asp:ImageButton ID="imbAddImage" runat="server" ImageUrl="images/plus.png"
Width="48" Height="48" OnClick="imbAddImage_Click"/>
</td>
</tr>
</table>
</asp:Panel>
This is code on button click
protected void imbAddImage_Click(object sender, ImageClickEventArgs e)
{
System.Web.UI.HtmlControls.HtmlTable tbl = (System.Web.UI.HtmlControls.HtmlTable)this.FindControl("tblImages");
System.Web.UI.HtmlControls.HtmlTableRow row = new System.Web.UI.HtmlControls.HtmlTableRow();
System.Web.UI.HtmlControls.HtmlTableCell cell = new System.Web.UI.HtmlControls.HtmlTableCell();
FileUpload temp = new FileUpload();
cell.Controls.Add(temp);
row.Controls.Add(cell); ;
int a=tbl.Controls.Count;
tbl.Controls.AddAt(a-1, row);
}
But problem is that a is always 2. Can anybody help ?
When it comes to dynamic controls, you need to add them on every post back.
You click event handler only adds the newest file and you are not saving the previous ones anywhere. You should add them to the ViewState and query the ViewState to retrieve them.
See this article For a detailed explanation of ViewState.
Am not sure why I cannot access my Label control which was inside the Panel and the Panel is inside the DataList
<asp:DataList ID="DataList2" runat="server" DataSourceID="SqlDataSource1" Width="100%">
<ItemTemplate>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<!-- post details -->
<td style="width: 60%">
<asp:Panel ID="panelPostDetails" runat="server" CssClass="postpage_details">
<table border="0" cellpadding="5" cellspacing="0" width="100%">
<tr>
<td colspan="2"><div class="postpage_header"><%# Eval("Heading") %></div></td>
</tr>
<tr>
<td>
<img src="picserver/posts/<%# Eval("ImagePath") %>/1.jpg" alt="preview" style="width: 240px;" />
<div id="morepictures">
<asp:Label ID="lblMorePictures" runat="server" />
</div>
</td>
<td>
<div style="padding: 0px 5px 0px 5px;">
<div>
more stuff here
</div>
</div>
</td>
</tr>
</table>
</asp:Panel>
<asp:RoundedCornersExtender ID="RoundedCornersExtender1" runat="server"
Radius="6"
Corners="All"
TargetControlID="panelPostDetails"></asp:RoundedCornersExtender>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
but when I tried using "lbl" in Page_Load, it seems it cannot find the control? can you please help me?
ItemDataBound and Page_Load event
---------------------------------
Panel p = DataList2.FindControl("panelPostDetails") as Panel;
Label l = p.FindControl("lblMorePictures") as Label;
l.Text = code;
that code returns Object reference not set to an instance of an object.
Thanks in advance
update:
ItemDataBound and Page_Load event
---------------------------------
Panel p = DataList2.FindControl("panelPostDetails") as Panel;
if(p==null)
{
System.Diagnostic.Debug.WriteLine("panel does not exist");
}
else
{
System.Diagnostic.Debug.WriteLine("panel does exist");
}
output:
panel does not exist
what on earth is going on!?!
Typically, you access controls like this at runtime by handling either the DataList's ItemCreated or ItemDataBound event. Here's a sample event handler:
protected void DataList2_ItemDataBound(object sender, DataListItemEventArgs e) {
if (e.Item.ItemType == ListItemType.Item) {
Label lbl = (Label)e.Item.FindControl("panelPostDetails").FindControl("lblMorePictures");
lbl.Text = code;
}
}
Wire up your the event handler like this:
<asp:DataList ID="DataList2" runat="server" OnItemDataBound="DataList2_ItemDataBound" ...
#Peter's code must work.
you can also try this:
protected void DataList2_ItemDataBound(object sender, DataListItemEventArgs e)
{
string st= (e.Item.FindControl("lblMorePictures") as Label).Text;
}
and put breakpoint to wath to st. In my case I get a text of lblMorePictures.
With my Form View I needed to add CType to the FindControl. I understand DataList doesn't necessarily need the Ctype however it is possible the Panel may need this prefix. In this case frmDelView is the name of my Form View. In my case the code line below is in my PreRender of my form. That was the only place at the time of creating the form it would return the data I needed to pass to a label on a next page of a Wizard Step page. Long story.
Note: my code is vb and not C#. It should be nearly or exactly the same.
CType(frmDelView.FindControl("txtcboDAcct"), TextBox).Text
I know I've done something like this before, but I have no idea why it isn't working now. I have a ListView with some textboxes. I want to read the text out of those boxes when I click a button (linkbutton, whatever).
<asp:ListView runat="server" ID="lv_bar" EnableViewState="true">
<LayoutTemplate>
<table>
<tr>
<th>Foo</th>
</tr>
<tr runat="server" id="itemPlaceholder"></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><asp:LinkButton ID="lb_delete" CausesValidation="false" runat="server" Text="Del" /></td>
<td><asp:TextBox id="txt_foo" runat="server" /></td>
</tr>
</ItemTemplate>
</asp:ListView>
<asp:LinkButton ID="lb_add" CausesValidation="false" runat="server" Text="Add" />
And then here's the relevant code-behind stuff:
protected void Page_Load(object sender, EventArgs e)
{
lb_chapter_add.Click += lb_chapter_add_Click;
if (!IsPostBack)
{
lv_chapters.DataSource = new List<Foo>() { new Foo() { Name = "harbl"} };
lv_chapters.DataBind();
}
}
void lb_add_Click(object sender, EventArgs e)
{
foreach (ListViewDataItem item in lv_bar.Items)
{
var txt_foo = (TextBox)item.FindControl("txt_foo");
Response.Write("foo: " + txt_foo.Text);
}
Response.Write("<br />the end");
Response.End();
}
But what I see when I enter some text into txt_foo and click lb_add is just "the end". What am I doing wrong here?
The problem it that you are using a a non persistent object as DataSource.
Due to clicking the button, you generate a postback and lv_chapters does not contain any items. Set a breakpoint in the line where the foreach is and you will see that lv_chapters.Items in null, or that it's Count property returns 0.