asp.net custom web user control -- button - asp.net

Is it possible to create a custom button -- web user control? I want certain java scripts to trigger when buttons are clicked. If so, are there any articles out there that explain the basics?
I completely understand that I can load javascript via .js link or dynamically at page load, but I would like to just drop a control on the page without manually adding code to every page on every one of my projects.

There is the IScriptControl interface you can implement. In MSDN are samples for that.
It will take care of aumatic script including from a resource whenever it is shown on a page.

Add a new Web User Control to your project by going to the menu item Project | Add New, then selecting Web User Control. Then just drop a button on it like this:
<asp:Button ID="Button1" runat="server"
onclientclick="alert("Javascript Here.")" Text="Button" />
You can then use that in your project. Replace the onclientclick with the javascript you would like to run.

Related

Need an asp.net control to click the button in another asp.net control

First time I have used the site. I appreciate the help I have gotten from the users before and hope to contribute as well as ask. Now, for the question.
I am developing a static toolbar that rests on the bottom on the webpage. The purpose is to display a couple of values to a user purchasing something and for the toolbar to update the information to reflect the choices. There is currently a box on the page that does this, but it rests at the top and the user would have to continuously scroll to see options appear, price changes, etc.
The values update just fine with the price in that box when certain check box buttons are clicked that auto-refresh the page (due to these check boxes changing pieces of the code). However, some things that can effect the price require a button on the page that 'recalculates' to be clicked. The information on this page is put on the page with a controller. The toolbar I am creating is a separate control.
What I am trying to do is have a button within the toolbar that will mimic the actions of the purchase button that recalculates. I tried doing this:
Code on control page for toolbar:
<input type="button" id="buttonID" class="buttonClass" runat="server" onclick="__doPostBack('<%= btnIDOtherControl.ClientID %>','OnClick');" value="Recalculate" />
Code on control with the purchase info:
<asp:ImageButton ID="btnIdOtherControl" runat="server" ImageUrl="~/image.jpg"
AlternateText="Recalculate" OnClick="recalcFunction_Click" />
That is just the current iteration. Before I did not have the 'OnClick' piece of the __doPostBack, I had ''. I also tried using a bit of jQuery. There are quite a few questions about firing an ASP.net button from javascript, but none seemed to work.
What happens is I have gotten it to a point where clicking the recalculate button in the toolbar actually DOES refresh the page, but none of the values change. If I click the recalculate button from the purchase page (where it exists normally), the value in the toolbar does not change even though the values on the page do. If I hit the recalculate button from the toolbar AFTER recalculating from the main page, the toolbar values reflect the regular values. Any other normal use of the page (such as the checkbox that auto-refreshes) will change the values correctly in both places.
If more info is needed just let me know.
Appreciate the help ahead of time!
This has to be done by using javascript or jQuery.
Use the code below:
$("#buttonID").click(function(){
$("#btnIdOtherControl").click();
});
This will trigger the button on other control to be triggered.
PS: Make sure that the ids are correct once the page is rendered in html as visual studio will append the ids of the button in the control to be (id of the control)_(actual id of the button) i.e. If on your page you have referenced control as
<user:control id="control" runat="server">
The id of your button (buttonID) on html will be control_buttonID, so you will need to use that (control_buttonID) in jQuery.
So your code will be
$("#control_buttonID").click(function(){
$("#control1_btnIdOtherControl").click();
});
On you other control make a public property with set only and call your button's click event in it, Then on your working control you have to call only that public property of other control. it should be like this :
public void PublicPropertyCallingEvent
{
set
{
Button1_ClickEvent(null,null);
}
}
You have to access this property only wherever you need.

Hide Checkbox on a particular page

I am working on ASP.NET application where I am reusing a user control. The user control contains a checkbox and bunch of other controls. I want to display all the controls inside the user control on all the pages but on one single page I want to hide the checkbox.
I was thinking that I can use the databind methods and see if I am on the "pagex" then hide the checkbox. Is there any other way to solve this problem?
If you have access to the code for the control you should be add a new property to the control for hiding/showing the checkbox and then just pass in the property depending upon what page you are on. You'd have to pass in the show/hide property on the load event of the page.
Do you have access to the code??
I would specify a parameter in the code behind for your ascx file. Example:
public bool HideCB = false;
THen when you put your User Control on your aspx page do this:
<uc:TestControl id="TestControl" runat="Server" HideCB="true" />
This way you can do a check on if(HideCB) to determine if you want to make it visible or not.

How to make Active a View inside Multiview in Clientside Javascript in Asp.net

I have three views inside a Multiview
How to activate one by one view means I Just want to change the Active View Index of the Multiview using javascript in ClientSide How can i do this?
I dont think you will be able to without causing a post back as the multiview is a server side control and only the active view is rendered to the browser.
You probably need to have all the views on the page and all but one hidden. Are you implementing some time of tab control?
There is a tab control in the ajax control toolkit which switches between views clientside:
http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/Tabs/Tabs.aspx

Visual Studio keyboard shortcuts for creating Event handler stubs

When you edit a simple page in the design view, you can add an event on most components by simply double-clicking the relevant event. This does the binding and generates the function declaration in the codebehind for you.
In larger projects where the pages are complex, the design view can be extremely slow. Is there a way to quickly generate the codebehind stubs directly from the .aspx source?
Just begin typing the code to create your component.
For example, this is the code to add a button to your .aspx file:
<asp:Button ID="ButtonAction" class="btn panel-black" runat="server" Text="" OnClick="Function_Name_Here" />
When you get to typing the function name for the OnClick method...
for example, this part of the code:
OnClick="Function_Name_Here"
... Visual Studio will display a little pop-up window with the option to "<create new event>".
Select this option and the codebehind event handler stub will be generated for you.
In the markup view the Properties window is still available: you may have it hidden.
When it's shown and the cursor is in the markup for a particular control, you get that control's properties and events like you would in Design view. Similarly, in the event tab you can double-click the event (i.e Click) to automatically generate an empty declaration in code and hook up the event without ever having to go into the Design view.
I've been able to do this with ReSharper, but I've been using it for so long that I don't recall whether Visual Studio has this capability natively.

asp.net FileUpload event after choice is made and before submit for upload

I would like to display the size of the file that was chosen via the Browse button of the FileUpload control.
Ideally, this value is displayed immediately after the user chooses the file but BEFORE the "Upload File" Button is clicked.
I have and on a webform. The Button looks like this:
<asp:Button ID="UploadButton" runat="server" onclick="UploadButton_Click" Text="Upload File"/>
The onclick event for the button control results in a postback and the file is uploaded.
I know how to get the size of the file but not before the Upload File button is clicked and a postback occurs.
Is there an event associated with the FileUpload web control that could submit the form (i.e. postback) without the clicking of the button?
The whole intent is that I want to give the user a feel for how long the upload might take...set a different expection for a 10mb file than for a 2kb file, etc.).
The problem is that there's no way to find out the size of file on the client side without posting back. You could use Ajax, but that would mean uploading the file first anyway.
This can only be done using an ActiveX control of some kind. I would recommend using something like the Silverlight FileUploader because it gets the size of the file before posting back and even has a nice progress indicator.
UPDATE: If you want to trigger a postback or Ajax Request after the user clicks browse, the client side event is "onchange". Here's an example of how to use the onchange event.
<asp:FileUpload runat="server"
onchange="alert('you selected the file: '+ this.value)" />
You could have the onchange, trigger an ajax to upload the file first and then update a label showing the size of the file. The problem with this is that if it is a large file, it defeats the purpose of letting the user know before hand that it will take a long time.
Here's another recommendation: There's a jQuery plugin that uses flash to determine the size of the file before uploading and it's very easy to use. Check it out at jQuery Uploadify

Resources