What is the best way to create a button that swaps the values of two text boxes, along with its corresponding backend code for asp.net

Is there a way to exchange values between two textboxes with the help of a UI button for swapping?

Here is an example of how it should work -

swap toggle

I have explored various options but have not found a suitable solution yet. I require this functionality specifically in ASP.NET using HTML and CSS.

Answer №1

To implement this functionality, use the following HTML markup:

        <div style="border:solid 1px black;width:220px;padding:5px;float:left" >
        <asp:Label ID="Label1" runat="server" Text=""
            ></asp:Label>            
        </div>

        <div style="float:left;margin-top:-8px">
        <asp:ImageButton ID="ImageButton1" runat="server" 
            ImageUrl="Content/swap2.png"
            OnClick="ImageButton1_Click"
            />

        </div>

        <div style="border:solid 1px black;width:220px;padding:5px;float:left" >
        <asp:Label ID="Label2" runat="server" Text="xx"
            ></asp:Label>            
        </div>

The code behind for swapping values on button click is as follows:

Protected Sub ImageButton1_Click(sender As Object, e As ImageClickEventArgs)

    Dim strTemp As String = Label1.Text
    Label1.Text = Label2.Text
    Label2.Text = strTemp

End Sub

Visual representation of the result can be seen here.

Although the example uses labels, text boxes can also be used interchangeably for this functionality.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error: The function for "category-name" value is not recognized

I encountered an issue Uncaught TypeError: "#category-name".val is not a function at HTMLButtonElement.<anonymous> (<anonymous>:28:42) at HTMLButtonElement.dispatch (jquery-3.3.1.min.js:2) at HTMLButtonElement.y.handle (jquery-3.3.1.min.js:2) ...

Switch the sign from plus to minus by modifying the :before element

My goal is to change the plus sign to a minus sign when it is collapsed by modifying the plus:before (by setting display to none or something similar to create a minus sign effect.) Currently, my code is functioning properly and collapsing as intended. You ...

Ways to make the height of two div elements the same using CSS

I need to ensure that the height of two div's is equal, with one stretching to match the height of the other if necessary. I am looking for a solution using only CSS. Here is the code I have so far (Fiddle Link): HTML: <div class="common"> ...

Item within text box remains untouched

I am working with a 1) Text field 2) Drop down list 3) List box I have written code to add the selected value from the drop down list to the list box and text field, but I'm having trouble removing the value from the text field. Can someone please he ...

Tips for positioning a Navbar in the middle of the screen at 90% width

I am struggling to center the navbar as a whole, rather than just the contents within it. Below is the CSS and HTML for my navbar. .navbar { border-radius: 10px 10px 30px 30px; background: #3CE18F; overflow: visible; position: fixed; bottom: ...

"Including Span icons, glyphs, or graphs within a table cell in AngularJS based on a specified condition or numerical

I'm attempting to incorporate span icons/glyph-icons using Angular within a td before displaying the country. I've utilized the code below to achieve this, but I'm looking for a more dynamic and elegant solution. Your assistance is greatly a ...

What is the proper way to nest a <frameset> tag?

Attempting to nest a frameset inside another frameset has proven unsuccessful for me. I attempted to use the <frame> tag but encountered some issues. <frameset rows="15%,85%"> <frame style="background-color: orangered"></frame> ...

Can the default position of the scrollbar be set to remain at the bottom?

I have a select option tag with a scrollbar to view the contents in the dropdown. I am looking for a way to automatically position the scroll at the bottom when an item is selected from the dropdown. jquery code $('document').ready(func ...

Exclude empty object nodes using Newtonsoft.Json CustomContractResolver

My current challenge involves serializing a group of objects that contain various types, including references to other custom types. I am aiming to exclude these object references if all their property members consist of default values or are null. Here is ...

Guide to repairing a CSS ball animation

I am attempting to create a simulation where a ball moves in the following pattern: bottom right -> top center -> bottom (/) left. However, my animation seems to be moving from top right -> bottom center -> top left (/). #stage { height:300 ...

External JavaScript file not executing defined function

My homepage includes a register form that should open when the register button is clicked. However, I am encountering an issue where the function responsible for opening the form from another JavaScript file is not being triggered. Here is the HTML code: ...

Discovering a project's file monitoring abilities using Rx to track changes both internally and externally

I am seeking to replicate the functionality of Visual Studio that alerts you when a project file is modified externally and suggests reloading it! Based on my requirements, I believe using reactive programming is an ideal solution for this problem. To ac ...

I am experiencing a problem trying to transfer a file to the specific folder I want

I am facing an issue where I am trying to move my file to a specific folder but it seems like the file is not being moved successfully. Can someone please assist me with this problem? Provided below is the code snippet from my controller "Sms_Control ...

Having trouble with a loop that only loops once? Need assistance finding a solution in C# and MySQL?

I'm currently attempting to run a MySQL query inside a loop that retrieves new results each time. However, I've encountered an issue where the loop only works successfully on the first iteration. Upon reaching the second iteration, I receive the ...

"Trying to upload a PDF file with ajax, but encountering consistent failures with

I've been struggling with this code that just won't work as intended. I'm attempting to send file data and an ID to the php side, but it's failing consistently. I can't figure out what mistake I'm making. Any help would be gre ...

PHP infinite redirection loop

I have encountered an issue where a user can successfully submit an event to a calendar, but I am facing difficulty redirecting them back to the original page. I attempted to use the following code snippet: header("Location: http://localhost/Project/Vie ...

I am having trouble toggling radio buttons in React

class VoiceSelector extends Component { constructor(props){ this.handleCheck = this.handleCheck.bind(this); } state={ voices : [ {"Voice":"Indian English Female Voice 1"}, {"Voice":&qu ...

Implementing dynamic controls in an ASP.NET MVC5 application using AngularJS

I am currently working on a project that will utilize MVC5, angularJS, and bootstrap for development. One of the key features in my project is a dropdown menu called "expense type". Upon selecting a value from this dropdown, additional controls need to be ...

Why is the Page_Load event still triggered even after the PreInit event redirects to a different page

On my aspx page, I have implemented a check to verify if the user is logged in during preinit. If not, the page redirects to another page. However, I noticed that the code inside page_load still executes even after the page jumps. Is this behavior correct? ...

What is the best way to trigger a method once an image has completed loading and rendering?

Is there a way to trigger a method once an image has finished loading and displaying on the web browser? Here's a quick example using a large image: http://jsfiddle.net/2cLm4epv/ <img width="500px" src="http://www.digivill.net/~binary/wall-cover ...