ASP.NET and C# Redirect - c#

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.

Related

How to change view in asp.net?

Im wondering how i can change "views" with a button click in Visual Studio (asp.net)?
I have 2 .aspx files, one called Default.aspx and another one called Library.aspx. How can i make a button change the views to display the other aspx file?
I tried googling but didnt find anything useful. Any help here is appreciated. Thanks
Ok, assuming default.aspx is in desing mode, then just drop a button from the toolbox onto the web page.
Say, like this:
Now, double click on the button, and we are jumped to code behind.
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Library.aspx");
}
So, we put for the code to jump to the web page Library.aspx
We get this then:
And now, when I click on that button, I am jumped to the web page called Library.aspx - we should see this (assuming the web page Library.aspx exists).

aspx pages not working after adding application in IIS

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.

Changing button label from c# code-behind

I am sorry I can't solve what must be simple to everyone else even after reading so many posts. Simply stated... I have an ASP webpage I created in VS2010 and when the user clicks the 'Send Email' button, I want to change the button either using JQuery then calling my code-behind to do the actual send. Alternatively, I want the C# code behind to change the button text while it is in the process of sending so the user knows to hold on.
I am new to JQuery and still pretty new to C#. My website is www. themilwaukeehandcenter.com. If you click the contact us... this is where I want the code. I have tried
$("ContactUsButton").click(function () {
ContactUsLabel.Text = "Processing";
});
on the main page. I have also tried
protected void ContactUsButton_Click(object sender, System.EventArgs e)
{
ContactUsLabel.Text = "Sending... Please Wait";
ContactUsLabel.Refresh();
I know this should be simple but hours later.... reading so many posts later.... I feel no farther. I can't quite figure out how the JQuery onclick and the ASP onclick interact. I also don't know why C# flags the Refresh() as not valid. Any help welcome.
I actually placed a ContactUsLabel on the form too when I couldn't get the ContactUsButton to do what I intended. I am too knew to understand what you mean by 'How have you declared your button, but this is the code that creates it:
<asp:Button ID="ContactUsButton" runat="server"
OnClick="ContactUsButton_Click" Text="Send Email" Width="120px"/>
<asp:Label ID="ContactUsLabel" runat="server" Text="" Width="120px">
</asp:Label>
Your WinForm Code:
<asp:button id="ContactUsButton" runat="server" onclick="ContactUsButton_Click" cssClass="myButton" text="Contact Us" />
On submit use the jQuery script to change button text.
$(document).ready(function() {
$('.myButton').click(function(){
$(this).attr('value', 'Processing...');
});
});
View in fiddler: view
Finally at the server side:
protected void ContactUsButton_Click(object sender, System.EventArgs e)
{
ContactUsButton.Text = "Thank You";
}

Refresh a button

protected void Button1_Click(object sender, EventArgs e)
{
}
My application contains a update button. when I click the button the browser must automatically refresh the page.
Is there any code behind code for button?
there is none. Doing so will create a postback on your page, and so will "refresh" your page, at least everything that was done in the page load event and in the prerender event.
In a updatePanel in ajax, it will create a callback. So that only the updatePanel will undergo the postback on the client side, whereas the whole page cycle will be run on the server side.
with server-side button control - you cant.
not sure what are you trying to achieve, but here's a JS workaround for the issue (using JS location.reload() to reload the page):
<input type="button" value="refresh" onclick="javascript:location.reload()" />
Here is one way to do it

NullReferenceException on ASP.NET PreviousPage Property

I followed the directions from MSDN on transferring data between asp.net pages.
But when using the 'PreviousPage' property to access the previous pages controls, I get a null ref exception that PreviousPage is not set to an instance of an object.
Here is my code:
public partial class Portal : System.Web.UI.Page
{
public string Username
{
get
{
return txt_User.Text;
}
}
And this is the submit button on initial page:
<asp:Button ID="btn_Submit" runat="server" onclick="btn_Submit_Click"
PostBackUrl="~/Query.aspx"
Previous page property on second page:
protected void Page_Load(object sender, EventArgs e)
{
Username = PreviousPage.Username;
}
As per MSDN instructions I also added this at the top of the second pages markup file:
<%# PreviousPageType VirtualPath="~/Portal.aspx" %>
Also note I have tried Server.Transfer to switch pages instead and that produces the same error.
EDIT, here is using Server.Transfer on the initial page click event:
protected void btn_Submit_Click(object sender, EventArgs e)
{
Server.Transfer("Query.aspx");
}
EDIT, button code without event handler:
<asp:Button ID="btn_Submit" runat="server"
PostBackUrl="~/Query.aspx"
style="height: 26px" Text="Submit" />
This works fine for me. If PreviousPage is null, that generally indicates the current page was not displayed as a result of a cross-page postback.
Can you confirm the error is being raised on the second page?
Also, what the onclick="btn_Submit_Click" in your button definition? There should be no code responding to the click event on the original page. Remember, it will be handled by the target page.
EDIT: Now that you've updated your question, my last point seems to be the issue. You are doing a server transfer from the original page. This is NOT a cross-page postback and that's why PreviousPage is null.
Remove the onclick attribute from your button and delete btn_Submit_Click.

Categories

Resources