On the click of the button my Onclick event gets fired.
Inside the onclick event I generate somefile at runtime and render it to the browser in the following way. But before rendering it to the browser I am making particular Label visible. But still the Label never become visible. Any idea what is the problem
lblInfoMessage.Visible=true;
Response.ContentType = "text/plain";
Response.AppendHeader("Content-Disposition", "attachment; filename=test.gxml");
doc.Save(Response.OutputStream);
Response.End();
You can either refresh the page (and thus change visibility or contents of controls) or send an attachment. Not both. So you will have to find some other way, maybe client side javascript?
EDIT
In the button you need a OnClientClick in the server side code, this will translate into a client-side "onclick". Here you can call a javascript function where you can (for instance) display some text. Note that this function is executed before the submit action that will generate the file.
Something like this in the html/aspx:
<span id="infoMessage"><!-- empty --></span>
...
<asp:Button OnClientClick="showInfo()" ... />
...
<script type="text/javascript">
function showInfo() {
document.getElementById("infoMessage").innerText =
"This is the info message.";
}
</script>
You can't just show that Label that you have now, as that doesn't exist in the client-side html when it is invisible.
should it not be as below?
lblInfoMessage.Visible=true;
Related
I have this page where you can "edit" the data of the users... I want to send a message like "Edited Successfully" AND update the page as well, with the new content ( DropDownLists AND TextBoxes).
I'm using this for the messages:
ScriptManager.RegisterStartupScript(this, typeof(Page), "alert", "alert('Edited Successfully !');", true);
And to update the page, I tried: Response.Redirect(Request.RawURl) and even a simple Response.Redirect(~/page.aspx) didnt work either...
If I try this way, it do update the page, but then it DOES NOT show the alert... ;\
All of this data (that fills the DropDownLists, Textboxes etc..) Is called on the Page_Load. I tried to call this same method after sending the message, but then it does not update =\
Any Idea?
The problem you're facing occurs because you are reloading the page, so the script you register gets lost.
An approach I've used in cases like this, involves the use of jQuery and jQuery UI Dialog:
In your page, put a div that will be the message container. It's hidden initially and will be shown after complete the database request:
<div id="modal" title="Alert" style="display: none;">
</div>
Write a javascript function that will display the dialog:
function showConfirmation(text){
$("#modal").html(text).dialog({
modal: true, // show the dialog as modal
buttons: {
Ok: function() {
location.reload();
}
}, // add a button that refreshes the page when clicked
closeOnEscape: false, // avoid the dialog close when ESC is pressed
dialogClass: 'no-close' // adds a CSS class that hides the default close button
});
}
The dialog function is responsible for showing the dialog, using the jQuery UI library. The buttons parameter displays a button that refreshes the page when pressed.
All you have to do is register the dialog script, with the RegisterStartupScript method:
ScriptManager.RegisterStartupScript(this, typeof(Page), "alert", "showConfirmation('Edited Successfully !');", true);
If you are not using jQuery and/or jQuery UI, all you have to do is add the references in head tag. If you don't want to use the CDN's, download the files to your local site.
<script type='text/javascript' src='http://code.jquery.com/jquery-1.9.1.js'></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
You can see a working example of the client side behavior this fiddle
I have a RadPanelBar as such...
<telerik:RadPanelBar
ID="ResourcesSubMenuRadPanelBar1"
Width="195px"
OnItemClick="RadPanelItemClick"
ExpandMode="MultipleExpandedItems"
OnClientItemClicked="RadPanelClientItemClicked"
OnClientLoad="RadPanelBarClientLoad"
runat="server"
AppendDataBoundItems="true"
EnableEmbeddedSkins="false"
OnClientItemCollapse="RadPanelClientItemClicked"
OnClientItemExpand="RadPanelClientItemClicked">
</telerik:RadPanelBar>
This all works as expected, except for one little thing. In the code behind, I explicitly set the NavigateUrl property to string.Empty but when an item is clicked, it adds a hash to the url. Obviously, this is because the href attribute has been set to "#" when the control renders the HTML.
I know that I can simply return false from the OnClientItemClicked event, but that will stop the ItemClick event from being fired on the server.
As I say, there is no real error with this code it's just bugging me (and, more importantly, the end users) that there is a # added to the URL.
Does anyone know how to stop this happening?
Try this in your OnClientItemClicking event:
eventArgs.set_cancel(true);
Ref: http://www.telerik.com/help/aspnet-ajax/panelbar-onclientitemclicking.html
And, if in case you want the post back to happen, I suppose there is a item.PostBack property (server-side). Set it to true. It should post you back - if the NavigateUrl is empty (or #).
Compatible in just about every browser, IE9 and up:
Javascript (no jQuery):
stripTelerikHashtag = function () {
[].forEach.call(
document.querySelectorAll(".rpLink"),
function (a) { a.removeAttribute("href") }
);
};
Javascript (with jQuery):
stripTelerikHashtag = function () { $(".rpLink").removeAttr("href"); };
In your ASP, set OnClientLoad on the RadPanelBar to stripTelerikHashtag.
I have a image button in a page which can be triggered on mouse click, by default it gets triggered on enter press also which i want to disable.
I know about "UseSubmitBehaviour" attribute in asp:Button, is there a way to do the same in asp:ImageButton?
I will assume you have some sort of input controls and you don't want an enter keypress to auto submit when a user accident hits the enter key. If so you can attach a javascript onkeypress event to each control that you want to disable this behavior for.
function disableEnterKey(e)
{
var key;
if (window.event) key = window.event.keyCode; // Internet Explorer
else key = e.which;
return (key != 13);
}
// In your aspx file Page_Load do the following foreach control you want to disable
// the enter key for:
txtYourTextBox.Attributes.Add("OnKeyPress", "return disableEnterKey(event);");
If you need to disable the Enter key submitting form completely. case use the OnKeyDown handler on <body> tag on your page.
The javascript code:
if (window.event.keyCode == 13)
{
event.returnValue = false;
event.cancel = true;
}
With JQuery this would be much cleaner, easier and the recommended method. You could make an extension with:
jQuery.fn.DisableEnterKey =
function()
{
return this.each(function()
{
$(this).keydown(function(e)
{
var key = e.charCode || e.keyCode || 0;
// return false for the enter key
return (key != 13);
})
})
};
// You can then wire it up by just adding this code for each control:
<script type="text/javascript" language="javascript">
$('#txtYourTextBox').DisableEnterKey();
</script>
If you put your content in a asp:Panel you can use the DefaultButton Property to set a different button as the default so your image button wont be clicked.
<asp:Panel runat="server" ID="pnl_Test" DefaultButton="btn_Test2">
<asp:ImageButton runat="server" ID="btn_Test1" />
<asp:Button runat="server" ID="btn_Test2" />
</asp:Panel>
In this example the btn_Test2 will be clicked when you hit enter where normally btn_Test1 would be clicked
Use Kelsey's Answer (This answer is wiki'ed)
...but please note a few things when you implement it.
I'd recommend the plain old javascript method if you're not already using jQuery. if you do that method, just return (keynum != 13) don't do something silly like if(true) return true; else return false;
You don't have to assign onkeydown from the code behind. That can be done in the markup and it's a lot cleaner when you do.
Don't disable the enter key in your entire form. You can do it in your inputs only, but if you do it in the entire form you won't be able to add a carriage return in a textarea.
If you do use jQuery, I'd recommend adding a CSS class called "disableEnterKey" and assigning it to your form elements you want to disable, then calling Kelsey's jQuery method on $(".disableEnterKey") in the document ready.
Don't answer too similar to anyone on SO, even if you don't fully agree with the answer. And even if the answer was simple and thousands of people probably have done the samething. It's "copying". Which is similar to being a "cutter" or a "tattle tale"... which is bad.
(this answer has been community wiki'ed as this question thread has gotten silly)
Use an asp:image instead. Then place some javascript code in the onclick "javascript:document.getElementById('imageClicked').setAttribute('value', 'true'); document.myform.submit();"
Set a hidden field's value (using javascript) to tell the server side code that the image was clicked.
document.getElementById('imageClicked').setAttribute('value', 'true');
Then, at the end of handling the postback on the server reset the hiddenField's value:
document.getElementById('imageClicked').setAttribute('value', 'true');
The form will execute the first button it finds on the page when you hit enter. If you can move the ImageButton further down the page so it's no longer the first button in the markup, and use CSS to position it properly, this should fix your issue. I fixed the same exact thing last week and this worked for me. I went with this solution because it didn't require JavaScript to work properly.
you want something like
<form ...>
<!-- some code here -->
<button style='display:none' onclick='return false'>here comes the magic</button>
<button>normal button </button>
</form>
See the following link.This can be solved for all default button submit problem.
http://weblog.kevinattard.com/2011/08/aspnet-disable-submit-form-on-enter-key.html
Please Use This Code
<script type="text/javascript" language="javascript">
$(document).ready(function {
$("#<%=imageClicked.ClientID%>").prop('disabled',true)
});
</script>
Is it possible to pass the contents of a textbox or Listbox into the URL portion of the javascript code window.open(URL)? I have an asp.net listbox control that displays URL values. When ever an end user clicks on another listbox, the URL listbox provides the specific URL. I am trying to pass this URL into the above javascript code, but I do not know the correct syntax to do this. This code will execute as an onclick event.
For clarification, similar to typing “+ ListBox.Text.ToString() +” or ‘” & List.Text & “’” to add the content of a listbox into something else, such as a textbox. Is there specific syntax to do the same, but add the listbox.text into javascript?
Thank you,
DFM
Simply add a client-side onclick handler to your listbox as shown below:
<asp:ListBox id="ListBox1" runat="server" .....
onclick="openPopup(this)">
........
</asp:ListBox>
Then add the following javascript code:
<script type="text/javascript">
function openPopup(e){
window.open(e.value);
}
</script>
Sure, this should be pretty easy with jQuery. Obviously the URL generation could be reduced to a single statement, but should give you the general idea.
$(document).ready(function() {
$("your-element").click(function() {
var str = $("#listbox-id").val();
var url = "your-url.com/" + str;
window.open(url);
});
});
I have a button control. Once the user clicks on it, the click event should fire and then the button should get disabled. How can I do this? I have the option to use JQuery or JavaScript or both.
Here is my button declaration:
<asp:Button
ID="Button1"
runat="server"
Text="Click Me"
onclick="Button1_Click"
/>
On the button click code behind, I have added a Response.Write(). That should get executed and then the button should be disabled
For whatever reason, the HTML spec dictates that disabled elements should not be included in POST requests. So, if you use JavaScript to disable the HTML element in the client-side onclick event, the input element will be disabled when the browser assembles the POST request, the server won't be properly notified which element raised the postback, and it won't fire server-side click event handlers.
When you set the UseSubmitBehavior property to false, ASP.NET renders an input element of type button instead of the regular input of type submit that the ASP.NET Button control normally generates. This is important because clicking a button element does not trigger the browser's form submit event.
Instead of relying on a browser form submission, ASP.NET will render a client-side call to __doPostBack() within that button element's onclick handler. __doPostBack will raise the postback explicitly, regardless of what POST data comes through in the request.
With the postback being raised independent of the browser submit event, you're freed of the previously mentioned HTML quirk. Then, you can set an OnClientClick of "this.disabled = true;", which will render as "this.disabled = true; __doPostBack('Button1', '');", and things will work as intended.
add an OnClientClick="this.disabled = true;" to your button.
If you are using Asp.net Ajax you might want to look at using PostBack Ritalin.
Have you tried this?
Add an OnClientClick="MyFunction();" to your .NET button.
Then in the .aspx page script tags you add the following JS function:
<script type="text/javascript">
function MyFunction()
{
window.setTimeout(function ()
{
// get the button/control to disable using your favourite clientside ...
// ... control grabbing code snippet ...
// ... eg. JQUERY $('Button1'), getElementById, etc.)
document.getElementsByName('Button1').Button1.disabled = true;
// I've used "getElementsByName" because .NET will render a button with
// ... a "name" attribute, and not an "id" attribute, by default
}, 1);
}
</script>
This gives the browser a chance to post back, followed by a quick button disable.
You need to be careful that the postback occurs before you disable the button through client script. This is a common gotcha with ajax and input boxes. Disabling an input box prevents the data from being sent from the browser, even if you can see text within it while it is disabled. The answer is that you need to use jquery for this to ensure the server-side code runs first before it is disabled.
-Oisin
// to disable
this.setAttribute('disabled', true);
// to enable
this.removeAttribute('disabled');
this is a cross browser solution
There is really cool event for body tag "<"body onBeforeunload="buttonId.disabled = true;" ">"
This event triggers right before form submits, in other words your data will be submitted correctly.
When using the "this.disabled = true" method make sure you check if the page is valid before disabling the control if you have validators on the page. If validation fails you won't be able to re-enable the control without reloading the page.
if (Page_IsValid) this.disabled = true;
<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
function BeginRequestHandler(sender, args) {
document.getElementById('<%= lblMessage.ClientID %>').innerText = "Processing...";
document.getElementById('<%= btnSubmit.ClientID %>').innerText = "Processing";
args.get_postBackElement().disabled = true;
}
</script>
Add Script Tag in source page . change Id of button in code . You can disable the button till the process completes execution .
you can disable it server side
Button1.Enabled = false;