Code in VB.net that switches between two gridviews on a page automatically with the help of a timer

There are 2 gridviews on a single aspx page, and I want the second gridview to be displayed after the first gridview for 5 seconds and vice versa automatically, without any manual intervention.

NOTE: This should happen on page load without clicking any buttons.

Currently, I am able to display the second gridview after 5 seconds of displaying the first gridview, but I'm unable to switch back to the first gridview automatically. I would appreciate your input on this issue!

<form id="form1" runat="server">
     <asp:ScriptManager ID="scriptManager1" runat="server"></asp:ScriptManager>

       <asp:Panel id="panel1" runat="server" Height="100%" Width="100%">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderColor="Purple" BorderStyle="Double" CssClass="auto-style2" DataSourceID="SqlDataSource1" Font-Bold="True" Font-Names="Algerian" Font-Size="XX-Large" Height="1200px" OnRowDataBound="OnRowDataBound" Width="100%" HorizontalAlign="Center" PageSize="15">
            <Columns>
                <asp:BoundField DataField="ProjectName" HeaderText="Project Name" SortExpression="ProjectName" ItemStyle-CssClass="FrozenCell" HeaderStyle-CssClass="FrozenCell">
                <ControlStyle Font-Bold="False"   Font-Size="XX-Large" Font-Italic="True" Font-Names="Calibri" ForeColor="#FFFFDF"  />
                <HeaderStyle ForeColor="#FF9999" CssClass="FrozenCell" />
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Font-Bold="True" Font-Names="Calibri" Font-Size="XX-Large"  />
                </asp:BoundField>
                 <asp:BoundField DataField="ReqBy" HeaderText="Requested By" SortExpression="ReqBy">
                <ControlStyle Font-Bold="False" Font-Size="XX-Large" Font-Italic="True" Font-Names="Calibri" ForeColor="#FFFFDF" />
                <HeaderStyle ForeColor="#FF9999" />
                     <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Font-Bold="True" Font-Names="Calibri" Font-Size="XX-Large" />
                </asp:BoundField>

				... (remaining code)

Answer №1

Below is an example you might find interesting to explore. It showcases a simple demonstration of how a certain functionality could be implemented.

EXAMPLE

https://i.sstatic.net/rKwRs.gif

Snippet Showcase

Representation

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick"></asp:Timer>
        <div style="display: flex;">
            <div style="margin-right: 20px;">
                <h4>GridView1</h4>
                <asp:GridView ID="GridView1" runat="server"></asp:GridView>
            </div>
            <div>
                <h4>GridView2</h4>
                <asp:GridView ID="GridView2" runat="server"></asp:GridView>
            </div>
        </div>
        <asp:HiddenField ID="HiddenField1" runat="server" />
        <div style="height: 200px; overflow-x: auto; margin-top: 10px;">
            <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        </div>
    </ContentTemplate>
</asp:UpdatePanel>

Visual Basic Code

Private Property gridNumber As Integer
    Get

        If Not IsPostBack Or String.IsNullOrEmpty(HiddenField1.Value) Then
            HiddenField1.Value = 1.ToString()
        End If

        Return Integer.Parse(HiddenField1.Value)
    End Get
    Set(ByVal value As Integer)
        HiddenField1.Value = value.ToString()
    End Set
End Property

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        BindGrid(1)
        BindGrid(2)
        Timer1.Enabled = True
        Timer1.Interval = 1000
    End If
End Sub

Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs)
    BindGrid(gridNumber)
    Dim colorCode As String = If(gridNo = 1, "green", "blue")
    Label1.Text = String.Format("<span style='margin-bottom: 5px; display: block;color:" & textColor & "'>GridView{0}.DataBind()</span>", gridNo)
    gridNumber = If(gridNumber = 1, 2, 1)
End Sub

Private Sub BindGrid(ByVal gridNumber As Integer)
    Select Case gridNumber
        Case 1
            GridView1.DataSource = Enumerable.Range(1, 3).Select(Function(x) New With {
                Key .CurrentTime = DateTime.Now.AddHours(x).TimeOfDay
            })
            GridView1.DataBind()
        Case 2
            GridView2.DataSource = Enumerable.Range(1, 3).Select(Function(x) New With {
                Key .CurrentTime = DateTime.Now.AddHours(x).TimeOfDay
            })
            GridView2.DataBind()
    End Select

Answer №2

Avoid relying on a VB timer for this task.

Using a VB timer will require you to constantly reconstruct the grid data and go through a complete HTTP request/response cycle with each tick. It is better to utilize JavaScript to switch between grids that have already been rendered on the same HTML page.

You can easily switch between the grids by incorporating a slideshow or carousel script within the container div.

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

Property missing in Typescript type definition

In my Typescript Next project, I am using this component: import PageTitle from './pagetitle' import style from './contact.styl' export default function Contact() { return ( <section> <a name="contact"> ...

Check if the element is located on the active tab using jQuery

I have implemented a tab system to organize a lengthy form with multiple input fields. The form consists of 4 tabs, and beneath them, there is a preview section displaying the user's selections. In this preview area, I added icons that enable users ...

Is it possible to generate unique identifiers for v-for keys using vue-uuid?

I'm attempting to utilize a random string (UUID v4) using vue-uuid for existing items and future additions to the list in my to-do list style application. However, I'm uncertain about the correct syntax to use. After installation, I included it ...

The yarn.lock file is failing to reflect the updated version of a dependency in package.json, even after running the yarn install command or simply using yarn to install the

Within the project's repository, both the package.json and yarn.lock files are already present. I am currently in the process of upgrading a specific package from version 2.0.14 to version 2.0.16. After running either yarn install or simply yarn, I n ...

Challenges faced with the $_POST["var"] variable in Yii Controller

I am facing an issue with $_POST["var"] in my controller. It appears to be empty. How can I capture the string entered in my textField? View <?php Yii::app()->clientScript->registerCoreScript("jquery"); ?> <script type="tex ...

insert data into modal's interface

I'm encountering an issue with my code, where the modal is not displaying the values inside the brackets as if they were not bound correctly. Everything else in the ng-repeat seems to be working fine, but for some reason the values are not being displ ...

Encountered a runtime error while processing 400 requests

Current Situation: When authenticating the username and password in my Ionic 2 project using WebApi 2 token authentication, a token is returned if the credentials are correct. However, a 400 bad request error is returned if the credentials are incorrect. ...

Exploring the internet with Internet Explorer is like navigating a jungle of if statements

I am facing an issue with my code that works well in Chrome, but encounters errors in IE. The if condition functions correctly in Chrome, however, in IE it seems like the first condition always gets executed despite the value of resData. Upon analysis thro ...

Manipulating TextGeometry by both removing and adding it can cause the scene to

My current project involves creating a clock using Text Geometry. To update the time displayed on the clock, I have to remove and recreate a new Text Geometry every time. Unfortunately, this process of adding a new Text Geometry causes my browser to freeze ...

Transforming an array into JSON format in order to verify the data

After sending an Ajax call to a PHP file, I receive an array in return. My objective is to verify the values of the received data. Take a look at the following Javascript/jQuery code snippet: $.ajax({ url: "file.php", type: "POST", data: {&a ...

Finding the dynamic width of a div using JavaScript

I have two divs named demo1 and demo2. I want the width of demo2 to automatically adjust when I drag demo1 left to right or right to left. <div class="wrapper"> <div class="demo"></div> <div class="demo2"&g ...

Set up local package dependencies using npm

My current situation involves having an npm dependency installed from a local path, which also has its own dependencies. I have observed that in this scenario, npm simply copies the contents of the local folder into node_modules. Is there any possible wa ...

Utilizing a dropdown selection value to filter a list in JavaScript

I have an array of objects where I need to filter based on a dropdown selection. const itemsList=[{ "id":"123", "problems":{ "causes":[ { "SSEnabled": true, "IndusEnabled": false, "LogEnabled": true } ] } ...

Having trouble with my Slack Bot development due to an unexpected error

While attempting to create a Slack Bot in Node, I consistently encounter an error when running npm start in my terminal: The error seems to be located in the Vow.js file. Despite double-checking everything, I can't seem to pinpoint the issue. For gui ...

A Guide to Displaying Django Forms.TimeField in 12-hour Format on a Template

I am facing an issue with my Django form when creating and updating model data. Upon creation, I simply input 6:30 pm using the timepicker, which works fine. However, when editing an existing record, Django displays the initial time in 24-hour format (e.g. ...

Twilio Group MMS feature experiencing technical difficulties

I currently have a Twilio Trial account with an active number that supports SMS/MMS. My goal is to use this number for group MMS chats, but I am facing some challenges. After following a tutorial on Tut, I attempted to create a basic test using the provid ...

Prevent any http requests until an observable completes its task

I'm currently developing an Angular interceptor where, before sending any non-GET requests, I need to make a GET request to obtain a specific header from the server and then set that header on all subsequent requests. Is there a method for achieving t ...

Implementing a specific block of code once an Angular service successfully retrieves data from the server and finishes its operations

Within the service: appRoot.factory('ProgramsResource', function ($resource) { return $resource('Home/Program', {}, { Program: { method: 'get', isArray: false } }) }); Inside the controller: appRoot.controller('Pro ...

Increase or decrease values by clicking on Material UI IconButton

Looking to incorporate the functionality of an onClick() event for a Material UI IconButton that can either increase or decrease the Calories value for each item in the table. I've referenced the code from the Table React component page. https://i.ss ...

How can I incorporate a feature in my Angular application that allows users to switch between different view types, such as days, using JavaScript

Greetings, community! I am currently utilizing version 5 of the fullcalendar library from https://fullcalendar.io/ in my Angular 9 application. I have noticed that the calendar offers various options to change the view type as shown below: https://i.stac ...