|
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
|
|
|
February 21st, 2006, 12:27 PM
|
Friend of Wrox
|
|
Join Date: Oct 2005
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
|
|
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
|
February 22nd, 2006, 03:59 PM
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
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
|
February 22nd, 2006, 04:07 PM
|
Friend of Wrox
|
|
Join Date: Oct 2005
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
|
|
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
|
February 22nd, 2006, 04:15 PM
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
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
|
February 22nd, 2006, 04:23 PM
|
Friend of Wrox
|
|
Join Date: Oct 2005
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
|
|
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
|
February 22nd, 2006, 04:31 PM
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
Use the FormView_Databound event since you need to grab values from the FormView as the data binds.
|
February 22nd, 2006, 04:32 PM
|
Friend of Wrox
|
|
Join Date: Oct 2005
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
|
|
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
|
February 22nd, 2006, 04:39 PM
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
Good to post for others who need the help.. :)
|
February 22nd, 2006, 05:04 PM
|
Friend of Wrox
|
|
Join Date: Oct 2005
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
|
|
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
|
February 23rd, 2006, 12:32 AM
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
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... :)
|
|
|