Pag.Previous page returns null - c#

I have tried many times and in different ways to see why "Page.PreviousPage" returns null but I did not get anywhere. I checked most of posts here in stackoverflow and some other website. None of them worked. Anybody has any idea? Following is the asp code in the source page which I named it "sourcePage.aspx".It shows the button that does the cross page post:
<asp:Button ID="btnCrossPagePostBack" runat="server" Text="Cross page post back navigation" PostBackUrl="~/targetPage.aspx" Width="270px"/>
The following is the code in the target page which I named it "targetPage.aspx":
protected void Page_Load(object sender, EventArgs e)
{
Page previousPage = Page.PreviousPage;
if (previousPage != null && previousPage.IsCrossPagePostBack)
{
lblName.Text = ((TextBox)previousPage.FindControl("txtName")).Text;
lblEmail.Text = ((TextBox)previousPage.FindControl("txtEmail")).Text;
}
else
{
lblStat.Text = "You loaded this page using a technique other than Cross Page Post Back";
}
}
"txtName" and "txtEmail" are two text boxes in the source page. I know how to do this by using "Server.Transfer" or Strongly typed reference. I am looking for doing by Corss page post back and not any other way. Somebody said I should use Empty page and then add web forms and not template web forms but there is not empty page and visual studio only provides web forms. Any clue?
Thanks!

The point was correct. When you want to start a new project you should choose "Empty" and not "Web Form" or etc.

Related

Multiple Pages of same Website through QueryString from IIS

I have faced an issue while deploying a web application(asp.net c#, in IIS). What I have is single application for two DBs. So, Through querystring, I am determining the connectionstring in the master page.
My problem,is that I have two links each with separate QSs and when clicked, should lead to the corresponding version of the same application.Which is fine, for now when used one at a time. But when used side by side, they are producing the same DB application, as a replica of the first opened URL (which I guess is based on the server session).
Any suggestion on how to achieve this? Appreciate your help. Thanks!
I'm assuming that the idea at the moment is user will click the link on a page and this will redirect them to another page and on this page create the database connection based on the querystring.
Why not store the link clicked in a session variable, which you can then access on the page you're redirected to and create the DB link from that. If you're using a link button or something like that you could do:
Main Page
ASP.NET:
<asp:LinkButton ID="lnkButton1" Text="Link Button 1" OnClick="lnkMyButton_Clicked" runat="server" />
<asp:LinkButton ID="lnkButton2" Text="Link Button 2" OnClick="lnkMyButton_Clicked" runat="server" />
C#
protected void lnkMyButton_Clicked(object sender, EventArgs e)
{
LinkButton lnkButton = (LinkButton)sender;
Session["LinkID"] = lnkButton.ID;
Response.Redirect("MyNewPage.aspx");
}
On your new page:
protected void Page_Load(object sender, EventArgs e)
{
string LinkID = Session["LinkID"];
if(!String.IsNullorEmpty(LinkID))
{
if(LinkID == "lnkButton1") //create connection if first link clicked
else if(LinkID == "lnkButton2") //create connection if second link is clicked
else //handle error
}
else //handle null or empty string
}
I've not tested this, but let me know if it doesn't work.

ASP.NET : my html element repeated after my postback finish

i have an asp web page and my page contains to RadCombobox. for first drop down i set item_requested method (when the user click on it and select the theater name.it has a post back and after post back the second drop down fill with sans numbers ).
my problem is here:
after page post back the second field set duplicate in my page. in these pictures you can see that.
Before post back:
the second image :(After postback the second fielset repeated)
Note:
all of my controls in update panel. after i remove update panel from my page i doesn't happen again.Do you know the reason ? :[
You probably have some logic where you are adding this component. Try to check IsPostBack before adding the component to the page. Just like that:
if (!IsPostBack)
{
//dynamically add component
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
// ASPX C#
}
}
ASP
................... IM NOT GOOD AT ASP

Activating a ASP Multiview control's view from other page without querystring/session

I was looking for an alternative that may be used to activate a multiview control of other page without passing a querystring/session variable.
Basically, my Home.aspx page has a link that takes us to a specific page say "NewPage.aspx". The NewPage.aspx page has a multiview control that has three child views.
I want to click on the link of the Home.aspx and go to NewPage.aspx with MultiView1.ActiveViewIndex=1. Please remember that I do not want to pass any querystring variable as that link already contains some encrypted data as querystring and adding another variable can cause the data to corrupt. Maintaining a session isn't a solution as well because the application is quite big.
Any inbuilt method that can activate that view? (I don't seem to be talking practical but any help is really appreciated)
If you are asking about how navigate to this page I would wire up a button event (I prefer to do so in OnInit). Something like this:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.btnlinkclick.Click += new EventHandler(btnlinkclick_Click);
}
void btnlinkclick_Click(object sender, EventArgs e)
{
this.MultiView1.ActiveViewIndex = 1;
}
This should work for you.

Remember pagination when returning to page with ListView/DataPager

I have a simple ListView paged by a DataPager giving a list of products. When we click on the product we open the product details page. On the details page we want to "return to product list" but of course we want it to remember what page the datapager was at.
Surely this was conceived as a natural design requirement - what is the simple out-of-the-box way to do this?
If we use the QueryStringField property of DataPager we get the page number in the URL so I hoped I could use the referrer URL in the back link, but I have found the Request.UrlReferrer to be unreliable (when I use F5 to debug the app in Internet Explorer for example, Request.UrlReferrer is always null).
I have found some success with storing the page number in a session variable:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["PagerIndex"] != null)
{
DataPager1.SetPageProperties((int)Session["PagerIndex"],
DataPager1.MaximumRows, false);
}
}
}
protected void DataPager1_PreRender(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
Session["PagerIndex"] = DataPager1.StartRowIndex;
}
}
This method has some drawbacks:
Only works if QueryStringField is blank (so IsPostBack can be detected)
Session/cookie variable required for each pager
Question of how to reset the session/cookie variables arises
What is the "proper" way to do this?
Thanks
You can try my solution
Set the QueryStringField property of your datapager to a querystringfield say page.
<asp:DataPager ID="DataPager2" runat="server" PagedControlID="ListView1"
QueryStringField="page">
....
Note: I placed the DataPager outside of the ListView.
Put linkButton in your listview that will redirect you to detailspage and in its click event save the current page number in a Session
int integ;
decimal fract;
integ = DataPager2.StartRowIndex / DataPager2.PageSize;
fract = (DataPager2.StartRowIndex / DataPager2.PageSize) - integ;
if (fract > 0)
page = integ;
else if (integ > 0) page = integ - 1;
Session["page"]=page;
On the details page retrieve the page and pass back as a querystring to ListViewpage.
Automatically the datapager will take you to that page number, if not specified to page number 1.
Good Luck !
If you don't have any filters you can simply recalculate the page on which the product was.
Another option will be to encode the page (together with possible filter values for the list) in the URL of the product detail page and use them to generate an URL for the list that will be essentially the same as the one of the original list. Even something like the ReturnUrl approach used for login. Sharepoint does similar thing with its lists but I feel the URL can get too messy (and I am not a person that falls for the whole "clean URL" bullshit when I say it is messy it really is)
One more option would be to pass the product ID to the list page via the URL. The list page can then calculate the appropriate page. This will remove the noise from the URLs

Submit value of a TextBox without reloading page

I'm working on an ASP.NET based TicTacToe game. The problem I have with it is that:
The game is played between two users. When the first one types 'x' in the TextBox I want the 'x' to be shown on the second player's computer without reloading the page.
I don't know if some code will help but here is the way I did it without reloading(the user must reload the page manually... dumb):
protected void TopLeft_TextChanged(object sender, EventArgs e)
{
Application.Lock();
GameBoard gameBoard = new GameBoard();
gameBoard.board[0, 0] = char.Parse(this.TopLeft.Text);
Application["TopLeft"] = gameBoard.board[0, 0];
Application.UnLock();
}
And then, on page pre render:
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
Application.Lock();
if(Application["TopLeft"] != "0")
{
this.TopLeft.Text = Application["TopLeft"].ToString();
}
...
And so on...
I'd be very thankfull to anyone who can help!
You will need to use AJAX to do this. I recommend looking at some of the AJAX capabilities that jQuery offers but you can also look at the AJAX Toolkit from Microsoft.
Here is documentation for AJAX in jQuery:
http://api.jquery.com/jQuery.ajax/
I feel this is much "lighter" than what Microsoft offers out of the box. You can find out more about the Microsoft AJAX toolkit here:
http://www.asp.net/ajax/ajaxcontroltoolkit/samples/
You are asking about Partial Page Update.
First, you need to place the client TextBox or what ever other controls that you need to reload inside an UpdatePanel.
Then, you need to call the UpdatePanel.Update to update those controls whenever you need.
Check out AJAX. This will require client scripting to submit and detect updates without submitting or updating the entire page.
Note, however, that this is a fairly advanced topic and will not simply be a little snippet of code you can add. I would recommend a good AJAX/JavaScript/jQuery book.

Categories