Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 21st, 2006, 12:27 PM
Friend of Wrox
 
Join Date: Oct 2005
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
Default 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
__________________
Rit
www.designandonline.co.uk
INSPIRE | CREATE | DELIVER
 
Old February 22nd, 2006, 03:59 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

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




 
Old February 22nd, 2006, 04:07 PM
Friend of Wrox
 
Join Date: Oct 2005
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
Default

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
 
Old February 22nd, 2006, 04:15 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

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

 
Old February 22nd, 2006, 04:23 PM
Friend of Wrox
 
Join Date: Oct 2005
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
Default

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
 
Old February 22nd, 2006, 04:31 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

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

 
Old February 22nd, 2006, 04:32 PM
Friend of Wrox
 
Join Date: Oct 2005
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
Default

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
 
Old February 22nd, 2006, 04:39 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

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

 
Old February 22nd, 2006, 05:04 PM
Friend of Wrox
 
Join Date: Oct 2005
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
Default

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
 
Old February 23rd, 2006, 12:32 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

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






Similar Threads
Thread Thread Starter Forum Replies Last Post
max length property of multiline text box MunishBhatia ASP.NET 2.0 Professional 5 June 14th, 2007 01:55 AM
Text Box .text Property Displays conflicting Valus JPOhlman Visual Basic 2005 Basics 3 August 23rd, 2006 02:39 PM
text-property in a non-server control propellis .NET Web Services 0 July 4th, 2006 03:29 AM
Can't set text property of dynamic text box sreerajrs ASP.NET 1.0 and 1.1 Professional 1 May 31st, 2006 09:39 PM
User Control Name in a Text Property laslanid Pro VB.NET 2002/2003 0 February 24th, 2005 04:31 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.