How to use findcontrol in createuserwizard1 complete step ...? - asp.net

Is this the right declaration of findcontrol for complete step of createuserwizard1 ?
Dim UserName As TextBox = CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Label11")
But when i use it its shows the error object expected !
What was the problem ?

You can't set the value of a Textbox to a generic control (especially one that is really a label). Plus you forgot the New keyword when initializing UserName. Depending on exactly what you're trying to do, you may want to try the following...
'This will set the textbox's Text to the label's text.
Dim UserName As New Textbox
UserName.Text = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Label11"), Label).Text
Or
'This will cast the label to a new label control you define in code.
Dim UserName as New Label
UserName = CType(CreateUserWizard2.CreateUserStep.ContentTemplateContainer.FindControl("Label11"), Label)

Related

Declaring a public variable

When I try to move 'UserName' from local scope 'dim' to public, I get 'Object reference not set to an instance of an object.' What is causing this?
Going from this:
Dim UserName As TextBox = DirectCast(LoginUser.FindControl("UserName"), TextBox)
to this:
Public UserName As TextBox = DirectCast(LoginUser.FindControl("UserName"), TextBox)
Then accessing variable like this:
If (Request.Cookies("Username") IsNot Nothing) Then
UserName.Text() = Request.Cookies("Username").Value.ToString()
End If
You have 3 potential problems with your code:
It should be UserName.Text i.e. not .Text()
Request.Cookies("Username").Value.ToString()
If "username" is not found in the cookie collection you will get a
Value of nothing. Then you call ToString on nothing which
gives you Object Reference error.
Make sure that the UserName TextBox is initalized and not nothing when
you try to access it.

How to reference dynamic textboxes created at run time?

I have an ASP project which references a WCF service. Does exactly half of what I need.
A button on the page calls a function from the WCF, which returns a list of objects (variable names). When returned, the vb code dynamically adds textboxes to a panel on the page. Like this:
For Each LetterVariables In LetterVarList
tb = New TextBox
lb = New Label
lb.Text = LetterVariables._key & " "
tb.ID = LetterVariables._key
pnlVars.Controls.Add(lb)
pnlVars.Controls.Add(tb)
Dim LineBreak As LiteralControl = New LiteralControl("<br />")
pnlVars.Controls.Add(LineBreak)
Next
Now the problem is, after this is finished the user will enter some values into those texboxes. I (somehow) need to reference those texboxes to snag the values when a user clicks another button.
How can I do this?
Thanks,
Jason
You can give the TextBox an ID which you could use FindControl to retrieve.
tb.ID = "txt" + LetterVariables._key.ToString();
Then when you want to reference it.
TextBox txtBox = (TextBox)FindControl("txt" + someKey);
Something like that might work for you.
Don't forget to recreate the controls on post back BEFORE the controls are loaded with the posted values.

Dynamically Reference an Object Property Using a String

I'm trying to reference a public property from a string. How can this be done in vb.net?
I have the text value of "FirstName" stored in strucParam(i).TxtPropertyName.
This is what I'm currently doing:
Dim tmpValue As String
Dim ucAppName As UserControl_appName = CType(Parent.FindControl(strucParam(i).ParentFindControl), UserControl_appName)
tmpValue = ucAppName.FirstName.Text
How can I use the value in strucParam(i).TxtPropertyName so that I can remove ".FirstName" from my code? Thanks!
This is basically a duplicate of this question, but I'll answer it for you since you're a VB user and probably didn't consider C# in your searches.
Suppose you have an object of any type stored in a variable called objObject, and the name of the property stored in a variable called strPropertyName. You do the following:
tmpValue = objObject.GetType().GetProperty(strPropertyName).GetValue(objObject, Nothing)
As a final note: please, please consider dropping pseudo-Hungarian notation. It's of no value when working with a statically typed language like VB.NET.
Edit:
The FirstName property is in reality a text box. So don't I need to somehow reference .Text in the code?
tmpFirstName = ucAppName.GetType().GetProperty(strucParam(i).PropertyName).GetValue(objAppNav, Nothing)
Try this:
Dim textBox as TextBox
Dim tmpValue as String
textBox = CType(ucAppName.GetType().GetProperty(strucParam(1).PropertyName).GetValue(objAppNav, Nothing), TextBox)
tmpValue = textBox.Text
Basically, you have to cast the value of the property to a TextBox type, then grab the Text property from it.

accessing HtmlTable inside a placeHolder

I'm working with a website written in aspx.net over vb.
I have a placeHolder, and I create a table of names inside this PlaceHolder, each name has an HtmlInputCheckBox next to it.
Im doing this in the aspx.vb file, when the page is uploading.
Then, when the user wants to send mail, he presses a button and than I need to access the checkboxes, and I'm having problems with this, the Sub doesn't know the checkBox object.
I would love for some help,
Thank you!
I understand that you're creating those checkboxes dynamically?
In such case, store them as global member of the class, most simple way is to have List of them:
List<HtmlInputCheckBox> arrCheckboxes = new List<HtmlInputCheckBox>();
...
...
HtmlInputCheckBox myCheckbox = new HtmlInputCheckBox();
arrCheckboxes.Add(myCheckbox);
...
This is C# but should be easy to translate to VB - anyhow having this, you can access the List and it should work.
Worst case as "last resort" you can simply iterate the whole Request.Form collection and look for Keys with name matching to the checkbox name.
Put this in the procedure...
Dim chkValue1 As New CheckBox
Dim chkValue2 As New CheckBox
'Find the Checkbox Controls in the PlaceHolder and cast them to the checkboxes we just made.
chkValue1 = CType(YourPlaceHolder.FindControl("Checkbox1ControlId"), CheckBox)
chkValue2 = CType(YourPlaceHolder.FindControl("Checkbox2ControlId"), CheckBox)
'Now you can do this...
Dim bolIsValue1Checked As Boolean = chkValue1.Checked

previouspage.findcontrol getting variable from previous page

i am trying to retain the value of a variable from a previous page. i believe this is the C# way to do this, but i would like the vb.net way can someone please help:
TextBox myTxt = (TextBox)Page.PreviousPage.FindControl("previousPageTextBox");
currentPageTextBox.text = myTxt.Text;
i would like to know how to code this in vb.net
i tried this:
Dim myTxt As TextBox = CType(Page.PreviousPage.FindControl("Textbox1"), TextBox)
TextBox1.Text = myTxt.Text
but i got this error msg:
use the new keyword to create an object instance
This should do it, also make sure you are using Server.Transfer to go between pages.. but I am sure you already know this:
Dim myTxt as TextBox = CType(Page.PreviousPage.FindControl("previousPageTextBox"), TextBox)
currentPageTextBox.text = myTxt.Text
dim txt as TextBox =CType( Page.PreviousPage.FindControl("previousPageTextBox"),
TextBox)
currentPageTextBox.Text = txt.Text
PS:- You need to set the previous page in the page where you want to find the control.
<%# PreviousPageType VirtualPath="~/Test.aspx" %>
Also better way will be to create an function on previous page which returns the text in textbox.
internal function TextBoxText() as string
return myTextPage.Text
end function
and use this on next page like this:
currentPageTextbox.Text = Page.PrevousPage.TextBoxText
let me know if this works because I've not used vb for long long time.
PPS:- It is only available if you user Server.Transfer or go to currentPage using some asp.net contorl like LinkButton from simple href links PreviousPage is null

Resources