Click here to Skip to main content
15,886,632 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm using one button for "on and "off" in asp.net c#. When i click that button it is "on" and when i click that same button second time it is "off". Now i want to do this within one click. This means that once i click and hold that button it should be "on" till i release the button, when released it should be "off". Please let me know how can i do that !

What I have tried:

C#
protected void Page_Load(object sender, EventArgs e)
    {
        ardo = new SerialPort();
        ardo.PortName = "COM5";
        ardo.BaudRate = 9600;
    }
    protected void BlueOn(object sender, EventArgs e)
    {
        if(Session["currentState"] == null)
           Session["currentState"] = "2";

       if(Session["currentState"].ToString() == "1")
         {

            Session["currentState"]= "2";
         }
         else
         {

            Session["currentState"]= "1"; 
         }
        ardo.Open();
        ardo.Write(Session["currentState"].ToString());
        ardo.Close();

    }
Posted
Updated 8-Aug-19 1:32am
Comments
Richard MacCutchan 7-Aug-19 2:20am    
It is more usual to use 0 for off and 1 for on.
Richard MacCutchan 7-Aug-19 7:52am    
Which serial port are you trying to use? If it is not the one on the web server then your code will never work.
Faisal_Raj 7-Aug-19 8:55am    
Please check my code up there."ardo = new SerialPort();
ardo.PortName = "COM5";
ardo.BaudRate = 9600;"
I have to pass the value "1" or "2" to this "ardo" port by "ardo.Write(Session["currentState"].ToString());" .
How can i do this when using script ?
Richard MacCutchan 7-Aug-19 9:12am    
The code is irrelevant if you think this is going to connect to the client's serial port. What exactly are you trying to do?
Faisal_Raj 7-Aug-19 9:19am    
As you can see from my code up there, when i press the button "BlueOn" it send the value "1" or "2" to the arduino port "ardo.PortName = "COM5";". For these 2 value i need to press the button 2 times. Now i'm trying to do something like when i press and hold the button it will pass the value "1" and when i release the button it will pass "2".

Don't do it in C# - all C# code runs in the server, so it means a round trip via the internet for everything - or your UI will be as fluid and responsive as treacle! Instead, do it in the client via Javascript, via the up and down events there: onmousedown Event[^]
 
Share this answer
 
Comments
Faisal_Raj 7-Aug-19 2:16am    
Can you please give me example code, so i can follow that ...
George Swan 7-Aug-19 2:27am    
A neat way to change the value of a ‘bool’ on each occurrence of a specific event is to use the NOT logical operator e.g. IsClicked=!IsClicked
OriginalGriff 7-Aug-19 2:50am    
Yes, but still - do it in the Client!
Faisal_Raj 7-Aug-19 3:00am    
Kindly help me on this matter
OriginalGriff 7-Aug-19 3:11am    
Help you how? Have you followed the link and tried to do it yourself?
If so, how far did you get?
Instead of using a single event, try with two separate

for example:

C#
private void BlueOn_Click(object sender, EventArgs e)
        {
            //This will be fired as soon as the button is clicked
            //Code for on condition
   
        }

private void BlueOn_MouseUp(object sender, MouseEventArgs e)
        {
            //This will be fired as soon as you release the click
            //Code for off condition
        }
 
Share this answer
 
Comments
Faisal_Raj 8-Aug-19 8:57am    
In that case what will be code from client site i mean in aspx ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900