calling a C# function from a link on an asp page - c#

I have an asp login page. When a user clicks a link I need it to call a function in a .cs file to dynamically create the url and redirect. How would I go about doing this?

I think this is what you're looking for.

you need to make use of postback. Add for example Button on the page and set property AutoPostBack=true, set it's Click event handler to be a method in your cs file (code behind) and use Response.Redirect() inside that handler

Related

LinkButton or HyperLink?

I have a situation where I'm not sure if I should use a HyperLink or LinkButton. When a user clicks on a list of links I want to trigger a click event where I save some information to session (should use LinkButton) but I also want these links to open up new tabs (should use HyperLink).
A LinkButton will postback, it's essentially a button that renders like a link. You could set a response.redirect(url) in the event handler to set a new tab.
Can you add more information, of what you want to do in the handler, maybe this could be achieved with Jquery calling a server-side method?
Difference between Hyperlink and LinkButton
Click Api with Jquery and Jquery post.
You will need to use a LinkButton which causes a PostBack. To open additional tabs, emit JavaScript.
protected void MyLinkButton_Click(object sender, EventArgs e)
{
Session["MyData"] = 123;
Page.ClientScript.RegisterStartupScript(Page.GetType(),
"newWindow",
"window.open('http://myurl','_blank');",
true);
}
I would say a HyperLink that hits a specific URL to store the necessary session data, then use Response.Redirect to redirect to the following page after storing the information.
The HyperLink's URL would point to your server so that you can store the information, and then you would use a redirect to point the user to the correct endpoint after storing the necessary data.
Example
HyperLink's URL points to ~/yourpage.aspx?state=NY, with target="_blank"
Server responds to URL and checks querystring.
If query string exists, store data (if (Request.QueryString["state"] != null) Session["state"] = Request.QueryString["state"])
Redirect the user to the appropriate URL (Response.Redirect("http://www.ny.gov"))
If the data is at all confidential, then you would want to use the LinkButton methods pointed out in other answers. Opening up a new tab is tricky, so you would probably have to write out some Javascript as outlined in #andleer's answer since I don't believe there is an easy way to pop open a new window from the server side otherwise.
You need to use LinkButton.
The difference between the two is that LinkButton postbacks your page to the server allowing you to make your logic while HyperLink does not postbacks - just redirects you to the specified link, therefore, use HyperLink when you want to navigate.
The LinkButton control is used to create a hyperlink button. This control looks like a HyperLink control but has the same functionality as the Button control.
with LinkButton you also get the facility of Web Control Standard Properties and Control Standard Properties .
You're right, can't do both, since a LinkButton will trigger a postback, and a hyperlink will simply navigate to a new page.
In your case use you can use a LinkButton and the server code will have to to do a redirect (if you want to navigate to another page) or handle the Tabs and return the page with the Tab opened if you are using a tabs element. (so the tab navigation will not be done front end)
In such cases you can use button for saving some information and you can also use it as a like option. But if you want to add a link then you have to use hyperlink.. You can also use javascript to link a url with the button so that when user clicks on that button the session information will get stored and then he will redirected to the new webpage.

Postbackurl Vs Response.Redirect (losing data)

I have a button in my aspx page, which in code behind the value of postbackurl gets set. I updated the code to add click event handler and added Response.Redirect code to redirect user to the page once the button is clicked. The only reason i ended up adding click event was because i had to add some extra logic when the button was clicked including redirecting to same url, that was used in postbackurl. The page does get redirected on click however it seems like all the hidden fields from the field gets lost once the form get redirected to the url.
Is there anyway i can submit the form without loosing the hidden data.?
Maybe one way you can solve this problem is to use the server side Transfer() call.
http://msdn.microsoft.com/en-us/library/540y83hx(v=vs.85).aspx
In most cases I've seen what you really want to do is pass the information for the new page using URL parameters. Take all the stuff the new page needs and put it into the url (encrypt if it is security sensitive).
If the only issue you are having is when you want to stay on the same page, this is simple, just have the click event handler return without doing a response.redirect or a transfer.
Post the code if this is not working for you.

Reload a Web Page Using C#

I have a web page that prompts for user input via DropDownLists in some table cells. When a selection is made the selection replaces the DropDownList so that the action can only be performed once. In the case that a change needs to be made I want to be able to click a button that reloads the page from scratch. I have Googled and Googled but I have not managed to find a way to do this.
Any advice is appreciated.
Regards.
Put a link on the page with the text "Reload" and the url the url of the page. It's perfectly valid to have a page with a link to itself.
If you don't like the link idea, use a standard Button and in the click event, use Response.Redirect to redirect to the current page.
You can set an OnClick for your button that resets each DropDownList's SelectedIndex to 0 instead of reloading the page from scratch. Alternatively, you can set a Response.Redirect([the page's url]) into the OnClick as is suggested here.

how to target codebehind method using __DoPostBack without hidden control

I want to call a server side method using __DoPostBack, but i don't want a hidden ASP runat server control in my page. Is it possible to call a server side method by its name and not by the name of the control that triggers it?
The problem is, I have an asp button on my aspx page with onclick="ExportButton_Click", and when the button is clicked, it calls the ExportButton_Click codebehind (server side) method. My problem is, I want to get rid of the asp button, because I am trying to create a button dynamically, which when clicked, will do the same thing. Right now, my dynamically created button is calling the doPostBack javascript function which targets the asp Button which triggers ExportButton_Click. So... is it possible to call the ExportButton_Click codebehind method with __doPostBack, without having an asp button? Thanks in advance.
Pass the event target argument in to the __DoPostBack('myEvent') method, like that.
Then in your code-behind Page_Load(), have somewhere code like this:
if (Request.Form["__EVENTTARGET"] == "myEvent")
{
//call your button click function, and pass the button to it (can pass null as the EventArgs)
Button1_Click(Button1, null);
}
These two methods are your server-side friends:
this.Page.ClientScript.GetPostBackEventReference
this.Page.ClientScript.GetPostBackClientHyperlink
They can be used to generate javascript to link to your server side events.

How to share html component amount different page

In ASP.NET, I have a master page, and five other pages which are under master page.
In all five pages, I need to implement a button, after clicking it, will generate different download results.
My original implementation is, in all five page, will all have a "Button", so in code behind will have corresponding event method to generate the download.
But my approach, will have duplicated html compoents.
Is there a way I can define the download button in my master page, when clicking button in different page, event method from different page will be invoked?
Yes you can, on master page you can have a Download Button and on it click check the URL, which will help you to identify the request has come from which page. Then you can write code on the basis of current page being displayed.
You can use the SHTML instead of HTML and include it every where that you need it.
I don't know what exactly You want to achieve, but maybe it would be better to have one aspx with the button and load the correct ascx file to this page according to some criteria? Each ascx can derive from some interface with some method, f.e. Click(), and each time the button on aspx is clicked, the Click() method from currently loaded ascx can be called.

Categories

Resources