I'm looking to create a custom date picker with code examples from several sources.
Is the code to display/hide an ASP.NET control when a user clicks a button usually done with JavaScript or ASP.NET code? By display/hide, I mean within the page not a popup window.
Please provide a simple example. (If ASP.NET, VB example preferred over C#)
The answer is, it depends. Do you want the date picker show/hide to trigger a postback and thus some code on the server, or do you want it to act purely on the client?
If you want it to act purely on the client, then, modify the markup for your button:
<asp:Button runat="server" ID="myButton" OnClientClick="ShowHideCalendar()" Text="myButton" />
<script language="javascript" type="text/javascript">
var calendarVisible = false;
function ShowHideCalendar()
{
if (calendarVisible)
{
// Code to *SHOW* calendar here
// Show the DIV it's contained in, pop the window with it in, etc..
}
else
{
// Code to *HIDE* the calendar here
}
}
</script>
The key bit is the "OnClientClick" property of the asp:Button control.
Its best practice to do such thing asynchronously, rather than having a full postback that refreshs the entire page.
That means that you have two options:
Update an UpdatePanel in which your
control is placed. That gives you
the benefit of only re-rendering the
content in the UpdatePanel.
Use
clientside scripts to toggle the
control. You also need to perform a
callback, that tells your codebehind
that you just toggled the visibility
to asure your code is in the same
state as the webpage displaying it.
I'd prefer using the second one.
Related
I have a page some 2 ASP Tabs and with an UpdatePanel on the first tab. Inside the update panel (on page_load), I dynamically create a table that contains rows with cells for an image, some text, and an ASP:Button. I'd like the button, when clicked to switch tabs from the first to the second. Instead, all it does is refresh the updatepanel it resides in. How can I stop it from behaving that way? How do I get it to perform a function of my own design, instead of posting?
Thank you,
You can add javascript click event and stop it to do post back by returning false;
In Code behind, where you add button dynamically.
btnChangeTab.Attributes.Add("onclick", "return YourJavascriptFunction();");
In Client side.
<script type="text/javascript">
function YourJavascriptFunction()
{
//Your javascript code here
return false; //the will stop from postback
}
</script>
I'm using .net 2.0. This is a project that I have taken over for another developer.
I have a aspx page that can take a long time to display under certain condition due to loading items from the database. What I want to do is to show a loading animation or something to let the user know the page is loading, so I tried to use the JQuery .ready() method, however, I can only see the results after the page is fully loaded. What I mean is that when I click on a link to my aspx page, nothing is drawn until all of the work is done. The work is done on the server side in Page_Load.
I'm looking for best practices of having the page display, even if all the user sees is an animation. Right now it appears as if something is wrong because it can take a while (over 15 seconds in some cases) before the page draws.
You might be able to use Asynchronous Pages to do this
http://msdn.microsoft.com/en-us/magazine/cc163725.aspx
One way to take the page loading code out and place it in a image button with a transparent image.
Then wrap any sections of the page that will be updated in an update panel.
Once the page has loaded you can click the hidden button with jQuery to load the data, and the update panel will handle a loading icon if you have set up a progress template.
asp:
<asp:UpdatePanel runat=server id=upMain>
<ContentTemplate>
<asp:gridView runat=server id=gridView>
<asp:ImageButton runat=server id=hiddenLoadButton>
</ContentTemplate>
</asp:updatePanel>
in button click:
protected void hiddenbuttonclick(object sender, eventargs args)
{
gridview.DataSource = yourDataSource;
gridView.DataBind();
}
jQuery:
$(document).ready(function(){
$("#hiddenLoadButton").click();
});
Another option would be to use an ajax method to load the data.
In the flow, you are coming to the database call prior to the JQuery method passing all of its information to the page. There are two solutions to this, I can see:
Get the data via AJAX after the page load
Explicitly control the HTTP Response stream and flush after the JavaScript is sent
Without seeing the code, I cannot offer a specific answer to your issue.
How can i hide a control thats located on my aspx file from within the aspx.cs file?
I have a textbox and a button, and the idea is when i press the button both those controls disapear and a dynamic control appears.
Basicly a way to make it invisible would to place something in the click event of that button or is there a other way?
Or would there be a better way to sovle this without having to make the control actually "unvisible"?
Thanks.
In the event handler of your button's click event, set the desired controls Visible property to false. You could also accomplish this via jquery without a postback, i.e. OnClientClick.
EDIT
Ok, the css approach below will mean that your control still renders but will be made invisible by the browser but will still be there in the mark up. If you don't want to it render at all set the visible to false as other guys here have mentioned.
It's one of those where there are many ways to skin this particular cat and it really depends what you want to achieve :)
Original Post
You can change it's cssClass to one that contains display:none;
<style type="text/css">
.ShowMyControl
{
display:block;
}
.HideMyControl
{
display:none;
}
</style>
Then in your code behind you can flip the styles on the button_click...
MyControl.cssClass = "ShowMyControl";
Or
MyControl.cssClass = "HideMyControl";
What you may want to do though is place your controls in asp:Panel controls and then flip the cssClass of the Panel instead of on each control. That'll work too.
This is one way to do it based on the server side model you appear to be using, however, you may want to look at utilising AJAX calls & JavaScript to do the dynamic showing & hiding on the client side. This may not be appropriate though - it depends on your design pattern really
You can subscribe to a server-side Click event of the button and set the Visible property of your user control to false:
protected void cmdHide_Click(object sender, EventArgs e)
{
myCustomControl.Visible = false;
}
However this will cause in your custom control not to be rendered at all. Furthermore the page will be reloaded since you're using server-side event.
If you don't want the page to be reloaded then you can hide it using Javascript:
Markup:
<asp:Button ID="cmdHide" Text="Hide" runat="server" />
<asp:Panel ID="myPanel" runat="server">
<cust:MyCustomControl ... />
</asp:Panel>
Code-behind:
cmdHide.OnClientClick = string.Format("document.getElementById('{0}').style.display = 'none'; return false;", myPanel.ClientID);
-- Pavel
This is probably a simple question.
I use ASP.NET ajax toolkit and Jquery. I want to call a server-side function/method from Javascript and have it update a control. Can i do this?
Client-side
send_request(foobar,args);
Server-side
private void foorbar(){
Label1.Text = "updated";
}
Do you want it to fire a server-side method and update a server-side control on the page? You can create a ASP.NET UpdatePanel, let's say there is a button1 inside, and from your JQuery code, write this.
function OnClick()
{
__doPostBack(button1.ClientID, "argument")
}
and in your server side code, Page_Load event, you will find the EVENTTARGET and EVENTARGUMENT in the REQUEST variable, which contains the information you just postback, you can then update the control in the UpdatePanel itself as long as the control is within the UpdatePanel, it will be handled properly by ASP.NET AJAX.
More details here
http://www.dotnetspider.com/resources/16920-Post-back-gets-demystified-doPostBack-defined.aspx
Yes that can be done Client Callback
You could take a look at ASP.NET Page Methods.
jQuery.ajax({
url:'url to call', //usually webservices in asp.net
dataType:'json',
type:'POST', //(asp.net werbservices by default accepts POST request only)
success:function(data){
//do some thing with this data, like will dom elements etc
}
});
#smkngspcmn:
I placed everything inside an update panel and did something like $('#Year').change(function() { __doPostBack("submit", ""); }); That does do a full post back without Ajax. What am i doing wrong? Should i place the above script inside the update panel as well?
The first argument to __doPostBack() should be the UniqueID of a server-side control inside the UpdatePanel. For example, you can put a hidden button inside the UpdatePanel:
<asp:Button ID="HiddenButton" runat="server"
style="display:none" OnClick="HiddenButton_Click" />
When the button is rendered on the page, you can take the name attribute of the <input type="submit"> element that represents the submit button and use it for the first argument to _doPostBack(). In this way, whenever your script runs it the UpdatePanel will make an asynchronous postback and the HiddenButton_Click event handler will be fired.
I have a few controls that inherit from ASP.NET buttons and use onserverclick.
If the user clicks twice, the button fires two server side events. How can I prevent this?
I tried setting this.disabled='true' after the click (in the onclick attribute) via javascript, but that blocks the first postback as well.
See this example for disabling control on postback. It should help you do what you're trying to achieve.
http://encosia.com/2007/04/17/disable-a-button-control-during-postback/
You don't necessarily want to show the button disabled on postback. You want to make sure they don't accidentally submit twice. So disabling or hiding the button as a result of a server-side action is already too late in the game. By this point the 2nd request is already on it's way. You need to either do it with javascript or make sure your server side code won't run twice.
In case of an updatepanel and a button inside a FormView-Template I use the following approach:
// Using that prm reference, hook _initializeRequest
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(InitializeRequestBuchung);
// Abfangen von Mehrfachklicks auf Buttons für asynchrone Postbacks im Updatepanel
function InitializeRequestBuchung(sender, args) {
var arrButtonIds = ["ButtonInsert", "ButtonUpdate"];
// Get a reference to the PageRequestManager.
var prm = Sys.WebForms.PageRequestManager.getInstance();
if (prm.get_isInAsyncPostBack() & jQuery.inArray(args.get_postBackElement().id, arrButtonIds) > -1) {
args.set_cancel(true);
}
}
This cancels the following postback if an async postback is currently still active. Works perfectly.
Someone else said this somewhere on here a few days ago, and I concur - use javascript to simply hide the button instead of disabling it; you could show a "spinner" image in its place, which lets the user know what is going on.
Instead of hiding, what I have done is swapping buttons using javascript. Show another greyed out image on the click of the first button.
Set the Button property UseSubmitBehavior to false. Then create an OnClientClick function that disables the button.
It would look something like this:
<script type="text/javascript">
function disableFunctn(button){
button.disabled = true;
}
</script>
<asp:Button ID="button1" UseSubmitBehavior="false" OnClientClick="disableFunctn(this);"/>
fastest cheapest way:
<asp:Button ID="button1" UseSubmitBehavior="false" OnClientClick="this.disabled=true;"/>
You can also try for example btnSave.Enable = false; when the button is hit and before the processing for the button is done in the Click Event routine. If you need it to be reset to allow it to be enabled have a separate button that resets the button for reuse.
Another method is to set the button with verification so that the user is asked if they want to Save, it should pop up both times.
Yet another method would be to flag the first occurrence then set a popup for the second to verify a second or subsequent usage.