Answered by:
Can't set value of Label with QueryString

Question
-
User-242390214 posted
Hi All
I'm having trouble with something that I thought should be pretty straighforwards...
I'm trying to set the value of a Label with the value from a QueryString. Here's what I'm doing:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim QS As String = Request.QueryString("MenuLinkID")
Dim myLabel As Label = TryCast(DetailsView1.FindControl("Label2"), Label)
Response.Write(QS)
myLabel.Text = QS
End Sub
I definitely get the right value from the QueryString as seen in the response.write(QS)
And if I put:
myLabel.Text = "Hello World" - that works too.
Weird.
What am I missing?
Monday, November 8, 2010 7:19 PM
Answers
-
User-242390214 posted
Hi Andrew
Thanks for the info.
In the end, I rebuilt the page from scratch and it worked as intended. I'm not sure what happened to the original file but it must have been corrupt somehow. Anyway, there's probably a moral to that story...
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 9, 2010 6:49 PM
All replies
-
User-311685349 posted
You should just be able to do
Label2.Text = Request.QueryString("MenuLinkID")
Monday, November 8, 2010 8:51 PM -
User-242390214 posted
I wish!
My Label is within a DetailsView, so I have to use FindControl.
I shortened to this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim myLabel As Label = TryCast(DetailsView1.FindControl("Label2"), Label)
myLabel.Text = Request.QueryString("MenuLinkID")
End Sub
Nothing comes up - yet I can still put myLabel.Text = "Hello World" and it pops up, no problem.
I can response.write(MenuLinkID) too...
How weird is that?!
Monday, November 8, 2010 9:04 PM -
User-242390214 posted
This works:
myLabel.Text = "8"
And this:
Dim QS = "Hello World?"
myLabel.Text = QS
But not this:
Dim QS = Request.QueryString("MenuLinkID")
myLabel.Text = QS
Monday, November 8, 2010 9:17 PM -
User-311685349 posted
If you set a breakpoint on myLabel.Text = QS
what does QS contain?
Tuesday, November 9, 2010 12:19 AM -
User-242390214 posted
It says:
QS Nothing String
But presumably this is because the referring page hasn't been activated right?
Not used break points before, so not sure how this helps...
Tuesday, November 9, 2010 12:38 AM -
User-242390214 posted
Ok, the debugger seems to work running from the referring page... cool (newb!)
Anyway, it says the same thing - QS Nothing String
However, when I click continue, the response.write value is written and correct.
So, is there something wrong with trying to change the value of the Label/Textbox control with the PageLoad event?
Tuesday, November 9, 2010 12:43 AM -
User-311685349 posted
What if you did a
myLabel.Text = Request.QueryString("MenuLinkID")
I shouldn't be to do with the label... maybe the response.write is clearning the variable??? But I doubt it
Tuesday, November 9, 2010 6:40 PM -
User-242390214 posted
Hi Andrew
Thanks for the info.
In the end, I rebuilt the page from scratch and it worked as intended. I'm not sure what happened to the original file but it must have been corrupt somehow. Anyway, there's probably a moral to that story...
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, November 9, 2010 6:49 PM