Refresh a button - c#

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

Related

ASP.NET and C# Redirect

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.

how recognize Html Link (anchor) in page_load after click?

i have some anchors in my aspx page.
i need to determine them (couse to run page_load) in page_load after click.
as you know those anchors are not regular asp.net controls and when you click them Page.IsPostBack is always false.
i can not use linkbuttons for some reasons.
so, how can i determine those anchors in page_load after click?
thanks in advance
Well, my only idea is tu use parameters in url and use them to identify which hyperlink was clicked.
Link 1
Link 2
And in code behind
string linkName = Request.QueryString["linkName"];
if (linkName = "link1")
{ // something
}
But what's the reason you cannot use LinkButtons or some other controls? This approach would be more convenient in ASP.NET.
A more elegant way would be to use __doPostBack function(It's already there in every asp.net page) in javascript and set appropiate
event targent and event argument. This is how asp.net controls posts back to server
for example.
<a id="LinkButton1" href="javascript:__doPostBack('Anchor1','')">LinkButton</a>
On the server Side, you could handle the click event as following
protected void Anchor1_Click(object sender, EventArgs e)
{
Response.Write("Hello World !");
}

Javascript for running code-behind code when a button is clicked

i have a popup that is getting displayed when Save button is clicked. The popup has 2 buttons. Yes and No. No should cancel the popup
and yes should take you to function in the code-behind say, btnSave_Click(object sender, Eventargs e). How is it possible. Could someone help me, i am new to Javascript.
Below is the code where i am showin the popup.
var mdlPopup = $find('<%= ModalPopupExtendersavechanges.ClientID %>');
if(mdlPopup)
{
mdlPopup.show();
}
To do this you will need to set your server side function as a web method like this:
Add to the top of your code behind:
using System.Web.Services;
using System.Web.Script.Services;
Then decorate your method with these attributes:
[WebMethod(), ScriptMethod()]
public static void btnSave_Click(Object sender)
{
//Stuff
}
To call this from the client side (Javascript) do this:
PageMethods.btnSave_Click(this,btnSave_Click_Finished);
You can place that in a client click event. The first argument is the sender parameter, the second is the javascript function to call when the server side method has completed.
You can't call server side code from JavaScript directly. Make a postback or fire a XHR (AJAX) request in the background.
I think it is possible for you to acess the serverside script by using javascript.
__doPostBackis a function which is behind the asp postback and this function is called when a button is clicked or dropdown value is changed.
For more details pls refer to [this.][1]
All you need is to place a <asp:Button ID="btnSave" runat="server" onClick="btnSave_Click" /> in the form and in the javascript (ie button click function) just call the __doPostBack('<%= btnSave.UniqueID %>', '');
So it will calls the serverside function.(btnSave_Click)
Notice that you need to give '<%= btnSave.UniqueID %>' as the firstargument.
The above will works as similar to a server button click
The another way is to make a post or ajax using jquery.post or jquery.ajax it is possible to send request asynchronously to the server.Here you want to pass some query string and call the appropriate function in the page_Load
This will do without any postback
One another method is to use PageMethods from clientside by defining a static WebMethod
[1]: http://msdn.microsoft.com/en-us/library/aa720099%28vs.71%29.aspx/"Call serverside function from clientside"
I hope any one of these will solve your problem
the Yes button should be an asp.net control with a server-side handler
markup
<asp:button id="yesButton" runat="server" onclick="yes_click" />
codebehind
void yes_click(object sender, EventArgs e) {
// TODO your thing here.
}
the other buttons can be standard html inputs with javascript event handlers. if you are using javascript to alter element style, the changes won't persist across a postback- when the yes button submits and the page reloads, the popup won't be visible.

JQuery: how to keep a dialog box open after an asp.net postback has occurred

Looking at the other dialog questions, mine seems to be the exact opposite to postback queries. I have a dialog box which opens when a button is clicked. all that is fine. I have attached the dialog to the form and posting to asp.net is fine too, but, my problem is when a postback occurs of course the dialog closes which I do not want to happen as the user may want to post via the dialog function several times. Basically the dialog connects to an asp process that creates a folder, the user may want to create several folders (for image upload) but postback causes the dialog to close, rather abruptly!, and I would rather the user dismissed the dialog when they have finished. Any ideas how I might achieve that? I'm using Jquery and Asp.net C#. I'm fairly new to both c# and Jq so am flummuxed. I've tried this lastly........... Thanks
protected void Page_Load(object sender, System.EventArgs e)
{
if (IsPostBack)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "callme", "$('#opener').click(function(x)", true);
}
}
I'd suggest looking at using an AJAX post to call your 'Create Folder' function, that way you can separate the postback from the currently displayed page and maintain your state there.
You might want to consider using AJAX to post the data from the dialog box. Otherwise, you could check for Page.IsPostback and open the dialog if true.
You could trigger the .click() event on your #opener object on Postback:
<script type="text/javascript">
$(function() {
<% if (Page.IsPostback) { %>
$('#opener').trigger('click');
<% } %>
});
</script>

Button disable function after clicking on it

i have save button in around 150 pages. when ever the user clicks on save button i should disable the button after the click. so that user does not keep clicking on save button again.
protected void Button1_Click(object sender, EventArgs e)
{
// right now once the user clicks the save button
Button1.Enabled = "false"
}
right now i am doing like this. is there any better solution u can provide me to improve codeing here would be great.
thank you
I think the best option to accomplish this is using javascript. If you are using jQuery (which I can even start to recommend enough) then you can put the javascript in your masterpage. You just need to find a way to create a selector for your save buttons. Like this:
$(document).ready(function(){
$('.saveButton').bind("click", function(e) {
$(this).attr("disabled", "true");
return true; //causes the client side script to run.
});
});
In this example, I assumed that all the save buttons would have the css class ".saveButton", but you can find your own way to select the button.
Cheers!
You can also try disabling it via Javascript.
You need to use JavaScript to alter the link once it's clicked to prevent future click handling, before allowing the page to proceed with the postback. A naive attempt would be,
<asp:LinkButton runat="server" id="button" OnClientClick="this.href='#'">
Click - doesn't quite work
</asp>
This successfully prevents successive clicks from triggering the postback, but it also prevents the first click from triggering the postback. A little more logic is required to make this work correctly. In the codebehind, I grab the actual postback JavaScript snippet and work it into some logic.
protected void Page_PreRender(object sender, EventArgs e)
{
button1.OnClientClick = string.Format(
"if(this.getAttribute('disabled')) return false; {0}; this.setAttribute('disabled','disabled'); return false;",
Page.ClientScript.GetPostBackEventReference(button1, null));
}
In the HTML template:
<asp:LinkButton runat="server" id="button" >
Click - does not allow multiple postbacks
</asp>
You can spruce this up with some CSS, and at that point I would advise including jQuery, so your code is more concise (due to its command chaining).
What you have to do is use some JavaScript and disable it when its clicked on the client side.
That click event fires on a postback.
Simplest way of attaching javascript to disable the button after user click would be this (in page onLoad event):
myButton.Attributes.Add("onclick", " this.disabled = true; " + ClientScript.GetPostBackEventReference(btnSave, null) + ";");
But if button is repeated on many pages, you can maybe create nested master page and put save button there, or inherit you page from class that implements this disable functionality. Also, you can "scan" for control you want to disable after click!

Categories

Resources