LinkButton alternative? (Need it to function like a normal link) - asp.net

LinkButtons are giving me headaches. I thought, foolishly, that they allowed you to create programmatic links. Obviously this is not the case (by design), as they have behave nothing like normal links - you can't see your address when you hover over them, you can't open them in a new tab, etc.
Is there an alternative in .NET that actually lets you programmatically create a true HTML link? The ability to open multiple items in tabs is sort of a requirement. I've looked into styling a Button to look like a link, but it still behaves like a button, so this won't work either. Any ideas?
EDIT: Sorry, forgot: can't use HyperLink, as I need the ability to send CommandArguments, set OnClick events, etc. It needs to function as a button still.

The HyperLink control.
Set the href using the NavigateUrl property, and tagets (for new windows/tabs) can be set using the Target property
Edit to respond to question edit
I'm not really sure what you're after - your question is asking for a control that "functions like a normal link", so that clicking on it can open in a new window/tab, but your edit says you want to be submitting CommandArguments and using the OnClick event - so not a normal link.
The problem you've got here is that the PostBack processing of command arguments and OnClick events happens at the server, but the "open in a new window" happens on the client (using the "target" attribute of the anchor tag, or possibly with JavaScript) - these two don't really mix all that well.
A couple of options spring to mind:
Use a LinkButton, and if you handle a PostBack, output some JavaScript to open the new page in a new window.
Use a HyperLink control with a target and set the "CommandArguments" as a querystring element to the link - you can then process that on the catching page that opens in the new tab.
You can also cause JavaScript to fire onClick using the Attributes collection:
// Create a hyperlink
HyperLink link = new HyperLink();
link.NavigateUrl = "/somepage.aspx?arg=First";
link.Target = "_blank"; // Open in a new window
// Add a client side onClick event calling someMethod function with a reference
// to the link, and making sure the link processing stops.
link.Attributes.Add("onClick", "someMethod(this);return false");

You mean HyperLink?
Of course you could always use the a tag in HTML.
EDIT: When you hover over a link, the browser displays the target. When you hover over a LinkButton, the browser displays the javascript call that will execute the function server side. I'd say the best you can do is display the target page in the ToolTip, since I think it would be pretty tough to display it in the browser.

If you use a LinkButton you will be able to set the command arguments and the onclick method in your code behind. By doing so, you will no longer be able to open the link in a new window as you have found out.
One of the reasons that you cannot open a LinkButton in a new window is because it is doing a postback to the same page.
I think you either have to use a HyperLink control and pass the command argument as a query string parameter or use a LinkButton control and loose the open in a new tab functionality.
If you pass the argument as a query string parameter, then you can check for that param in page load and still call your onclick function which you were going to use for your LinkButton.
Hope this is clear and it helps you.

Related

Can I have a modal dialog from asp.net gridview edit click?

I am wondering if it is possible to have a modal dialog (like JQuery) by clicking Edit button on a asp.net built in gridview control. If yes, can anymore point me out the brief process of how it could be done. Please see the picture below for clarification.
Thanks.
I see few ways to do that:
Using OnEditCommand property (assuming you are using <asp:EditCommandColumn to draw that edit link):
You can show popup using serverside handler (for instance, popup included into ajaxcontroltoolkit.dll which allows to show popup from serverside easily on page reload)
Another option:
Make your own column with edit link for each item. It can have OnClientClick handler which will open jQuery popup directly on client (but you will need to get row info for current line from server somehow: with your own ajax call or, suppose it will be better, using webservice with webmethod)
Second option could be modified: instead of creating own column, you may add click even handler with that same jquery on default edit link with return false, so it will prevent form submition.
I never did something like this personally and even newer saw implementations of such thing, but I would select some option from those listed above. I do not think that there is some really simply, built in way of doing that.
UPD:
Here is an example of opening popup with own edit button and modalpopupextender from ajax control toolkit (similar like in my first option except that they are using own edit button, which I think could be easily replaced by default one and OnEditCommand even handler) :
http://www.c-sharpcorner.com/UploadFile/krishnasarala/edit-gridview-row-with-model-popup-extender-in-Asp-Net-ajax/

asp.net form postback or not depending on button clicked

I need an idea for a situation like this:
There is this aspx form which contains some buttons like "save", "import", and print;
on the last one clicked a window should appear providing the user with a print-friendly version. what i need is the form not to cause postback and reload in this case, while normally postback-ing when the other buttons are clicked.
Or use hyperlink with target="_blank" to open up the document in a new window.
If you like to use the button, you have to use the window.open syntax to open a new page: http://www.javascript-coder.com/window-popup/javascript-window-open.phtml
HTH.

Setting default button in asp.net 2.0

I have 3rd party user control (a captcha control), which has a captcha image, a text box within it.
I am using the above user control in my webpage. I have a 3 submit buttons on my webpage (Validate Captcha, Submit Page, Add User). When I click the Validate Captcha submit button using the mouse, I am validating whether captcha is empty and showing a javascript alert.
The problem comes when I enter the valid captcha text in the textbox and hit enter key when the cursor is in the textbox. The page just refreshes. I am unable to add keypress event to textbox and call Validate Captcha button event as I am using the 3rd party user control which I cannot modify.
Also, Page.ClientScript.RegisterHiddenField(...) will not work in my case as I have two other submit button inside the same page.
Only option left is to enclose these in panels and set default button.
Please let me know if anyone has any better options for achieving this.
Greetings! I too use alot of third party controls. The thing to remember about these controls, it that in the end they just emit HTML. This means you can use the DOM to access and attach event handlers such as onKeyPress. The trick is to identify how your control creator named the control you are looking for, in this case a {textbox}. The easiest way to achieve this is to simply run the page and view the page source. It is there that you can find the name as it is rendered and sent to the browser, after that all you have to do us use document.getElementByID to get the object and setup your handler
Example:
<script>
//Place this AFTER your textbox control is declared in the HTML
//Get the textbox
var textbox = document.getElementById('nameOfRenderedControlHere');
//Assign the event handler and function you want it to call
textbox.onclick = function() { validateCaptcha(); };
function validateCaptcha()
{ //Do your Stuff here }
</script>
That should be it..havent tested, let me knwo if you run into questions.
Put the captcha in its own <asp:Panel> and add a DefaultButton property for the panel with the ID of the captcha's submit button.

Ordering javascript code on a page executed with argument calculated in codebehind in ASP.NET

A situation requires me to order javascript code on a page be executed with an argument calculated in codebehind in ASP.NET
Here is the situation:
I have a page called search.aspx . This page contains a button and a textbox. Users put their arguments for search in the textbox and then click the button. The button posts back and runs a button click method. This code behind logic (running on the server) serializes and inserts the contents of the textbox to a DB.
Assuming a rowID or something to identify the serialized query by will be returned inside the button click method, how can I then tell the page (search.aspx) to open a new tab with results.aspx?query=.
I know I have to use javascript as the code behind can't open a new tab, but I am just wondering how to do so.
I've never used JS so a maximum amount of details in the answer is better.
Assuming your rowID is loaded in a variable named, well, rowID, then put this at the end of your click event (VB.Net):
ClientScript.RegisterStartupScript(me.GetType(), "results", _
string.Format("window.open('results.aspx?query={0}','Search Results','status=1')", rowID), True )
To directly answer your question, you can insert something like the following directly in the page output just before the body end tag.
<script type="text/javascript">
window.open("http://servername/pagename.aspx?queryid=blah");
</script>
by using Response.Write.
However, did you also consider simply opening a new window on the button click with the button's text passed in as a query string to an aspx page?

Modal popup search window to replace dropdown control ASP.NET

I'm looking for the simplest way of popping a modal search window on top of an ASP.NET 3.5 application to look up values for a field. I've got a screen for users to add courses; users need to be able to choose an instructor by searching for instructors in a popup.
So - the popup would have a textbox and a gridview with results; clicking the "choose" button in a result would populate the instructor field on the calling form.
What's the simplest way to achieve this?
Try using jQuery inside a UserControl with something like the tutorial from yensdesign.
The UserControl I created with this approach provided the user the option to set their preferences for the site. I found with this approach it was easier to control the interaction between the modal window and the calling window than calling a new popup browser window. One also doesn't have to worry about popup blockers getting in the way.
Is this helpful or are you looking for more detail?
A very simple approach would be to add javascript to your page to popup a new browser window dialog, something like this:
function fnFieldSearch(searchURL)
{
var wndSearch = window.open(searchURL,"SearchPopup","toolbar=yes,width=600,height=400,directories=no,status=yes,scrollbars=yes,resizable=yes,menubar=no");
wndSearch.focus();
}
On the modal search page, use javascript to send the search value back:
window.opener.document.FormName.ControlName.value = 'whatever';

Resources