Results 1 to 23 of 23

Thread: [RESOLVED] Dynamic button text changes

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    [RESOLVED] Dynamic button text changes

    I have a page that I create 5 sections dynamically each with a
    1) textbox
    2) button
    3) label.

    If I enter text into say textbox 2 and hit button 2 the label 2 is populated correct. Then if I click any button the last button I pushed will change it's text to what was entered into the last populated label.

    Any thoughts on how I stop this from doing that?

    Thanks.

    HTML Code:
    <asp:placeholder id="plhContest" Runat="server"></asp:placeholder>
    Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Put user code to initialize the page here
            Dim intCnt As Integer
    
            If Not IsPostBack Then
                For intCnt = 1 To 5
                    AddContest(intCnt)
                Next
            Else
                For intCnt = 1 To 5
                    AddContest(intCnt)
                Next
            End If
        End Sub
    
        Sub AddContest(ByVal sContestNum As String)
            Dim litLabel As LiteralControl
            Dim txtTextBox As TextBox
            Dim btnButton As Button
            Dim lblOutput As Label
    
            'Add Literal Control
            litLabel = New LiteralControl()
            litLabel.Text = "<strong>Contest " & sContestNum & ":</strong>"
            plhContest.Controls.Add(litLabel)
    
            'Add Textbox Control
            txtTextBox = New TextBox()
            txtTextBox.ID = "txtContest" & sContestNum
            plhContest.Controls.Add(txtTextBox)
    
            'Add Button Control
            btnButton = New Button()
            btnButton.ID = "btnButton" & sContestNum
            btnButton.Text = "Check Contest"
            AddHandler btnButton.Click, AddressOf btnCheckContest_Click
            plhContest.Controls.Add(btnButton)
    
    
            litLabel.Text = "<br>"
            plhContest.Controls.Add(litLabel)
    
            lblOutput = New Label()
            lblOutput.Text = ""
            lblOutput.ID = "lblContest" & sContestNum
            plhContest.Controls.Add(lblOutput)
    
            litLabel.Text = "<br><br>"
            plhContest.Controls.Add(litLabel)
    
        End Sub
    
        Protected Sub btnCheckContest_Click(ByVal sender As Object, ByVal e As EventArgs)
            Dim sButton As String = CType(sender, Button).ID
            Dim sButtonID As String
            sButtonID = sButton.Replace("btnButton", "")
    
            Dim lbl As Label
            lbl = CType(Page.FindControl("lblContest" & sButtonID), Label)
            Dim txt As TextBox
            txt = CType(Page.FindControl("txtContest" & sButtonID), TextBox)
    
            lbl.Text = "Value entered was:" & txt.Text
        End Sub
    Last edited by lleemon; Sep 1st, 2009 at 06:30 AM. Reason: [RESOLVED]

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Dynamic button text changes

    Hey,

    Have you tried stepping through the code in the debugger? Set a couple breakpoints, and follow the code through. This should help you figure out what is going on.

    Gary

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    Re: Dynamic button text changes

    Greg - Yes I did. It doesn't change in AddContest routine or the btnCheckContest_Click routine. Clueless.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Dynamic button text changes

    Am I understanding this right?

    You click Button2 and Label2 gets the text "2".
    You then click Button 4 and then Button2 gets the text "2"?

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    Re: Dynamic button text changes

    mendhak - That is correct.

    The proper label always get populated but the last button text changes after the first click.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    Re: Dynamic button text changes

    I thought I would provide a few screenshots to illustrate what is happening.

    1) After I enter text and click the corresponding button.


    2) Now I enter text in the 3rd textbox down and click the corresponding button and it populates the corresponding label properly but the first button changes it's text as shown below:

  7. #7
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Dynamic button text changes

    Hey,

    I have just taken the code that you provided, and I have been able to replicate the issue that you are having, but at this point I can't offer a reason why it is happening, and how you would go about fixing it?!?

    I suspect it has something to do with the fact that you are generating the controls each time the page is created, but I haven't really got enough time to look into it just now.

    What is the reason that you are dynamically creating the controls?

    Gary

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    Re: Dynamic button text changes

    Hi Gary,

    I am creating a quiz/contest type page where we will provide X contests stored in a database and on submit compares to answer and then provides points.

    Is there a better way to do this?

    Thanks for the time.

  9. #9
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Dynamic button text changes

    Fascinating. I had a look as well, and the 'change' happens right between these two lines of code:

    Code:
            plhContest.Controls.Add(btnButton)
    
    
            litLabel.Text = "<br>"
    So it's right after it's added to the controls collection that it appears that the viewstate for the previous label gets assigned. I suspect that this has something to do with the order in which the items were added to the viewstate on the page. I'm also currently trying to deserialize the viewstate to take a look but VS 2010 keeps crashing. Mysteriouser!

  10. #10
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    Re: Dynamic button text changes

    lleemon/Mendhak/Gary: did you guys ever figure out what was going wrong or how to fix it? I couldn't make head or tail of this situation and in the process read 2-3 ViewState related articles multiple times.

  11. #11
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Dynamic button text changes

    Nope, nothing here. Maybe this should be submitted to MS Connect.

  12. #12
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Dynamic button text changes

    Nope, I remember the issue well, but I couldn't for the life of me figure out what was going on.

    Gary

  13. #13
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    Re: Dynamic button text changes

    Shouldn't MVP's be reporting such things without delay..
    Hey Mendhak, if you can please do that (I am not sure how MS Connect works) and let us know if they get back with any reason for it or solution.

  14. #14
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Dynamic button text changes

    Hmm, I just tried to recreate the bug (but in C#), I got this:

    Code:
            protected void Page_Load(object sender, EventArgs e)
            {
                int count;
                for (count = 1; count <= 5; count++)
                {
                    AddContest(count);
                }
            }
    
            private void AddContest(int count)
            {
                Literal lit = new Literal();
                TextBox txt1 = new TextBox();
                Button btn1 = new Button();
                Label lbl = new Label();
                Label output = new Label();
    
                lit.Text = "<strong>Contest " + count.ToString() + ":</strong>";
                plhContest.Controls.Add(lit);
    
                txt1.ID = "txtContest" + count.ToString();
                plhContest.Controls.Add(txt1);
    
                btn1.ID = "btnButton" + count.ToString();
                btn1.Text = "Check Contest";
                btn1.Click += new EventHandler(btn1_Click);
                plhContest.Controls.Add(btn1);
    
                lbl.Text = "<br />";
                plhContest.Controls.Add(lbl);
    
                output.Text = String.Empty;
                output.ID = "lblContest" + count.ToString();
                plhContest.Controls.Add(output);
    
                lbl.Text = "<br />";
                plhContest.Controls.Add(lbl);
    
           
    
            }
    
            void btn1_Click(object sender, EventArgs e)
            {
     
    
                string buttonId = ((Button)sender).ID;
                string id = buttonId.Replace("btnButton", String.Empty);
    
                Label lbl = Page.FindControl("lblContest" + id) as Label;
                TextBox txt = Page.FindControl("txtContest" + id) as TextBox;
    
                lbl.Text = "Value entered was: " + txt.Text;
    
    
            }
    And the bug didn't show itself. I wonder...

  15. #15
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Dynamic button text changes

    Now in VB, the bug doesn't happen anymore.

    vb Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         'Put user code to initialize the page here
    3.         Dim intCnt As Integer
    4.         For intCnt = 1 To 5
    5.             AddContest(intCnt)
    6.         Next
    7.  
    8.     End Sub
    9.  
    10.     Sub AddContest(ByVal sContestNum As String)
    11.         Dim litLabel As New Literal()
    12.         Dim txtTextBox As New TextBox()
    13.         Dim btnButton As New Button()
    14.         Dim lblOutput As New Label()
    15.         Dim lbl As New Label()
    16.  
    17.  
    18.  
    19.         'Add Literal Control
    20.         litLabel.Text = "<strong>Contest " & sContestNum & ":</strong>"
    21.         plhContest.Controls.Add(litLabel)
    22.  
    23.         'Add Textbox Control
    24.         txtTextBox.ID = "txtContest" & sContestNum
    25.         plhContest.Controls.Add(txtTextBox)
    26.  
    27.         'Add Button Control
    28.         btnButton.ID = "btnButton" & sContestNum
    29.         btnButton.Text = "Check Contest"
    30.         AddHandler btnButton.Click, AddressOf btnCheckContest_Click
    31.         plhContest.Controls.Add(btnButton)
    32.  
    33.  
    34.         lbl.Text = "<br>"
    35.         plhContest.Controls.Add(lbl)
    36.  
    37.         lblOutput.Text = ""
    38.         lblOutput.ID = "lblContest" & sContestNum
    39.         plhContest.Controls.Add(lblOutput)
    40.  
    41.         lbl.Text = "<br><br>"
    42.         plhContest.Controls.Add(lbl)
    43.  
    44.     End Sub
    45.  
    46.     Protected Sub btnCheckContest_Click(ByVal sender As Object, ByVal e As EventArgs)
    47.         Dim sButton As String = CType(sender, Button).ID
    48.         Dim sButtonID As String
    49.         sButtonID = sButton.Replace("btnButton", "")
    50.  
    51.         Dim lbl As Label
    52.         lbl = CType(Page.FindControl("lblContest" & sButtonID), Label)
    53.         Dim txt As TextBox
    54.         txt = CType(Page.FindControl("txtContest" & sButtonID), TextBox)
    55.  
    56.         lbl.Text = "Value entered was:" & txt.Text
    57.     End Sub

    The difference here is that as I habitually started cleaning up, I made new labels for the "<br>" instead of reusing it. Might that be it?

  16. #16
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Dynamic button text changes

    Interesting, when I found it the first time, I didn't bother cleaning up the code, I just did a blind copy of the data.

    That is very interesting!!

    Gary

  17. #17
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Dynamic button text changes

    The bugs were... literally... lost in translation!

  18. #18
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Dynamic button text changes

    Oh my word, where do you get these

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    Re: Dynamic button text changes

    Thanks guys for spending your time on this. Hope it helps others.

    Dynamic controls seem a little touchy. Do a lot of people not use because of this?

  20. #20
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] Dynamic button text changes

    As much as possible, developers tend to use the controls such as repeaters/gridviews which do a lot of viewstate maintenance for you. It's only sometimes that you'd have to use dynamic controls to accomplish a certain task, but yes, people do use it without problems. You just got unlucky.

  21. #21
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Dynamic button text changes

    Quote Originally Posted by lleemon View Post
    Thanks guys for spending your time on this. Hope it helps others.

    Dynamic controls seem a little touchy. Do a lot of people not use because of this?
    Hey,

    I think I have had to use Dynamic Controls once, I have been able to do everything else using the built in controls.

    Gary

  22. #22
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    Re: [RESOLVED] Dynamic button text changes

    Wow! Great find. And I thought it's ViewState.

    eDIT: And why should I "spread some Reputation around before giving it to mendhak again."? I don't remember that happening. A Session issue this time?

  23. #23
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] Dynamic button text changes

    Hey,

    Forum rules says that you can't simply keep giving reputation to the same person over and over again. This could lead to some people abusing the system by rep'ing each other all the time.

    If I remember correctly, you have to rep 10 other people, before you can give rep to the same person again.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width