I used server.execute to hide my query string in URL, but I found another problem, when I'm in page 1 and clicks a button that transfers me to page 2 , page 1 content is still displayed in the page with the content of page 2. Both pages are shown to me. How can I resolve this ?
example of my code. in page 1 , there is a button, I add this code in click event.
protected void Button1_Click(object sender, EventArgs e)
{
Server.Execute("Page2.aspx?Name=john");
}
in page 2 , there is a text box that reads the query string value.
TextBox1.Text = Request.QueryString["Name"].ToString();
I send multiple values in query string but this is just an example. however, page 1 and page 2 contents are both displayed in one page after clicking the button that should transfer me to page 2.
Use Server.Transfer instead of Server.Execute.
Check this out to understand the difference.
Difference between both
When Server.Execute is used, a URL is passed to it as a parameter, and the control moves to this new page. Execution of code happens on the new page. Once code execution gets over, the control returns to the initial page, just after where it was called. However, in the case of Server.Transfer, it works very much the same, the difference being the execution stops at the new page itself (means the control is'nt returned to the calling page).
In both the cases, the URL in the browser remains the first page url (does'nt refresh to the new page URL) as the browser is'nt requested to do so.
As server.execute transfer control back to original page and continue execution of it also, therefor you are seeing output of both pages. To Transfer the request completely use Server.TransferRequest
Page1.aspx : add query string values as NameValueCollection
NameValueCollection nv = new NameValueCollection();
nv.Add("Name","john");
Server.TransferRequest("Page2.aspx",true,"GET", nv);
Page2.aspx : get your values from Request.Header
TextBox1.Text = Request.Headers.Get("Name");
Related
I have two simple pages. Page 1 postbacks to page 2. I have two buttons that can cause a postback, Confirm and Delete. On page 2 there are two buttons; one saves the passed data, and the other deletes the passed data. What I need to do is have the unneeded button on page 2 set to visible = false. How can I set up different actions based on which button caused the postback?
you could use Query string to indicate the page to show the appropriate button in your second page
ex : http:\localhost\page1.aspx?show=delete
at the other hand, in your page2 "page_load" function you could manage the code to show the button based on query string requested along with the request url
the code will be something like this on your Page_Load:
string showButton = Request.QueryString["show"];
if(showButton == "delete")
{
// write the code to show the delete button here
}
A button on html page redirect to aspx page with window.open() command.
There are certain data on the html page which i want on server-side of the aspx page before page_load executes.
following is my code on the html page which redirects to the aspx page
var win = window.open('mypage.aspx','_self');
win.args=this.Args;
following is my code on aspx page which tries to catch the data passed from the html page
<script type='text/javascript'>
var readyStateCheckInterval = setInterval(function () {
if (document.readyState === "loading") {
$('#hdnArgs').val(window.args);
clearInterval(readyStateCheckInterval);
}
}, 100);
</script>
<input type='hidden' id='hdnArgs' runat='server'/>
Following is the code on the aspx.cs file which tries to read the hidden variable's value which has been set from the data of the html page
protected void Page_Load(object sender, eventargs e)
{
string data = hdnArgs.value; //this is always null
}
But what I get is always 'null'.
The readyStateCheckInterval sets the hidden variable value after the page_load event is completed.
I want the value of hidden variable before page_load.
How can I get that?
Its not possible to set value of any control before page life cycle finish.
let me explain you..
You try to set value on hdnArgs but In Page lifecycle control only generate and send it to browser only after finish Page_init,Page_Load,Page_Load_Complete
All those methods..
So,When you try set args value to hdnArgs using $('#hdnArgs').val(window.args); Page_load event already completed..
Solution
I think you need to pass value as a QueryString to get Value in Page_load
var win = window.open('mypage.aspx?Args='+this.Args,'_self');
and In ASPX page
protected void Page_Load(object sender, eventargs e)
{
string data = Request.QueryString["Args"];
}
Also you can send data using Page Postback if you have large data.
To confirm:
hdnArgs is on the starting page
starting page opens a new page in a window
when that page is opening (but not complete, which will hopefully happen when you happen to look at it at 100ms intervals... it won't if it's quicker than that), input#hdnArgs is updated to the a value from the opened page
The Page_Load is on the starting page
You want the start page Page_Load to get a value from the popup
You'll need to review the lifecycle of asp.net pages, eg: https://msdn.microsoft.com/en-gb/library/ms178472%28v=vs.100%29.aspx
By the time the start page even thinks about processing any javascript, the Page_Load of the startup page is long gone.
Perhaps, in the Page_Load, you could load the new page directly eg with
var content = new System.Net.WebClient().DownloadString(contentUrl);
and parsing it for the #hvnArgs
I am trying to Session over my Label.Text in the first .aspx page to another Label in the second .aspx page. I retrieve my value from the database and place them in the first Label.Text and want to Session this Label (Which i got the text from the database) to another page linked to the first page. I use this method is because i have a detailed products page (My first .aspx page) which consists of many products and when user click a particular product, it's product name have to be displayed in the second page of the .aspx . My problem here is that when i Session over the Label's text on the first page, the value does not pass over to another page.
My first page's .cs code (code behind):
Session["productName"] = productName.Text;
Response.Redirect("products2.aspx");
My second page's .cs code (code behind - I place this code in page load)
if (Session["productName"] != null)
productName.Text = Session["productName"].ToString();
Have i gone wrong anywhere?
Inorder to make the session not to be expired for a long time, you should follow two steps.
Keep a continous eye on the Session Timeout.
Redirect the session when its about to expire.
The Base Page for Detecting Sessions will explain you every thing you need to do, please refer it once.
your code is totally right..
but my question is are you using ispostback in your page load??
if not then use it like this..because of postback sometimes you can't get values.
then put your code if is not postback
page_load()
{
if(ispostback)
{
}
else
{
//put your code here
}
}
I have a list of 10 hyperlink on default1.apx. On selecting any hyperlink it redirects to another page and all hyperlinks redirects to the same page default2.aspx. But how can i now which hyperlink is clicked from 10 hyperlinks list in asp.net using C#.
There are a couple of ways, all depending on how you're doing your redirect.
I'm guessing you're using Response.Redirect(), which means you must be raising a server side event when clicking. In this case all you need to do is check the sender argument which will give you details of which control was clicked.
Another way is to append the controls' name onto the URL in a GET request. So that each link is slightly different. For example link one could point to default2.aspx?linkthatwasclicked='link1' where you can substiute link1 accordingly, this value can then be retreived on default2.aspx through the Request.QueryString object.
m.edmondson has got it, another way I've seen is having a "hyperlink.aspx" page which then requests an id:
Link 1
then in the hyperlink.aspx code:
int id = int.Parse(Request.QueryString["id"].ToString());
switch(id)
{
case 1:
// Add a count to a table maybe
// Get the url from the DB here...
string url = GetUrlById(id);
Response.Redirect(url);
break;
}
I am trying to a redirect the user when they click on a specific button
protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("ControlPanel/Default.aspx");
}
The problem is when I click Button1 it redirects me to another page
localhost:57988/WebSite5/Default.aspx
and the weirdest thing is it open another page with this link above, not the default page I have, but another but with the default.aspx page url that you see!
Any suggestions?
You are not doing a redirect, you are doing a transfer. That means that the execution continues with the new page, but the URL doesn't change. The page that you transferred to is returned as response to the request for the first page.
Use Response.Redirect instead of Server.Transfer to do a redirect.
When you use Server.Transfer it won't display the new URL in the query string. Could it be that is what's throwing you off?
Here is a good article on Response.Redirect vs Server.Transfer. If you really want to redirect the user then you should use Response.Redirect.