Showing posts with label webpage. Show all posts
Showing posts with label webpage. Show all posts

Saturday, March 31, 2012

Trim Each textbox control - Repost

HI

I'm trying to iterate through all the textboxes on a webpage and trim
them for spaces.

i.e. If a user enters " hello world " we correct it to "hello
world"

So far I've come up with this:

------------------------

'Trim all textboxes
Dim ThisControl As System.Web.UI.Control
For Each ThisControl In Me.Controls

If ThisControl.GetType().ToString() =
"system.Web.UI.WebControls.TextBox" Then

ThisControl.text = Trim(ThisControl.text)
'Above line is wrong syntax, I want to trim the textbox here, but I'm
stuck! Please help...

End If

Next ThisControl

------------------------

The problem I have is not really with the trim function. It's the fact
that I cannot update the control text value. It won't allow the .text
property at all in this example because "thiscontrol" is not actually
aware that the control assigned to it is actually a textbox. Is there a
way to tell it it's a textbox so I can access this property, or is there
another way around this?

Can you help?

Many Thanks!

AlexHi,

here is an example how to loop through controls on the Page:
http://www.aspnet101.com/aspnet101/tips.aspx?id=97

If you want to access Text property, you need to have typed reference (cast
it to TextBox with CType and set the value to the property).

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke

"Alex Shirley" <postings@.alexshirley.com> wrote in message
news:e9704b08.0406280400.238ba501@.posting.google.c om...
> HI
> I'm trying to iterate through all the textboxes on a webpage and trim
> them for spaces.
> i.e. If a user enters " hello world " we correct it to "hello
> world"
> So far I've come up with this:
> -----------------------
--
> 'Trim all textboxes
> Dim ThisControl As System.Web.UI.Control
> For Each ThisControl In Me.Controls
> If ThisControl.GetType().ToString() =
> "system.Web.UI.WebControls.TextBox" Then
> ThisControl.text = Trim(ThisControl.text)
> 'Above line is wrong syntax, I want to trim the textbox here, but I'm
> stuck! Please help...
> End If
> Next ThisControl
> -----------------------
--
> The problem I have is not really with the trim function. It's the fact
> that I cannot update the control text value. It won't allow the .text
> property at all in this example because "thiscontrol" is not actually
> aware that the control assigned to it is actually a textbox. Is there a
> way to tell it it's a textbox so I can access this property, or is there
> another way around this?
>
> Can you help?
> Many Thanks!
> Alex
Thanks Teemu

However I'm still stuck with the casting the object I've discovered in
a for next loop to a textbox so I can access its properties in
VB/ASP.Net... dumb I know, but I've looked everywhere for an answer,
but obviously not in the right places!

So far I've got this code:

-----------

Dim ThisControl As System.Web.UI.Control
Dim txtControl As TextBox

For Each ThisControl In Me.Controls
If ThisControl.GetType().ToString() =
"system.Web.UI.WebControls.TextBox" Then
txtControl = CType(ThisControl, TextBox) ' am I casting this
correctly?
txtControl.Text = "I'vechanged" 'Doesn't work
End If
Next ThisControl

-----------

I tried ThisControl = CType(ThisControl, TextBox), but the properties
won't appear for thiscontrol.

Sorry but can you or somebody help in this casting so I can update or
get the text property?

Thanks

Alex

"Teemu Keiski" <joteke@.aspalliance.com> wrote in message news:<elLAShQXEHA.4064@.TK2MSFTNGP11.phx.gbl>...
> Hi,
> here is an example how to loop through controls on the Page:
> http://www.aspnet101.com/aspnet101/tips.aspx?id=97
> If you want to access Text property, you need to have typed reference (cast
> it to TextBox with CType and set the value to the property).
> --
> Teemu Keiski
> MCP, Microsoft MVP (ASP.NET), AspInsiders member
> ASP.NET Forum Moderator, AspAlliance Columnist
> http://blogs.aspadvice.com/joteke
> "Alex Shirley" <postings@.alexshirley.com> wrote in message
> news:e9704b08.0406280400.238ba501@.posting.google.c om...
> > HI
> > I'm trying to iterate through all the textboxes on a webpage and trim
> > them for spaces.
> > i.e. If a user enters " hello world " we correct it to "hello
> > world"
> > So far I've come up with this:
> > -----------------------
> --
> > 'Trim all textboxes
> > Dim ThisControl As System.Web.UI.Control
> > For Each ThisControl In Me.Controls
> > If ThisControl.GetType().ToString() =
> > "system.Web.UI.WebControls.TextBox" Then
> > ThisControl.text = Trim(ThisControl.text)
> > 'Above line is wrong syntax, I want to trim the textbox here, but I'm
> > stuck! Please help...
> > End If
> > Next ThisControl
> > -----------------------
> --
> > The problem I have is not really with the trim function. It's the fact
> > that I cannot update the control text value. It won't allow the .text
> > property at all in this example because "thiscontrol" is not actually
> > aware that the control assigned to it is actually a textbox. Is there a
> > way to tell it it's a textbox so I can access this property, or is there
> > another way around this?
> > Can you help?
> > Many Thanks!
> > Alex

Trim Each textbox control

HI
I'm trying to iterate through all the textboxes on a webpage and trim
them for spaces.
i.e. If a user enters " hello world " we correct it to "hello
world"
So far I've come up with this:
----
--
'Trim all textboxes
Dim ThisControl As System.Web.UI.Control
For Each ThisControl In Me.Controls
If ThisControl.GetType().ToString() =
"system.Web.UI.WebControls.TextBox" Then
ThisControl.text = Trim(ThisControl.text)
'Above line is wrong syntax, I want to trim the textbox here, but I'm
stuck! Please help...
End If
Next ThisControl
----
--
Can you help?
Many Thanks!
Alexuse RTrim() and LTrim().
R for right and L for the left side to remove spaces.
"Alex Shirley" <postings@.alexshirley.com> schreef in bericht
news:e9704b08.0406240318.6e352a45@.posting.google.com...
> HI
> I'm trying to iterate through all the textboxes on a webpage and trim
> them for spaces.
> i.e. If a user enters " hello world " we correct it to "hello
> world"
> So far I've come up with this:
> ----
--
> 'Trim all textboxes
> Dim ThisControl As System.Web.UI.Control
> For Each ThisControl In Me.Controls
> If ThisControl.GetType().ToString() =
> "system.Web.UI.WebControls.TextBox" Then
> ThisControl.text = Trim(ThisControl.text)
> 'Above line is wrong syntax, I want to trim the textbox here, but I'm
> stuck! Please help...
> End If
> Next ThisControl
> ----
--
> Can you help?
> Many Thanks!
> Alex
Hi Richard
The problem I have is not really with the trim function. It's the fact
that I cannot update the control text value. It won't allow the .text
property at all in this example because "thiscontrol" is not actually
aware that the control assigned to it is actually a textbox. Is there a
way to tell it it's a textbox so I can access this property, or is there
another way around this?
Any answers folks?
Cheers
Alex
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Trim Each textbox control

HI

I'm trying to iterate through all the textboxes on a webpage and trim
them for spaces.

i.e. If a user enters " hello world " we correct it to "hello
world"

So far I've come up with this:

------------------------

'Trim all textboxes
Dim ThisControl As System.Web.UI.Control
For Each ThisControl In Me.Controls

If ThisControl.GetType().ToString() =
"system.Web.UI.WebControls.TextBox" Then

ThisControl.text = Trim(ThisControl.text)
'Above line is wrong syntax, I want to trim the textbox here, but I'm
stuck! Please help...

End If

Next ThisControl

------------------------

Can you help?

Many Thanks!

Alexuse RTrim() and LTrim().
R for right and L for the left side to remove spaces.

"Alex Shirley" <postings@.alexshirley.com> schreef in bericht
news:e9704b08.0406240318.6e352a45@.posting.google.c om...
> HI
> I'm trying to iterate through all the textboxes on a webpage and trim
> them for spaces.
> i.e. If a user enters " hello world " we correct it to "hello
> world"
> So far I've come up with this:
> -----------------------
--
> 'Trim all textboxes
> Dim ThisControl As System.Web.UI.Control
> For Each ThisControl In Me.Controls
> If ThisControl.GetType().ToString() =
> "system.Web.UI.WebControls.TextBox" Then
> ThisControl.text = Trim(ThisControl.text)
> 'Above line is wrong syntax, I want to trim the textbox here, but I'm
> stuck! Please help...
> End If
> Next ThisControl
> -----------------------
--
> Can you help?
> Many Thanks!
> Alex
Hi Richard

The problem I have is not really with the trim function. It's the fact
that I cannot update the control text value. It won't allow the .text
property at all in this example because "thiscontrol" is not actually
aware that the control assigned to it is actually a textbox. Is there a
way to tell it it's a textbox so I can access this property, or is there
another way around this?

Any answers folks?

Cheers

Alex

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Trim Each textbox control - Repost

HI
I'm trying to iterate through all the textboxes on a webpage and trim
them for spaces.
i.e. If a user enters " hello world " we correct it to "hello
world"
So far I've come up with this:
----
--
'Trim all textboxes
Dim ThisControl As System.Web.UI.Control
For Each ThisControl In Me.Controls
If ThisControl.GetType().ToString() =
"system.Web.UI.WebControls.TextBox" Then
ThisControl.text = Trim(ThisControl.text)
'Above line is wrong syntax, I want to trim the textbox here, but I'm
stuck! Please help...
End If
Next ThisControl
----
--
The problem I have is not really with the trim function. It's the fact
that I cannot update the control text value. It won't allow the .text
property at all in this example because "thiscontrol" is not actually
aware that the control assigned to it is actually a textbox. Is there a
way to tell it it's a textbox so I can access this property, or is there
another way around this?
Can you help?
Many Thanks!
AlexHi,
here is an example how to loop through controls on the Page:
http://www.aspnet101.com/aspnet101/tips.aspx?id=97
If you want to access Text property, you need to have typed reference (cast
it to TextBox with CType and set the value to the property).
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke
"Alex Shirley" <postings@.alexshirley.com> wrote in message
news:e9704b08.0406280400.238ba501@.posting.google.com...
> HI
> I'm trying to iterate through all the textboxes on a webpage and trim
> them for spaces.
> i.e. If a user enters " hello world " we correct it to "hello
> world"
> So far I've come up with this:
> ----
--
> 'Trim all textboxes
> Dim ThisControl As System.Web.UI.Control
> For Each ThisControl In Me.Controls
> If ThisControl.GetType().ToString() =
> "system.Web.UI.WebControls.TextBox" Then
> ThisControl.text = Trim(ThisControl.text)
> 'Above line is wrong syntax, I want to trim the textbox here, but I'm
> stuck! Please help...
> End If
> Next ThisControl
> ----
--
> The problem I have is not really with the trim function. It's the fact
> that I cannot update the control text value. It won't allow the .text
> property at all in this example because "thiscontrol" is not actually
> aware that the control assigned to it is actually a textbox. Is there a
> way to tell it it's a textbox so I can access this property, or is there
> another way around this?
>
> Can you help?
> Many Thanks!
> Alex
Thanks Teemu
However I'm still stuck with the casting the object I've discovered in
a for next loop to a textbox so I can access its properties in
VB/ASP.Net... dumb I know, but I've looked everywhere for an answer,
but obviously not in the right places!
So far I've got this code:
Dim ThisControl As System.Web.UI.Control
Dim txtControl As TextBox
For Each ThisControl In Me.Controls
If ThisControl.GetType().ToString() =
"system.Web.UI.WebControls.TextBox" Then
txtControl = CType(ThisControl, TextBox) ' am I casting this
correctly?
txtControl.Text = "I'vechanged" 'Doesn't work
End If
Next ThisControl
I tried ThisControl = CType(ThisControl, TextBox), but the properties
won't appear for thiscontrol.
Sorry but can you or somebody help in this casting so I can update or
get the text property?
Thanks
Alex
"Teemu Keiski" <joteke@.aspalliance.com> wrote in message news:<elLAShQXEHA.4064@.TK2MSFTNGP1
1.phx.gbl>...
> Hi,
> here is an example how to loop through controls on the Page:
> http://www.aspnet101.com/aspnet101/tips.aspx?id=97
> If you want to access Text property, you need to have typed reference (cas
t
> it to TextBox with CType and set the value to the property).
> --
> Teemu Keiski
> MCP, Microsoft MVP (ASP.NET), AspInsiders member
> ASP.NET Forum Moderator, AspAlliance Columnist
> http://blogs.aspadvice.com/joteke
> "Alex Shirley" <postings@.alexshirley.com> wrote in message
> news:e9704b08.0406280400.238ba501@.posting.google.com...
> --
> --

Wednesday, March 28, 2012

Trouble binding a dropdown list in a detailsview.

Ok, I'm lost. I'm writing a webpage in VB/Dot Net 2.0 and I'm trying to put
a
dropdown list in a detailsview. The dropdownlist is supposed to display a
simple enum.
From what little I've seen so far, the place in VB code to bind a ddl is in
the detailview's databound event. Any earlier and you can't get a handle on
the ddl. The detailsview is essentially alone on it's page and is called in
either edit or insert mode only. (In the page lode, I examine a querystring
parameter and based on that, set the detailsview to either edit or insert
mode.) I have created template ddls in the column corresponding to this
variable. All existing records have valid values in this field (a smallint)
.
The code itself is routine:
ddlClassType = DetailsView1.FindControl("ddlClassType")
Dim names() As String = System.Enum.GetNames(GetType(ClassType))
Dim values() As Integer = System.Enum.GetValues(GetType(ClassType))
Dim i As Integer
For i = 0 To names.Length - 1
Dim item As New ListItem(names(i), values(i).ToString)
ddlClassType.Items.Add(item)
Next
This works fine for Inserts but the code never gets to this point for
updates. It crashes with the error: 'ddlClassType' has a SelectedValue whic
h
is invalid because it does not exist in the list of items. Parameter name:
value
Any suggestions?Howdy,
Moving population code into detail's DataBinding event should resolve the
problem. Take a look at your aspx code and notice, there's Bind or Eval
statement inside SelectedValue: SelectedValue='<%# Eval("WhatEver") %>' whic
h
expects items have already been populated. Unfortunatelly, you're populating
items afterwards, hence the exception.
hope this helps
--
Milosz
"B. Chernick" wrote:

> Ok, I'm lost. I'm writing a webpage in VB/Dot Net 2.0 and I'm trying to pu
t a
> dropdown list in a detailsview. The dropdownlist is supposed to display a
> simple enum.
> From what little I've seen so far, the place in VB code to bind a ddl is i
n
> the detailview's databound event. Any earlier and you can't get a handle
on
> the ddl. The detailsview is essentially alone on it's page and is called
in
> either edit or insert mode only. (In the page lode, I examine a querystrin
g
> parameter and based on that, set the detailsview to either edit or insert
> mode.) I have created template ddls in the column corresponding to this
> variable. All existing records have valid values in this field (a smallin
t).
>
> The code itself is routine:
> ddlClassType = DetailsView1.FindControl("ddlClassType")
> Dim names() As String = System.Enum.GetNames(GetType(ClassType))
> Dim values() As Integer = System.Enum.GetValues(GetType(ClassType))
> Dim i As Integer
> For i = 0 To names.Length - 1
> Dim item As New ListItem(names(i), values(i).ToString)
> ddlClassType.Items.Add(item)
> Next
> This works fine for Inserts but the code never gets to this point for
> updates. It crashes with the error: 'ddlClassType' has a SelectedValue wh
ich
> is invalid because it does not exist in the list of items. Parameter name:
> value
> Any suggestions?
>
Sorry, actually no. If I move the code into the DetailView's DataBinding
event (or the screen's), the code fails entirely because it can't find the
ddl. Hasn't been rendered yet.
I have a vague memory of populating a dropdown in my last job using
javascript and those <%# %> brackets, but I'm still digging through my notes
for that one. Have you ever done that?
"Milosz Skalecki [MCAD]" wrote:
> Howdy,
> Moving population code into detail's DataBinding event should resolve the
> problem. Take a look at your aspx code and notice, there's Bind or Eval
> statement inside SelectedValue: SelectedValue='<%# Eval("WhatEver") %>' wh
ich
> expects items have already been populated. Unfortunatelly, you're populati
ng
> items afterwards, hence the exception.
> hope this helps
> --
> Milosz
>
> "B. Chernick" wrote:
>
Many times. Paste the code.
--
Milosz
"B. Chernick" wrote:
> Sorry, actually no. If I move the code into the DetailView's DataBinding
> event (or the screen's), the code fails entirely because it can't find the
> ddl. Hasn't been rendered yet.
> I have a vague memory of populating a dropdown in my last job using
> javascript and those <%# %> brackets, but I'm still digging through my not
es
> for that one. Have you ever done that?
> "Milosz Skalecki [MCAD]" wrote:
>
Hi
On 17 Nov, 01:37, B. Chernick wrote:
> Sorry, actually no. If I move the code into the DetailView's DataBinding
> event (or the screen's), the code fails entirely because it can't find the
> ddl. Hasn't been rendered yet.
Milosz is right about the timing of the code causing the exception. In
insert mode, there is no attempt to automatically bind the selected
value of the ddl control to a field value because it's a new record,
whereas in Edit mode it tries to bind the selected value to the
current value of the field.
To get round this you'll have to delete the databinding between the
ddl and the datasource (in the edit mode template) and set the
selected value manually in code immediately after the code you have
already written. In the DataBound event, you can differentiate
betweeen the two modes using DetailsView1.CurrentMode() and make the
code for setting the ddl selected value conditonal upon that.
You'll also need to handle the update manually for the respective
datafield as well in the Updating event.
HTH
Thank you Milosz and Phil.
Out of town for the holiday but I continue this as soon as I get home Friday
.
"Phil H" wrote:

> Hi
> On 17 Nov, 01:37, B. Chernick wrote:
> Milosz is right about the timing of the code causing the exception. In
> insert mode, there is no attempt to automatically bind the selected
> value of the ddl control to a field value because it's a new record,
> whereas in Edit mode it tries to bind the selected value to the
> current value of the field.
> To get round this you'll have to delete the databinding between the
> ddl and the datasource (in the edit mode template) and set the
> selected value manually in code immediately after the code you have
> already written. In the DataBound event, you can differentiate
> betweeen the two modes using DetailsView1.CurrentMode() and make the
> code for setting the ddl selected value conditonal upon that.
> You'll also need to handle the update manually for the respective
> datafield as well in the Updating event.
> HTH
>
After some trial and error, I finally got it to work. Is this roughly what
you had in mind? (I first replaced the ddl in the edit template with a new
unbound dropdownlist.)
Private Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DetailsView1.DataBound
Dim names() As String = System.Enum.GetNames(GetType(ClassType))
Dim values() As Integer = System.Enum.GetValues(GetType(ClassType))
If DetailsView1.CurrentMode = DetailsViewMode.Edit Then
ddlClassType = DetailsView1.FindControl("ddlClassType")
Dim i As Integer
For i = 0 To names.Length - 1
Dim item As New ListItem(names(i), values(i).ToString)
ddlClassType.Items.Add(item)
Next
' Set incoming value
Dim dr As DataRowView
dr = DetailsView1.DataItem
ddlClassType.SelectedValue = dr.Item("ClassType").ToString
End If
End Sub
Private Sub ObjectDataSource1_Updating(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs) Handles
ObjectDataSource1.Updating
' manually update values here
ddlClassType = DetailsView1.FindControl("ddlClassType")
e.InputParameters.Item("ClassType") = ddlClassType.SelectedValue
End Sub
"Phil H" wrote:

> Hi
> On 17 Nov, 01:37, B. Chernick wrote:
> Milosz is right about the timing of the code causing the exception. In
> insert mode, there is no attempt to automatically bind the selected
> value of the ddl control to a field value because it's a new record,
> whereas in Edit mode it tries to bind the selected value to the
> current value of the field.
> To get round this you'll have to delete the databinding between the
> ddl and the datasource (in the edit mode template) and set the
> selected value manually in code immediately after the code you have
> already written. In the DataBound event, you can differentiate
> betweeen the two modes using DetailsView1.CurrentMode() and make the
> code for setting the ddl selected value conditonal upon that.
> You'll also need to handle the update manually for the respective
> datafield as well in the Updating event.
> HTH
>

Monday, March 26, 2012

Trouble posting to Database from Drop Down List and Text box

I'm having real trouble posting from the webpage back to a database. This is supposed to be one of the easiest things to do in ASP.NET. I've checked viewstate on all of the controls and at the page level and all are set to true.

When I type in some text information, no other characters but plane text, and hit the submit button the text disappears from the box, the drop downlist returns to default, the subroutine runs and an empty space is submitted to the database.

I've verified that the procedure in my namespace is running correctly by allowing the subroutine in the code behind to trigger the SPROC in the namespace. The 'hardcoded' values are inserted into the database.

I also placed a label.text = textbox.text into the subroutine and it is always set to a blank. Even on page intialization. None the less, I wanted to see if running the subroutine would place text inside of the label and it didn't. If I hard code the label, such as label.text = "stanley" then stanley appears when you click the link button. Thus, I'm sure that the subroutine is running, its appears that the text in the text box is being lost when the page makes a round trip.

Any ideas?

SethLet's see your code.
Be sure that you are wrapping any code that initializes the values textbox and dropdownlist in your Page_Load with a check for IsPostBack:


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
'Initialize initial values of textbox and dropdownlist here
End If
End Sub

If you don't, each time the page postsback, the controls will be reset back to their initial state, probably blank for the textbox and no selected item for the drop down list. Remember that when the page posts back, Page_Load will run BEFORE any of the web control event handlers.