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!
Related
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 !");
}
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
I have a RadioButtonList with 2 items. I want a postback when any of the items is selected. I have added a confirm box for one item of RadioButtonList. But, when I click OK in the confirmbox, there is no postback and SelectedIndexChanged event is not getting fired.
AutoPostback property of RadioButtonList is set to true.
This is a code fragment from my Page_Load method:
RadioButtonOpenRestricted.Attributes.Add("AutoPostBack", "True");
RadioButtonOpenRestricted.Items.FindByValue("Open Access").Attributes.Add("AutoPostBack", "True");
RadioButtonOpenRestricted.Items.FindByValue("Open Access").Attributes.Add("OnClick", "javascript:return confirm('Are you sure?');");
Earlier, I had added confirm box for entire RadioButtonList and postback was working as expected. But I want the confirm box to be displayed only when user clicks on "Open Access" item.
Please help!
I tried a few things. The new code lines look like:
RadioButtonOpenRestricted.Items.FindByValue("Open Access").Attributes.Add("OnClick", "javascript:showConfirmBox(0,'" + RadioButtonOpenRestricted.ClientID + "')");
RadioButtonOpenRestricted.Items.FindByValue("Restricted Access").Attributes.Add("OnClick", "javascript:showConfirmBox(1,'" + RadioButtonOpenRestricted.ClientID + "')");
The javascript method is:
function showConfirmBox(i,id)
{
if(i==0)
{
if (confirm("Are you sure you want to provide Open Access? All existing individual permissions will be removed!")==true)
{
var ctrl1 = document.getElementById(id + "_0");
ctrl1.selected=true;
}
else
{
var ctrl2 = document.getElementById(id + "_1");
ctrl2.selected=true;
}
}
if(i==1)
{
var ctrl2 = document.getElementById(id + "_1");
ctrl2.selected=true;
}
}
The problem with this code is that it is treating both OK and Cancel as same. The confirm box is getting displayed but if-else part of the javascript method is not getting called. I tried using OnClientClick also...this doesnt even display the Confirmbox.
Help!!!
This is because your on click script does not play well with auto-post back script generated by the ASP.NET. A quick hack solution can be
RadioButtonOpenRestricted.AutoPostBack = true;
RadioButtonOpenRestricted.Items.FindByValue("Open Access").Attributes.Add("OnClick", "if (!confirm('Are you sure?')) return false;");
Although, this will still give you an issue when you cancel on your confirmation box (in such case, you have to add script to select the previous radio button again).
As such I am not a great fan of radio button list - you may consider alternate mechanism such as repeater with radio button in item template and having your own java-script for confirmation.
I think you want to use OnClientClick to show a "confirm" window.
I don't think you can have a javascript confirm window perform a postback, at least not the way your code is set up.
You should set the OnClientClick to show a confirm modal or window with an <asp:Button/> and have that button perform the postback your looking for.
Khushboo, it used to happen with me many times. The reason behind that was I was missing some closing tag somewhere in my aspx page. I used to copy my whole aspx page to some other text editor and paste all the elements one by one to my aspx page. It always solved my this problem. I am sure you must be missing some closing tag, please cross check all elements.
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>
I have a question about creating and managing events inside an ascx custom web control.
I have created a very stupid control consisting in a div containing a asp:Label control, it is a very simple structure:
<div id="mydiv" runat="server">
<asp:Label id="mylabel" text="Text"... />
</div>
That is, very simple.
I would like to add an event: clicked. I want to let the user add this control on the page and attach handlers to this event so that when this control is clicked it is possible to do something. It might seem a strange solution, it's like i'm trying to invent again the button control, that is: my custom button.
Well to fire the event I would like to add a javascript in my div and call a js function that calls, using ajax mechanism, a server side function.
Well how to call a server side function from here. I posted a question about how to call a server side function from a client side one and got some answers (many of them told me to use PageMethods), well it seems that pagemethod does not work, it compiles but when running and clicking on my control and executing the js (in the line PageMethods.mymethod()) here I have an error --> Java script exception: unrecognized method. It seems not finding the PageMethod.
Well, considering my objective, how can I do?
Ah, a solution like: use the click event in the label is not what I want because the click event must fire when I click in the div, consider that I might set a large padding so that a large empty space can provide a large clickable area.
Thanks in advance.
Create a Event in your User Control
E.g: -
public event EventHandler<CustomEventArgs> Click;
and a handler
public void OnClick()
{
if(this.Click !=null)
{
//what ever else you do
this.Click(new CustomEventArgs(...));
}
}
Also handle the mouse click (mouseleftbuttonup) on the User Control (and fire your event from there). i.e.
protected void OnMouseDown(...){ this.OnClick(); }
From the ASP.NET page, you can register the user control event and handle it:
MyControl.Click += new EventHandler<CustomEventArgs>(myctr_click)
protected myctr_click(object sender, CustomEventArgs e){
//do anything
}
PageMethods uses a web service within the page class; I assume you want a postback to the server and process this click in the page, right?
All controls that postback use a __doPostBack('clientid', '') method to postback to the server. Your label would need to do the same. Within the control, the control needs to implement IPostBackEventHandler to process these events, and raise the click event.
Check out this example: http://www.myviewstate.net/blog/post/2009/05/14/Implementing-IPostBackEventHandler-in-an-ASPNET-Control.aspx
I may have misinterpreted your requirements, but can't you just wrap the div in an asp:linkbutton?
<asp:LinkButton ID="lnkSomething" runat="server" OnClick="lnk_something_Click">
<div id="mydiv" runat="server">
<asp:Label id="mylabel" text="Text"... />
</div>
</asp:LinkButton>
and then on the server side:
protected void lnk_something_Click(object sender, EventArgs e)
{
}
As yellowfrog mentioned in the comments, you could then extend this by further wrapping it in an update panel for an async postback.