Disable Button Click after clicking - c#

I am uploading large file to Amazon S3 with Progress Bar, I am displaying Jquery progress bar, following This
Every thing is working fine, but there are two problems
If during processing user Select another file or Click Upload button multiple times, the progress bar count get increased
I have disabled FileUpload Control on button upload click event, but its not disabling the button upload control
This is How I am doing:
protected void btnupload_Click(object sender, EventArgs e)
{
if (FileVideoUpload.HasFile)
{
FileVideoUpload.Enabled = false;
btnupload.Enabled = false;
System.Threading.Thread.Sleep(8000);
//Upload File on Amazon S3
lblmsg.Text = "Video uploaded Successfully";
FileVideoUpload.Enabled = true;
btnupload.Enabled = true;
}
}
I also tried this,but still button is not disabling
protected void Page_Load(object sender, EventArgs e)
{
btnupload.Attributes.Add("onclick", " this.disabled = true; " + ClientScript.GetPostBackEventReference(btnupload, null) + ";");
}
Also I have tried disabling it in JS.
I want to disable both controls while processing.

Try this with Jquery :-
$('#FormId').one('submit', function() {
$(this).find('input[type="submit"]').attr('disabled','disabled');
// find input type submit button or any other buttons which u want //
});

try this if #btnupload is in fact the button that you want to be clicked and disabled. I didn't quiet get that.
$('#btnupload').on('click',function(){
$(this).prop('disabled',true);
});
and later for enabling it again use:
$('#btnupload').prop('disabled',false);

you can disable it on click event
$('#btnupload').on('click',function(){
$(this).prop('disabled','disabled');
});

Related

How can i add close button to toastr.js in asp.net web form?

I'm beginning in asp.net and want to use the toastr.js for show user any message,for that purpose download the toastr.js and in my web form in submit button write this code:
protected void Submit(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(this.GetType(),
"toastr_message", "toastr.error('There was an error', 'Error')", true);
Div3.Visible = true;
}
show me message correctly,but message box not hide,and I want add close button that message box ,when user fire the close button,message start unhide with fade effect.How can I solve that ?thanks.
Adding the closeButton option should do the trick https://github.com/CodeSeven/toastr#close-button:
Page.ClientScript.RegisterStartupScript(this.GetType(), "toastr_message", "toastr.error('There was an error', 'Error', { closeButton: true })", true);

Testing to verify that a button is clicked first before another is clicked "Webfomrs"

I used an if statement saying that if lblTotalAmount is populated then you can be able to click the second button. Because if lbltotalamount is populated then the first button was clicked to populate it. However, with my code below it works by showing the error message if you try to click the second button before the first button but then if i do it in the correct order it will not redirect me to the page i stated below. How can i correctly state this so that it will work?
protected void btnSubmitOrder_Click(object sender, EventArgs e)
{
if (lblTotalAmount == null)
{
Response.Redirect("~/Default.aspx");
}
else
{
lblMessage.Text = "Please click the Calculate Order Total button first";
}
}
You should be trying to validate on the Text property of the lablel instead
protected void btnSubmitOrder_Click(object sender, EventArgs e) {
if (string.IsNullOrEmpty(lblTotalAmount.Text))
{
Response.Redirect("~/Default.aspx");
}
else
{
lblMessage.Text = "Please click the Calculate Order Total button first";
}
}

How can I invoke a server-side event from jQuery (Sharepoint 2010)?

In my Sharepoint 2010 app, I'm handling most events client-side with jQuery. However, for the saving of data to the Sharepoint list, and the generation of a PDF file with that data, I want to handle that server-side, with C#.
I tried to invoke a C# event this way:
0) Aded an HTML button in my project's *.ascx file:
<button type="button" name="saveData" id="saveData">Save Data</button>
1) Added jQuery to respond to that button being clicked (in the same *.ascx file):
$("#saveData").click(function () {
$('#hiddenSave').trigger('valuechanged');
});
2) Created a hidden element in the server-side C# (in the *.ascx.cs file):
HiddenField hiddenSave = null;
protected void Page_Load(object sender, EventArgs e)
{
base.OnPreRender(e);
hiddenSave = new HiddenField();
hiddenSave.ID = "hiddenSave";
hiddenSave.ValueChanged += new EventHandler(hiddenSave_ValueChanged);
this.Controls.Add(hiddenSave);
}
protected void hiddenSave_ValueChanged(object sender, EventArgs e)
{
GeneratePDF();
}
private void GeneratePDF()
{
;//bla
}
But I never reach the "ValueChanged" event handler; $("#saveData").click() fires, but not hiddenSave_ValueChanged().
So do I need a tweak to this, or a completely different approach? How can I do as much as possible client-side with jQuery, but also run server-side/C# code where necessary, in a Sharepoint 2010 app?
UPDATE
A little more detail, and about additional things I've tried: I'm creating a button on a Sharepoint page dynamically (in C#) in my *.ascx.cs file:
Button btnSave = null;
protected void Page_Load(object sender, EventArgs e)
{
base.OnPreRender(e);
this.Controls.Add(new LiteralControl("<br />"));
btnSave = new Button();
btnSave.ID = "btnSave";
btnSave.Text = "Save the Data";
btnSave.Click += new EventHandler(btnSave_Click);
btnSave.Visible = false;
this.Controls.Add(btnSave);
}
protected void btnSave_Click(object sender, EventArgs e)
{
btnSave.Text = "You clicked me!";
PostTravelData ptd = new PostTravelData();
}
I set it visible at the right time in the client-side jQuery in the *.ascx file:
$('#btnSave').show();
However, clicking the button does not reach the btnSave_Click() event - the breakpoint there is never reached, nor is the button's text changed. Why not?
Even when I don't set the button invisible (comment out the "btnSave.Visible = false;" line), the click handler isn't reached...is Page_Load() too late? Is there an earlier page event I can use that would work?
I tried moving it from Page_Load() to OnPreRender(), too, like this:
protected void OnPreRender(EventArgs e)
{
this.Controls.Add(new LiteralControl("<br />"));
btnSave = new Button();
btnSave.ID = "btnSave";
btnSave.Text = "Save the Data";
btnSave.Click += new EventHandler(btnSave_Click);
//btnSave.Visible = false;
this.Controls.Add(btnSave);
}
...(and OnRender()) but the button doesn't even display...
And, trying a different tack, I commented out the dynamic creation server-side code and tried to attach to a button created in the HTML (*.ascx file):
<button type="button" name="saveData" id="saveData" runat="server" onclick="saveData_Click">Save Data</button>
(by adding the "runat server" and the onclick handler), and then adding this "code-behind" (*.ascx.cs)):
protected void saveData_Click(object sender, EventArgs e)
{
PostTravelData ptd = new PostTravelData();
SaveToList(ptd);
GeneratePDF(ptd);
}
...but there was still no joy in Mudville -- the breakpoint in the handler is not reached.
Yet another attempt was:
In the *.ascx:
<asp:Button runat="server" id="saveData" name="saveData" onclick="saveData_Click" Text="Bla" />
In the code-behind:
saveData.Click += saveData_Click;
The "Bla" button is created, but clicking on it reaches not the breakpoint in the "saveData_Click" handler.
I even adapted some code from here like so:
Button btnSave = null;
. . .
protected override void CreateChildControls()
{
btnSave = new Button();
btnSave.Width = new Unit(150, UnitType.Pixel);
btnSave.Text = "Can you see me?";
btnSave.Click += new EventHandler(btnSave_Click);
Controls.Add(btnSave);
}
...but I still do not reach the "protected void btnSave_Click(object sender, EventArgs e)" handler when I click the button.
Surely there's a way to get a handle on the button server-side and manipulate it (specifically, respond to its click event)...?!?
First of all, as far as I know, there is no such event for input type hidden in a ascx page. If you create the input type hidden with runat server in the ascx code then you'll see that when you try to add this event it's not available. However there are other events like OnClick that you can simulate to get the desired result.

Clicking a button: disabling and executing the button's handler

On the Button's Click event, ow can you disable the button, and also call its event handler?
I have tried this:
protected void Button1_Click(object sender, EventArgs e)
{
Button1.Enabled = false;
}
This doesn't work in IE, Mozilla, or Chrome.
You could try adding this :
protected void Button1_Click(object sender, EventArgs e)
{
// Do some stuff here
Button1.Attributes.Add("onclick", "javascript:return false;");
Button1.Enabled = false;
}
<asp:Button ... OnClientClick="disableButton(this);" />
<script type="text/javascript">
function disableButton(button)
{
//need to delay a few ms so that the button posts back before being disabled
var buttonToDisable = button;
setTimeout("reallyDisableButton()"), 2);
}
function reallyDisableButton()
{
buttonToDisable.disabled = true;
}
</script>
I assume that your problem is that people are clicking a button several times (because the page it taking a while to load), thus kicking off the event several times and you only want them to be able to do it once. This will disable the button after it posts back preventing accidentally sending multiple concurrent postbacks.

linking to a radiobutton value, data is not binding, asp.net c#

hi i need to modify my code a little bit. i have a page with a radio button list and a textarea. the textarea is populated when a users makes a radio button selection.
also, when a user makes a radio button selection the url will hold an extention in the url to show which selection index number they have selection. (i.e. ?selected=0)
http://test.com/frm_Articles.aspx?selected=0
http://test.com/frm_Articles.aspx?selected=1
http://test.com/frm_Articles.aspx?selected=2
that way they can copy the url and reference it in other websites as a link. or place it in their favorites.
the problem is, if you grab the url and open a new browser, the page does not pass the value and databind accordingly. no radio buttons or content appear on the page.
must be the postback logic i think???
what's working:
when i launch the website the radio buttons appear and index 0 is set
when i select radio buttons the correct data displays and urls linking to radio button values display in browser (i.e. http://test.com/test.aspx?selected=2)
if i cut and paste pointer urls within the same browser then correct data is rendered
what doesn't work (everything that deal with an false PostBack):
1.when i launch website no data within the textarea apprears even though the radio button is set to 0 index and is visiable.
2. if i cut and paste pointer url into a new browser, text area and radio buttons do not display.
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
int selected;
if (Request.QueryString["selected"] != null)
{
if (int.TryParse(Request.QueryString["selected"], out selected))
{
RadioButtonList1.SelectedIndex = selected;
RadioButtonList1.DataBind();
}
}
else
{
int firstart = 0;
RadioButtonList1.SelectedIndex = firstart;
RadioButtonList1.DataBind();
}
}
}
protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
//
}
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
try{
e.Command.Parameters["#URL_FK"].Value = Session["URL_PK"];
}
catch (Exception ex)
{
}
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
string strRedirect;
strRedirect = "test.aspx?selected=" + RadioButtonList1.SelectedIndex;
Response.Redirect(strRedirect);
}
}
In your code at Page_Load event before this line
RadioButtonList1.SelectedIndex = selected;
you should bind RadioButtonList1. after binding RadioButtonList you can set SelectedIndex.
my SqlDataSource1_Selecting method was the issue. i used another approach and my code worked.

Categories

Resources