locked
When A Function is Called from Another Function Buttons Dont' Display as Enabled/Disabled. RRS feed

  • Question

  • User569238728 posted

    I've had it, I can't get this program to run right, could I please have some help?  The problem is when I call the NewResponseCall() function from SendMessage() the button event handler that called SendMessage() doesn't display the disabled/enabled buttons. 

    Also when I try to use this call :    AllDisabledClick(new object(), new EventArgs()); from SendMessage() the buttons are not displayed correctly (enabled/disabled) either.  They only work right when NewResponseCall() is not called.

    SendMessage() calls NewResponseCall() so that sender becomes receiver.  The new Sender Presses buttons to run it's functions.  

    Sorry about the code format, couldn't get it to work right!

    Here is the code:

    public string SendMessage(string buttonpressed)
            {
                AllDisabledClick(new object(), new EventArgs());
                Response.Write("in send message<br>");

    //OTHER FUNCTION CODE HERE//

    string msg = buttonpressed;
    //should be 1 - 5 is the message
    OneChatRoom message = new OneChatRoom(msg, "0", "0");
               
                    //variable will be set at previous screen
                    int roomnumber = 1;

                    Response.Write("adding a message<br>");
                    //sends message
                    switch (roomnumber)
                    {
                        case 1:
                            list1.Add(message);
                            HttpContext.Current.Application["Application_list1"] = list1;
                            break;

    //OTHER FUNCTION CODE HERE//

    //THIS IS THE FUNCTION CALL THAT CAUSES FAILURE:

     // NewResponseCall()
     return ("A");

    ///OTHER FUNCTION

    public string NewResponseCall()
            {
                string flag = "1";
                Response.Write("in new respones function<br>");
                //waits for list to be added too

                list1 = (List<OneChatRoom>)Application["Application_list1"];
                while(list1 == null || list1.Count() == 0)
                {
                    list1 = (List<OneChatRoom>)Application["Application_list1"];
                }
                //TWO OF THESE!!!!! - determined by previous page - hack
                string roomnumber = "1";

               if (roomnumber == "1")
                {
                    var firstItem = list1.ElementAt(0);
                    string _firstItem = firstItem.Strin;
                    list1.RemoveAt(0);
                    //kept current
                    Application["Application_list1"] = list1;

    //THE REST OF FUNCTION CODE HERE//

    //BUTTON CODE THAT CALLS SendFuntion()

    protected void Button1_Click(object sender, EventArgs e)
            {
                if (rand2 == "1")
                {
                    Application["randomnumber1"] = rand2;
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('You Win!'); ", true);
                }
                Button6.Enabled = false;
                Button2.Enabled = false;
                Button3.Enabled = false;
                Button4.Enabled = false;
                Button5.Enabled = false;
                SendMessage("1");
            }

    Thank you,

    Josh

    Thursday, September 26, 2019 12:49 PM

Answers

  • User475983607 posted

    Your design is based on a misconception.  There is no persistent connection between a browser and a web server.   The browser sends an HTTP request to the server then waits for a response.  Once the browser receives the response it refreshed the page with the content sent from the server.  When you are viewing a web page in the browser there is not active connection to the server.  You are seeing the results of a past action.

    Your design must be completely reworked.  The game state must be moved to the web server.  Each play must be identified and I would use a cookie but Session would work too. Lastly, there must be many page refreshes in order for each player to see the state change within there browser.  I recommend using AJAX or fetch to make the HTTP requests then you don't see the screen flicker. 

    SignalR is an API that you might want to look into.  SignalR keeps a persistent connection between the browser and server and can respond to events like clicking a button.

    https://docs.microsoft.com/en-us/aspnet/signalr/overview/getting-started/tutorial-getting-started-with-signalr

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 28, 2019 7:24 PM

All replies

  • User475983607 posted

    Refactor your code.  Move the AllDisabledClick event handler method body to another method and simply call that method. 

    Keep in mind you did not post the relevant code.  I assume AllDisabledClick has the disable logic?  Also this is a chat application?  

    Lastly, use the Insert/Edit code sample button to insert code.

    Thursday, September 26, 2019 1:24 PM
  • User569238728 posted

    It's a simple guess the number program that two users play on a site.  I was thinking about making a lobby for it. Unfortunately, your solution didn't work.  I want the button to be pressed and immediately have the buttons disabled.  As far as I know, controls can't be accessed in functions, only event handlers.  About my code, I did supply the code for the button one click. 

    Thanks,

    Josh

    Thursday, September 26, 2019 2:25 PM
  • User569238728 posted

    Maybe this will help!

    protected void AllDisabledClick(object sender, EventArgs e)
    {
    Button1.Enabled = false;
    Button2.Enabled = false;
    Button3.Enabled = false;
    Button4.Enabled = false;
    Button5.Enabled = false;
    }
    public void AllDisabledClick()
    {
    Button1.Enabled = false;
    Button2.Enabled = false;
    Button3.Enabled = false;
    Button4.Enabled = false;
    Button5.Enabled = false;
    }

    Thursday, September 26, 2019 2:34 PM
  • User475983607 posted

    ASP_User_qwert

    It's a simple guess the number program that two users play on a site. 

    Application variables are persisted application wide.   How is the game played?  Each player takes a turn at guessing a random number?  Which is the reason for disabling the buttons?

    ASP_User_qwert

    Unfortunately, your solution didn't work.

    What does "didn't work" mean exactly?  Can you show the code that "didn't work"?

    ASP_User_qwert

    As far as I know, controls can't be accessed in functions, only event handlers.

    Controls are members of the page class and available to all instance methods which include event handlers and methods but not static methods.

    ASP_User_qwert

    About my code, I did supply the code for the button one click. 

    The code shown is very hard to follow and understand the intent.  that's why I'm asking for clarification.

    Thursday, September 26, 2019 3:01 PM
  • User665608656 posted

    Hi About ASP_User_qwert,

    According to your description, I'm not very clear about your needs.

    Hope you can provide detailed and comprehensive code, and clearly describe the rules and logic of the game, as well as your problems, which will help us solve your issues more easily.

    Best Regards,

    YongQing.

    Friday, September 27, 2019 1:59 AM
  • User569238728 posted

    Could I have instructions on how to use Insert/Edit code samples.  On one of my posts it just turned the text red.  There is no delete, so what am I doing wrong?

    Josh

    Friday, September 27, 2019 12:00 PM
  • User475983607 posted

    The insert/edit code sample is a button in the Message toolbar {;} when creating a new post.  Click the button, add your source code, and click Ok.

    Friday, September 27, 2019 1:12 PM
  • User569238728 posted

    The game is about choosing a button from one to five.  The sender picks a button to try and win, there is only one button that can win.  The Sender should see all of the buttons disabled as soon as he picks his guess and the receiver gets to see the all of the disabled and enabled buttons.  This is because it is now the receivers turn to be the sender and the sender must wait.  The new sender now gets to choose a button and than his buttons are disabled.

    Here is a problem.  Without changing the code the sender does not see any changes.  The receiver does see all of the changes every time.

    Here is a second problem.  When changing the code to use either of the AllDisableClick() functions, the second receiver sees all the buttons disabled.

    The following is all the code that is used.

    protected void Page_Load(object sender, EventArgs e)
            {
                
                if (Page.IsPostBack)
                {
                    
                    
                    Response.Write("in postback<br>");
                    
                }
                else
                {
                    
                    //Label2.Text = "testing this too..";
                    //needs to be reworked
                    rand2 = "2";
                    Response.Write("in else<br>");
                    //in previous page   - whose using current send
                    var temp1 = Session["MyUserNumber"];
                    string temp1a = temp1.ToString();
                    //creates this variable from previous page
                    var temp2 = Application["ThisIsFirstPageLoad"];
                    string temp2a = temp2.ToString();
                    //starting, user two goes in these (previous page)
                    if (temp1a == "0")
                    {
                        if (temp2a == "1")
                        {
                            Application["ThisIsFirstPageLoad"] = "2";
                            Response.Write("new response call<br>");
                            ReceiveMessage();
                        }
                    }
                }
            }
    protected void Button1_Click(object sender, EventArgs e)
            {
                if (rand2 == "1")
                {
                    Application["randomnumber1"] = rand2;
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('You Win!'); ", true);
                }
                
                UpdateButtons(new object(), new EventArgs());
                SendMessage("1");
            }
    protected void UpdateButtons(object sender, EventArgs e)
            {
                Response.Write("in update buttons<br>");
                
                if (holdsbuttonspressed[0] == "1")
                {
                    //this is correct!
                    Button6.Enabled = false;
                }
                if (holdsbuttonspressed[1] == "1")
                {
                    Button2.Enabled = false;
                }
                if (holdsbuttonspressed[2] == "1")
                {
                    Button3.Enabled = false;
                }
                if (holdsbuttonspressed[3] == "1")
                {
                    Button4.Enabled = false;
                }
                if (holdsbuttonspressed[4] == "1")
                {
                    Button5.Enabled = false;
                }
            }
     protected void AllDisabledClick(object sender, EventArgs e)
            {
                Button6.Enabled = false;
                Button2.Enabled = false;
                Button3.Enabled = false;
                Button4.Enabled = false;
                Button5.Enabled = false;
            }
            protected void AllDisabledClick()
    
            {
                Button6.Enabled = false;
                Button2.Enabled = false;
                Button3.Enabled = false;
                Button4.Enabled = false;
                Button5.Enabled = false;
            }
     public string SendMessage(string buttonpressed)
            {
                
                //without either: all receiving displays disabled buttons are right
                //sender doesn't update until next receive
    
                //second sender causes reciever to be all disabled
                //should be this sender gets disabled on it's button press
                //AllDisabledClick();
    
                //second sender causes reciever to be all disabled
                //should be this sender gets disabled on it's button press
                //AllDisabledClick(new object(), new EventArgs());
    
                Response.Write("in send message<br>");
                
                ////make an array that holds real state of buttons
                ////so restore after all buttons disabled after send
                if (buttonpressed == "1")
                {
                    holdsbuttonspressed[0] = "1";
                }
                else if (buttonpressed == "2")
                {
                    holdsbuttonspressed[1] = "1";
                }
                else if (buttonpressed == "3")
                {
                    holdsbuttonspressed[2] = "1";
                }
                else if (buttonpressed == "4")
                {
                    holdsbuttonspressed[3] = "1";
                }
                else if (buttonpressed == "5")
                {
                    holdsbuttonspressed[4] = "1";
                }
                
                Label1.Text = "here!";
    
                string msg = buttonpressed;
                //should be 1 - 5 is the message
                OneChatRoom message = new OneChatRoom(msg, "0", "0");
                //variable will be set at previous screen
    
                int roomnumber = 1;
                Response.Write("added a message<br>");
                
                //sends message instead of switch for testing purposes
                list1.Add(message);
                Application["Application_list1"] = list1;
                
                //switch (roomnumber)
                //{
                //    case 1:
                //        list1.Add(message);
                //        HttpContext.Current.Application["Application_list1"] = list1;
                //        break;
    
                //    case 2:
                //        list2.Add(message);
                //        HttpContext.Current.Application["Application_list2"] = list2;
                //        break;
                //    case 3:
                //        list3.Add(message);
                //        HttpContext.Current.Application["Application_list3"] = list3;
                //        break;
                //    case 4:
                //        list4.Add(message);
                //        HttpContext.Current.Application["Application_list4"] = list4;
                //        break;
                //    case 5:
                //        list5.Add(message);
                //        HttpContext.Current.Application["Application_list5"] = list5;
                //        break;
                //        default:
                //        break;
                //}
                Response.Write("swapping turn<br>");
                swapuser.SwapWhoseTurn();
    
                string flag2 = "1";
                //waits for receiver to finish before running receive function
                while (flag2 == "1")
                {
                    var waitforreceiver = Application["WaitingForReciever"];
                    string waitforrec = waitforreceiver.ToString();
                    if (waitforrec == "done")
                    {
                        flag2 = "0";
                        waitforrec = "not done";
                        Application["WaitingForReciever"] = "not done";
                    }
                }
                Response.Write("at end, call response<br>");
                ReceiveMessage();
                return ("A");
            }
    public string ReceiveMessage()
            {
                //code for testing:
                //string msg4 = "1";
                //OneChatRoom messagez = new OneChatRoom(msg4, "0", "0");
                //list1.Add(messagez);
                //Application["Application_list1"] = list1;
                //list1= (List<OneChatRoom>)Application["Application_list1"];
    
                string flag = "1";
                Response.Write("in new response function<br>");
                ////waits for list to be added too
                list1 = (List<OneChatRoom>)Application["Application_list1"];
                while (list1 == null || list1.Count() == 0)
                {
                    list1 = (List<OneChatRoom>)Application["Application_list1"];
                }
                //Determined by previous page, will do later
                string roomnumber = "1";
                if (roomnumber == "1")
                {
                    var firstItem = list1.ElementAt(0);
                    string _firstItem = firstItem.Strin;
                    list1.RemoveAt(0);
                    //kept current
                    Application["Application_list1"] = list1;
                    //update button array
                    if (_firstItem == "1")
                    {
                        holdsbuttonspressed[0] = "1";
                    }
                    if (_firstItem == "2")
                    {
                        holdsbuttonspressed[1] = "1";
                    }
                    if (_firstItem == "3")
                    {
                        holdsbuttonspressed[2] = "1";
                    }
                    if (_firstItem == "4")
                    {
                        holdsbuttonspressed[3] = "1";
                    }
                    if (_firstItem == "5")
                    {
                        holdsbuttonspressed[4] = "1";
                    }
                }
                //commented out for testing purposes
                //else if (roomnumber == "2")
                //{
                //    list2.RemoveAt(0);
                //    Application["Application_list2"] = list2;
                //}
                //else if (roomnumber == "3")
                //{
                //    list3.RemoveAt(0);
                //    Application["Application_list3"] = list3;
                //}
                //else if (roomnumber == "4")
                //{
                //    list4.RemoveAt(0);
                //    Application["Application_list4"] = list4;
                //}
                //else if (roomnumber == "5")
                //{
                //    list5.RemoveAt(0);
                //   Application["Application_list5"] = list5;
                //}
                
                ////enable / disable buttons
                Response.Write("update buttons - swap user<br>");
                UpdateButtons(new object(), new EventArgs());
                Response.Write("recieve function - swap user<br>");
                //swap user
                var temp = Application["whoseturnisit"];
                string temp2 = temp.ToString();
                if (temp2 == "1")
                {
                    Application["whoseturnisit"] = "0";
                }
                else
                {
                    Application["whoseturnisit"] = "1";
                }
                Application["WaitingForReciever"] = "done";
                return ("Z");
                }
    namespace lobby
    {
        public partial class Site1 : System.Web.UI.MasterPage
        {
            
            //CHANGE THIS!!!
            string rand2 = "0";
            string[] holdsbuttonspressed = new string[] {"0","0","0","0","0" };
            ChangeUser swapuser = new ChangeUser();
            int counter = 0;
            List<OneChatRoom> list1 = new List<OneChatRoom>();
            List<OneChatRoom> list2 = new List<OneChatRoom>();
            List<OneChatRoom> list3 = new List<OneChatRoom>();
            List<OneChatRoom> list4 = new List<OneChatRoom>();
            List<OneChatRoom> list5 = new List<OneChatRoom>();
    public class ChangeUser
        {
            public string SwapWhoseTurn()
            {
            
                //switch whose turn
                var temp = HttpContext.Current.Application["whoseturnisit"];
                string temp2 = temp.ToString();
                if (temp2 == "1")
                {
                    HttpContext.Current.Application["whoseturnisit"] = "0";
                }
                else
                {
                    HttpContext.Current.Application["whoseturnisit"] = "1";
                }
            return ("D");
    
        }
    
    }

    If you have any questions, please ask.

    Joshua



     

    Saturday, September 28, 2019 6:27 PM
  • User475983607 posted

    Your design is based on a misconception.  There is no persistent connection between a browser and a web server.   The browser sends an HTTP request to the server then waits for a response.  Once the browser receives the response it refreshed the page with the content sent from the server.  When you are viewing a web page in the browser there is not active connection to the server.  You are seeing the results of a past action.

    Your design must be completely reworked.  The game state must be moved to the web server.  Each play must be identified and I would use a cookie but Session would work too. Lastly, there must be many page refreshes in order for each player to see the state change within there browser.  I recommend using AJAX or fetch to make the HTTP requests then you don't see the screen flicker. 

    SignalR is an API that you might want to look into.  SignalR keeps a persistent connection between the browser and server and can respond to events like clicking a button.

    https://docs.microsoft.com/en-us/aspnet/signalr/overview/getting-started/tutorial-getting-started-with-signalr

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, September 28, 2019 7:24 PM