i have webapplication. In my master page i have few link buttons which are as below
<asp:LinkButton ID="link1" runat="server" OnClick="linkAge_Click">Age</asp:LinkButton>
<asp:LinkButton ID="link2" runat="server" OnClick="linkName_Click">Name</asp:LinkButton>
In my Visual studio i have seperate folder called Age and Name and under those there are default.aspx.
on click event of link button i have this code
protected void linkAge_Click(object sender, EventArgs e)
{
Response.Redirect("/Age/");
}
protected void linkName_Click(object sender, EventArgs e)
{
Response.Redirect("/Name/");
}
In IIS i added Application called "Test" and then added all code inside it
When i browse i get to master page as http://localhost:80/Test
When i click on link "Age" the url Changes to http://localhost:80/Age
I expected it to be http://localhost:80/Test/Age
What is wrong that i am doing? Can i achieve this without using any code changes.
Try to use http://localhost/Test/ (with slash in the end) link in to Your browser.
You need to add a tilde (~) to get URLs relative to application root:
Response.Redirect("~/Age/");
BTW: Why do you need to have the redirect behind a post-back? You could use <asp:HyperLink> instead
Like we do this in mvc to get path from root - Url.Content("~\Action\")
In web form -
protected void linkAge_Click(object sender, EventArgs e)
{
Response.Redirect(Server.MapPath("~/Age/"));
}
You got to think like this i think.
Since you're using only the /Age/ path, with nothing to indicate that you're using a relative path. For code managed by IIS, you'd use ~/Age/Whatever to indicate that you intend to go to Age relative to Test.
Related
I have 2 page in my application:
First is:"A.aspx" and Second is:"B.aspx"
A.aspx has one button with event as below:
protected void Button6_Click(object sender, EventArgs e)
{
HttpContext.Current.Items["Name"] = "University";
Server.Transfer("B.aspx");
}
In B.aspx :it just writes session data from A.aspx
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(Context.Items["Name"].ToString());alues
}
The issue is when it redirects to "B.aspx" and since i have used server.transfer
the content of "b.aspx" is shown with url as "A.aspx" in address bar which is inconsistent for users.
I want consistent url,means along with content of "B.aspx" ,url should also reflect as "B.aspx".
I tried using Response.Redirect but HttpContext does not work with Response.Redirect as context is getting lost while traversing.
Since i have to implement it in a banking application,So i cannot use querystring ,sessions instead of HttpContext.
So,is there is way to use Response.Redirect with HttpContext or can i manipulate url on page load of B.aspx so as to achieve consistent URL.
Or there is any other way to achieve this.
Is it possible from c#/asp.net to call/show a separeted html-page in the same window.
I don't want to store the entire html code into a
, for exemple...
(that works).
Possible with javascript? or do I have to modify the code-behind?
The purpose is to load this html page everytime the default.aspx is invoked, as a webform(pop-up)
You can Use iframe to open your .html page as:
.aspx Page:
<iframe id="ifrm" runat="server"></iframe>
And on Page_Load(code behind):
protected void Page_Load(object sender, EventArgs e)
{
ifrm.Attributes["src"] = "yourHtmlpageURL.html";
}
You can use it asp.net
<iframe src="http://www.google.com"></iframe>
For c# application you can use WebBrowserControl.
I am needing to use the same form for displaying multiple things and I realize that when I add links like:
A
it has the same form and web address, but with a #A at the end of the address.I thought I could use this for displaying multiple things on the same form. My idea is to have C# code in the page_load method to detect what the web address is and use a conatins method for the url string and detect if there is #A to change the content of the form. Here is an example:
C# code:
protected void Page_Load(object sender, EventArgs e)
{
string url = HttpContext.Current.Request.Url.AbsoluteUri;
if(url.Contains("#A"))
{
div1.Visible = false; //content 1
div2.visible = true; //content 2
}
}
asp.net code:
A
<div ID="div1" runat="server">
content 1
</div>
<div ID="div2" runat="server">
content 2
</div>
I have tried to put the Page_Load method in a script tag, but still didn't work. I guess since the url is different the cs code is not valid? I know it goes through the page_load method once, before I click on the link. Also I do use a method that gives me the controls of div1 and div2, so that is not the problem. I thank everyone in advance for your help! Also if my way is not the way to do the job then please tell me any way possible to achieve what I am trying to do.
edit: I can't use a button to replace a link... maybe a asp:hyperlink?
That's an HTML hyperlink you're using and it won't cause a postback thus page_load will never get called when you click it.
I would suggest if you want to show an hide divs that you use client side JavaScript. Alternatively you could (for example) use an asp.net button control which will cause a postback.
I would suggest scrapping the anchor with an href approach in favor of this:
Use the ASP.NET server controls, along with their click event handlers to manage the visibility of controls on your page.
In your Page_Load, make it so the page has an initial state of showing controls, like this:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
div1.Visible = false; //content 1
div2.visible = true; //content 2
}
}
Now instead of an anchor tag, you can use an ASP.NET Button or LinkButton to cause a postback to the server, like this:
<asp:Button id="Button1"
Text="Click here to change page to B"
OnClick="Button1_Click"
runat="server"/>
Now you have the event handler code which would change the visibility of controls, like this:
protected void Button1_Click(Object sender, EventArgs e)
{
div1.Visible = true; //content 1
div2.visible = false; //content 2
}
I am working on a project for school, and this is an extra credit part. I have a project started in VS 2010 using master pages, and what I'm trying to do is get a "Submit" button to redirect people to the "MyAccounts.aspx" page. My current code for the ASP part for the button looks like this:
<asp:Button ID="btnTransfer" runat="server" Text="Submit"/>
I have tried adding in the OnClick option, as well as the OnClientClick option. I have also added this code to the Site.Master.cs file as well as the Transfer.aspx.cs file:
protected void btnTransfer_Click(object sender, EventArgs e)
{
Response.Redirect(Page.ResolveClientUrl("/MyAccounts.aspx"));
}
When I run this and view the project in my browser, the whole thing runs fine, but when I click on the "Submit" button, it just refreshes the current page and does not properly redirect to the MyAccounts page. Anyone have any ideas for me?
You are doing it almost correctly, you just haven't put the correct pieces together. On Transfer.aspx, your button should be:
<asp:Button ID="btnTransfer" OnClick="btnTransfer_Click" runat="server" Text="Submit"/>
and your code behind should be like what #KendrickLamar said:
protected void btnTransfer_Click(object sender, EventArgs e)
{
Response.Redirect("~/MyAccounts.aspx");
}
The OnClick event tells it what to execute on post-back when the users clicks the button. This is in the code-behind for Transfer.aspx, not the site master.
i am using Intelligencia UrlRewriter for converting my page
www.mywebsite.com/subject.aspx?subject=sub1
to
www.mywebsite.com/subjects/sub1.aspx
On this page, I have next and previos buttons to browse the different subcategories on that subject and have used DataList with paging to support that.
When the page is first shown(IsPostBack=False) it works fine, but when next button is fired, the URL converts into this:
www.mywebsite.com/subjects/subject.aspx?subject=sub1
Is there any idea why it is happening ?
My web.Config file is as follows :
Mt web hosting company uses IIS 7.
EDIT: I have windows 7 and I tried by using local IIS and it ran fine there.
You can code this in your master page for this problem
Here form1 is the form tag and place it in master page's load event
protected void Page_Load(object sender, EventArgs e)
{
form1.Action = Request.RawUrl;
}