Quite simple question, but I am having loads of issues with it.
protected void restorePagerNumber()
{
if (Session["PageNumber"] != null)
{
System.Diagnostics.Debug.Write(Session["PageNumber"]);
DataPager pager = searchListView.FindControl("searchDataPager") as DataPager;
pager.SetPageProperties((int)Session["PageNumber"] * pager.PageSize, pager.MaximumRows, false);
}
}
Thats what I currently have, I tried to use it before databind, after data bind, none of them seem to work. Can I actually change pager value after creating new object?
Doesnt sound logical, but I cannot access datapager without that. Is there another way to access dataPager that is in a listView and maybe another way to set its page number.
Cheers
I found a scenario that is similar to yours (https://web.archive.org/web/20210125144848/http://www.4guysfromrolla.com/articles/021308-1.aspx) and I verified that the sample application works calling SetPageProperties() at run-time.
Be sure to change the last "databind" argument in your SetPageProperties call to from False to True:
pager.SetPageProperties((int)Session["PageNumber"] * pager.PageSize, pager.MaximumRows, true);
then make sure you're calling restorePagerNumber at PageLoad
protected void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack) {
restorePagerNumber();
}
}
Hope that helps.
Related
I have a problem that I can't resolve on the server side of my project.
I'll explain:
I have a page named Global,this is ASP.NET page.
This page uses a UserControl named CateGories.
Now I have a button on this UC page,that when I press I want to invoke a function on the Global page that makes a connection with my DB.
I decided to use delegates(events)
This is the code.
Global page:
//here i add my function to the event
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ShowCurrentTime.Text = DateTime.Now.ToString();
}
CateGories ClassCat = new CateGories();
ClassCat.MainDel += PopulateLinks;
}
//this is the function that the event will run
public void PopulateLinks(string CategoryName)
{....}
Code of the UC page (CateGories):
//delegation of the event
public delegate void Click(string ButtonName);
public event Click MainDel = null;
//function that invokes when I click a button
protected void News_Click(object sender, EventArgs e)
{
if (MainDel != null)
{
MainDel(News.Text);
}
}
Now everythig should work fine, but there is a problem, when the compiler gets to the
if(MainDel!=null)
...
It doesn't get in the function, there go MainDel is null.
I can't see the problem here, why after I insert function to MainDel, its gets null eventualy...
I'll be happy if someone can help
thanks.
Max.
I think I've encountered this problem before, when working with web applications the way I would a windows application.
The problem lies in that when the page gets reloaded, a new instance of your page class is created so any values from the last server interaction are lost.
MainDel is indeed null for what I can see.
You're creating one instance of CateGories on your Web Form and another one on your User Control. And once you check it for beeing null on the UC the reference is to the not initialized object.
One possible way to do that is creating and adding the User Control programatically to the page before PageLoad() and keeping a reference to it so you can access it's properties.
Another solution could be using Page.FindControl to find the UC and make the subscription to the event.
The method names above may be incorrect, it's been a long time without working with web forms.
Is there an event I can use for whenever the value of a NumericUpDown Control is set. Not just changed because alot of the times it is set programatically to the same value but the event still needs to be called.
Edit: I mean its not very complicated code its just there's alot of it. because this is pretty much the same for all the body parts
private void NumUpDownLeftHandDirection_ValueChanged(object sender, EventArgs e)
{
game.Ragdoll.LeftHand.DirectionOffset = (float)NumUpDownLeftHandDirection.Value;
game.Ragdoll.Direction = game.Ragdoll.Direction;
MainTimeLine.Frames[MainTimeLine.CurrentFrame].LeftHandDirection = (float)NumUpDownLeftHandDirection.Value;
}
I could just put it all in properties but that will be alot of work since I've already coded this expecting it to work.
I use frame to load pages in my WPF project, is there a way to detect a page loading is the first or not? Something like "IsPostBack" in ASP.NET, I'm trying to find an equivalent to it in WPF.
I found IsPostBack is a property in System.Web.UI, should I include this namespace in my page?
I still have to use a static variable "bool SystemLoad = true", at the first load it is True and then I set it to False, so when the page is reloaded, it doesn't do as at the first load.
Thank you!
IsPostBack is not relevant to a WPF application, and since your WPF application window does not inherit 'Page', there is no way you can use IsPostBack variable from System.Web.UI.
The best you can do is to implement your custom logic as below.
private bool isLoaded;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (isLoaded)
return;
isLoaded = true;
}
Created a user control.
Within user control, I have a simple control, like a Literal, in the markup.
In the user control's Page_Load, I attempt to change a property, like Text, on control created in the markup.
Control is null.
What in the world am I missing?
(Begin rant: I am doing all these fancy things with login systems and dynamically adding/modifying controls on the fly with AJAX, etc, but I can't change an f'n static control's property! I can't even Google such a simple problem without finding something else! In the past, I had to dynamically generate the whole bloody page to avoid this idiotically simple problem. End rant.)
Page.LoadControl() has two overloads, one accepts a Virtual Path, the other accepts a Type.
Sometimes, when using the Type version overload, you get this kind of behaviour.
When possible, try to use the Virtual Path version of LoadControl.
Edit: as an FYI, this is because the actual Type of a User Control is not what you would expect it to be. ASP.NET kind of 'wraps' the Type of the User Control.
I figured out a way after analyzing the order a page is executed (http://msdn.microsoft.com/en-us/library/ms178472.aspx).
protected void Page_Init(object sender, EventArgs e)
{
literalGameName.Text = myGame.Name;
}
Init happens after controls have been made, but not after the page is done loading control-stuff.
I know this is an old one, but wanted to chime in on how I fixed a similar issue I was having. I have a usercontrol with an asp:Panel on it that I was trying to set the visibility off during the Page_Load, but the control was coming up null. I then noticed that my Page_Load definition looked like this:
protected void Page_Load(object sender, EventArgs e)
{
_myControl.visible = true;
}
When in fact, I needed the definition to look like this:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
_myControl.visible = true;
}
Doing the override fixed the issue.
I am trying to create a popup which will be used to select a month/year for a textbox. I have kind of got it working but however when I try and read from the textbox when I Submit the form it returns an empty string. However visually on the page I can see the result in there when I click the Done button which can be seen in the screenshot.
http://i27.tinypic.com/2eduttx.png - is a screenshot of the popup
I have wrapped the whole textbox/popup inside a Web User Control
Here is the code of the control
Code Behind
ASP Page
and then read from the Textbox on the button click event with the following
((TextBox)puymcStartDate.FindControl("txtDate")).Text
Any suggestions of how to fix the problem?
You may need to read the form posted value rather than the value from the view state. I have the following methods in my code to handle this.
The below code just grabs the values in the request headers (on post back) and sets/updates the controls. The problem is that when using the ASP.NET Ajax controls, it doesn't register an update on the control, so the viewstate isn't modified (I think). Anyways, this works for me.
protected void btnDone_Click(object sender, EventArgs e)
{
LoadPostBackData();
// do your other stuff
}
// loads the values posted to the page via form postback to the actual controls
private void LoadPostBackData()
{
LoadPostBackDataItem(this.txtYear);
LoadPostBackDataItem(this.txtDate);
// put other items here if needed
}
// loads the values posted to the page via form postback to the actual controls
private void LoadPostBackDataItem(TextBox control)
{
string controlId = control.ClientID.Replace("_", "$");
string postedValue = Request.Params[controlId];
if (!string.IsNullOrEmpty(postedValue))
{
control.Text = postedValue;
}
else
{
control.Text = null; // string.Empty;
}
}