p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 2.0 Basics (http://p2p.wrox.com/forumdisplay.php?f=136)
-   -   Control a text box property from an Event (http://p2p.wrox.com/showthread.php?t=38810)

rit01 February 21st, 2006 12:27 PM

Control a text box property from an Event
Hi All

I have a FormView control databound to an SQLDataSource and within the FormView I have several textboxes and a dropdownlist.

What I am trying to acheive is to be able to disable a text box depending on the value of the dropdownlist control. I have created an Formview_databound event but am having problems with

1. finding the value of the dropdownlist withint hat formview and
2. assigning the enabled="false" to my textboxes from within the event.

Basically I haven't a clue how to do this... Can anyone point me in the right direction regarding syntax?



jbenson001 February 22nd, 2006 03:59 PM

Ok there are a few things you need to do. First create a selectedindexchanged event for the dropdown list:

    Protected Sub ddl_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        'Get a refernce to the dropdownlist
        Dim ddl As DropDownList = CType(sender, DropDownList)
        Dim s As String

        'Get the selected text in the dropdown.
        s = ddl.SelectedItem.Text

        'Do something based on the text.
        Select Case s
            Case "Two"
                'Get the FormViewRow
                Dim fvr As FormViewRow = CType(ddl.Parent.Parent, FormViewRow)
                'Get the textbox(s) you need from that FormViewRow.
                Dim tb As TextBox = fvr.FindControl("DescriptionTextBox")
                tb.Enabled = False

        End Select
    End Sub

Then in the HTML view of the form, add this to the tag for the dropdownlist:

This is telling the dropdownlist to call that sub when the index changes. (You can call it what ever you want, just make sure the HTML name matches the sub name above)

Hope this helps... Let me know if you have any questions....


rit01 February 22nd, 2006 04:07 PM

You must be tapping into the minds of the needy!.. I am just this second scouring the net for an answer when your message from the heavens drops into my mailbox :-)...

I'll give it a go... The event needs to be more on the formview_load as disabling the texboxes need to happen as soon as the page is accessed or formview is loaded.

Many thanks


jbenson001 February 22nd, 2006 04:15 PM

I thought you needed to disable textboxes depending on what the user selected in the ddl. You can modify the code to work in the FormView_Databound event if you need to check values as the data is binding.


rit01 February 22nd, 2006 04:23 PM

Thanks Jim,
disabling the textbox is dependent on the selected vaue of a dropdownlist but its what is already selected when the form is databound - the selected value of the dropdownlist is stored within a DB... (if that made any sense)I have started tweaking your code to suit, but am doing it in the Formview_Load event... is this wrong or should it be on the databound?

Thank you for your valuable support... much appreciated.


jbenson001 February 22nd, 2006 04:31 PM

Use the FormView_Databound event since you need to grab values from the FormView as the data binds.

rit01 February 22nd, 2006 04:32 PM


I'll share my code once complete... not that you need it personally but just in case their are some 'needy' eyes looking in just like myself.

Many thanks again!


jbenson001 February 22nd, 2006 04:39 PM

Good to post for others who need the help.. :)

rit01 February 22nd, 2006 05:04 PM

Voila.. thanks Jim!!

In the HTML - Formview, as I have 3 Textboxes that I needed to control the Enabled property of what I did was set each textbox Enabled Property to False as so


<asp:FormView DefaultMode="Edit" ID="FormView1" runat="server"
OnDataBound="FormView1_DataBound" … other bits…

<asp:DropDownList ID="MyDropdownList" runat="server" ..other bits..
        SelectedValue='<%# Bind("DatafromDB") %>'>

<asp:TextBox ID="txtbox1" Enabled="false"
runat="server" ></asp:TextBox>

<asp:TextBox ID="txtbox2" Enabled="false"
runat="server" ></asp:TextBox>

<asp:TextBox ID="txtbox3" Enabled="false"
runat="server" ></asp:TextBox>

An then the code-behind stuff for referencing/manipulating the controls within the FormView went like this..


Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As
        Dim DDList As DropDownList =
CType(FormView1.FindControl("MyDropdownList"), DropDownList)
        Dim s As String
        s = DDList.SelectedItem.Value

        Select Case s
            Case 1
                Dim box1 As TextBox =
CType(FormView1.FindControl("txtbox1"), TextBox)
                box1.Enabled = True
            Case 2
                Dim box2 As TextBox =
CType(FormView1.FindControl("txtbox2"), TextBox)
                Box2.Enabled = True
            Case 3
                Dim box3 As TextBox =
CType(FormView1.FindControl("txtbox3"), TextBox)
                Box3.Enabled = True
        End Select
    End Sub

If the above can be improved in anyway, please feel free to tweak.. always time for educating.



jbenson001 February 23rd, 2006 12:32 AM

Glad to help.. :) The code looks fine. I don't think you have to change the name for each textbox you declare. You can keep them all the same, say dim box as textbox in all the case statements. But that is a minor thing and will not cause any problems. Just letting you know they can be the same since they are declared in separate case statemets.

glad you got it working... :)

All times are GMT -4. The time now is 02:42 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.