p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   ASP.NET 2.0 Basics (http://p2p.wrox.com/asp-net-2-0-basics-136/)
-   -   Control a text box property from an Event (http://p2p.wrox.com/asp-net-2-0-basics/38810-control-text-box-property-event.html)

rit01 February 21st, 2006 11:27 AM

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?

Ta

Rit

jbenson001 February 22nd, 2006 02: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:
OnSelectedIndexChanged="ddl_SelectedIndexChanged"

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....

Jim





rit01 February 22nd, 2006 03: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

Rit

jbenson001 February 22nd, 2006 03: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.

Jim


rit01 February 22nd, 2006 03: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.

Rit

jbenson001 February 22nd, 2006 03:31 PM

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


rit01 February 22nd, 2006 03:32 PM

Cool,

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!

Rit

jbenson001 February 22nd, 2006 03:39 PM

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


rit01 February 22nd, 2006 04: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

Code:


<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:DropDownList>

<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..

Code:

Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As
 System.EventArgs)
        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.

Ta

Rit

jbenson001 February 22nd, 2006 11:32 PM

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 04:53 AM.

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