|
-
Aug 7th, 2009, 11:43 AM
#1
Thread Starter
Fanatic Member
[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]
-
Aug 8th, 2009, 02:40 AM
#2
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
-
Aug 8th, 2009, 12:45 PM
#3
Thread Starter
Fanatic Member
Re: Dynamic button text changes
Greg - Yes I did. It doesn't change in AddContest routine or the btnCheckContest_Click routine. Clueless.
-
Aug 9th, 2009, 02:49 AM
#4
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"?
-
Aug 9th, 2009, 05:19 AM
#5
Thread Starter
Fanatic Member
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.
-
Aug 11th, 2009, 08:27 AM
#6
Thread Starter
Fanatic Member
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:
-
Aug 11th, 2009, 08:51 AM
#7
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
-
Aug 11th, 2009, 09:25 AM
#8
Thread Starter
Fanatic Member
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.
-
Aug 11th, 2009, 02:38 PM
#9
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!
-
Aug 28th, 2009, 10:09 PM
#10
Fanatic Member
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.
-
Aug 29th, 2009, 03:05 AM
#11
Re: Dynamic button text changes
Nope, nothing here. Maybe this should be submitted to MS Connect.
-
Aug 29th, 2009, 03:28 AM
#12
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
-
Aug 29th, 2009, 09:23 PM
#13
Fanatic Member
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.
-
Aug 30th, 2009, 03:11 AM
#14
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...
-
Aug 30th, 2009, 03:21 AM
#15
Re: Dynamic button text changes
Now in VB, the bug doesn't happen anymore.
vb 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 For intCnt = 1 To 5 AddContest(intCnt) Next End Sub Sub AddContest(ByVal sContestNum As String) Dim litLabel As New Literal() Dim txtTextBox As New TextBox() Dim btnButton As New Button() Dim lblOutput As New Label() Dim lbl As New Label() 'Add Literal Control litLabel.Text = "<strong>Contest " & sContestNum & ":</strong>" plhContest.Controls.Add(litLabel) 'Add Textbox Control txtTextBox.ID = "txtContest" & sContestNum plhContest.Controls.Add(txtTextBox) 'Add Button Control btnButton.ID = "btnButton" & sContestNum btnButton.Text = "Check Contest" AddHandler btnButton.Click, AddressOf btnCheckContest_Click plhContest.Controls.Add(btnButton) lbl.Text = "<br>" plhContest.Controls.Add(lbl) lblOutput.Text = "" lblOutput.ID = "lblContest" & sContestNum plhContest.Controls.Add(lblOutput) lbl.Text = "<br><br>" plhContest.Controls.Add(lbl) 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
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?
-
Aug 30th, 2009, 04:47 AM
#16
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
-
Aug 30th, 2009, 03:22 PM
#17
Re: Dynamic button text changes
The bugs were... literally... lost in translation!
-
Aug 31st, 2009, 02:35 AM
#18
Re: Dynamic button text changes
Oh my word, where do you get these
-
Sep 1st, 2009, 06:29 AM
#19
Thread Starter
Fanatic Member
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?
-
Sep 1st, 2009, 06:43 AM
#20
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.
-
Sep 1st, 2009, 06:58 AM
#21
Re: Dynamic button text changes
 Originally Posted by lleemon
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
-
Sep 1st, 2009, 03:36 PM
#22
-
Sep 1st, 2009, 03:52 PM
#23
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|