|
|
 |
BOOK: Beginning ASP.NET 2.0 Databases Beta Preview  | This is the forum to discuss the Wrox book Beginning ASP.NET 2.0 Databases: Beta Preview by John Kauffman, Thiru Thangarathinam; ISBN: 9780764570810 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning ASP.NET 2.0 Databases Beta Preview section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|

May 25th, 2005, 03:48 PM
|
|
Registered User
|
|
Join Date: May 2005
Location: woods cross, utah, USA.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
referencing controls in templates in form_view
I am having problems referencing controls and their properities the various templates in form_view. I need to set the visible property to false on a button in the item_template. I looked in the HELP section and there was a reference hyperlink called "Web Forms Control Identification" but the link is invalid. I have tried to do the following in the page_load subroutine.
dim var_deletebutton as new button
var_deletebutton = CType(me.formview1.findcontrol("bt_Delete"), button)
var_deletebutton.visible = false
I get this error when the program executes the second line of code:
"There is no source code available for the current location".
Can anyone help me?
thanks
dave potter:)
|

May 25th, 2005, 05:19 PM
|
 |
Wrox Author
Points: 33,554, Level: 80 |
|
|
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,228
Thanks: 7
Thanked 203 Times in 201 Posts
|
|
How does your markup for the Form View look like? And what template are you trying to access? Is the FormView's FormViewMode valid for the template you're accessing? That is, are you sure that, say, your FormView is in Edit mode and you're trying to access controls in the Edit template?
The code you posted works for me in a number of pages. BTW, there is no need to instantiate an instance of the Button. The FindControl method will do that for you:
Code:
Dim var_deletebutton As <s>new </s>Button
var_deletebutton = CType(Me.FormView1.FindControl("bt_Delete"), Button)
var_deletebutton.Visible = False
HtH,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|

May 25th, 2005, 06:26 PM
|
|
Registered User
|
|
Join Date: May 2005
Location: woods cross, utah, USA.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks Imar for your response. Here are the answers to your questions.
(1) What Template are you trying to access? - Item_template
(2)Is the formView's formviewmode valid for the template you're accessing? - The form opens up in the select mode not edit mode. On this screen I want to show or hide the "DELETE" button.
(3) I took out "NEW" and still had the same error.
(4) Here is some of the code in the ITEMTEMPLATE
<ItemTemplate>
<asp:Button ID="bt_Delete" runat="server" CausesValidation="False" CommandName="delete"Font-Bold="True" Font-Names="Arial" Font-Size="8pt" Style="z-index: 102; left: 672px; position: absolute; top: 56px" Text="Delete Record" Width="88px" />
<asp:Button ID="bt_edit" runat="server" CausesValidation="False" CommandName="edit" Font-Bold="True" Font-Names="Arial" Font-Size="8pt" Style="z-index: 102; left: 656px;
position: absolute; top: -78px" Text="Edit" UseSubmitBehavior="False"
Width="120px" />
In my page_load subroutine, I check to see if the user has permission to delete a record, if they don't I want to make the button invisible.
thanks for your help.
dave
|

May 25th, 2005, 06:37 PM
|
|
Registered User
|
|
Join Date: May 2005
Location: woods cross, utah, USA.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I got it to work ... when I just ran it. It failed only when I put a break point on it and tried running it by hitting F11 until I came to the line.
THANKS FOR ALL YOUR HELP :)
|

May 26th, 2005, 01:24 PM
|
 |
Wrox Author
Points: 33,554, Level: 80 |
|
|
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,228
Thanks: 7
Thanked 203 Times in 201 Posts
|
|
Glad it's working, although it's strange that it fails when debugging....
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|

May 29th, 2005, 05:01 PM
|
|
Registered User
|
|
Join Date: May 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by Imar
Glad it's working, although it's strange that it fails when debugging....
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
Hi Imar
I am having a similar problem in VS2005 I am trying to access 2 DropDownLists in a FormView control in edit mode. Everything that I try wont let me access the control. I have tried using the FindControl Method but it never seems to return the control and I get Object Reference Not Set to an Instance of Object. Here is the code.
Dim var_ddl1 As New DropDownList
Dim var_ddl2 As New DropDownList
Dim var_txt1 As New TextBox
Dim var_txt2 As New TextBox
var_ddl1 = CType(Me.FormView1.FindControl("DropDownList1"), DropDownList)
var_ddl2 = CType(Me.FormView1.FindControl("DropDownList2"), DropDownList)
var_txt1 = CType(Me.FormView1.FindControl("txtAppID"), TextBox)
var_txt2 = CType(Me.FormView1.FindControl("txtPIID"), TextBox)
var_ddl1.SelectedValue = var_txt1.Text
var_ddl2.SelectedValue = var_txt2.Text
What I want to eventually do is access the DropDownList1 SelectIndexChange method and re-bind the DropDownList2 control.
Is what I want to do even possible?
|

May 29th, 2005, 05:03 PM
|
|
Registered User
|
|
Join Date: May 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Imar
I am having a similar problem in VS2005 I am trying to access 2 DropDownLists in a FormView control in edit mode. Everything that I try wont let me access the control. I have tried using the FindControl Method but it never seems to return the control and I get Object Reference Not Set to an Instance of Object. Here is the code.
Dim var_ddl1 As New DropDownList
Dim var_ddl2 As New DropDownList
Dim var_txt1 As New TextBox
Dim var_txt2 As New TextBox
var_ddl1 = CType(Me.FormView1.FindControl("DropDownList1"), DropDownList)
var_ddl2 = CType(Me.FormView1.FindControl("DropDownList2"), DropDownList)
var_txt1 = CType(Me.FormView1.FindControl("txtAppID"), TextBox)
var_txt2 = CType(Me.FormView1.FindControl("txtPIID"), TextBox)
var_ddl1.SelectedValue = var_txt1.Text
var_ddl2.SelectedValue = var_txt2.Text
What I want to eventually do is access the DropDownList1 SelectIndexChange method and re-bind the DropDownList2 control.
Is what I want to do even possible?
|

May 30th, 2005, 03:03 PM
|
 |
Wrox Author
Points: 33,554, Level: 80 |
|
|
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,228
Thanks: 7
Thanked 203 Times in 201 Posts
|
|
Hi there,
You don't just "access the SelectedIndexChanged" event; that event is fired by the run-time for you, so you can write code for it.
Anyway, no need for all this wizardry; all you need is an event defined on the drop-down:
<asp:FormView ID="FormView1" runat="server" DefaultMode="Insert">
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="MyMethod">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
</asp:DropDownList>
</InsertItemTemplate>
</asp:FormView>
and then in your code behind:
Protected Sub MyMethod(ByVal sender As Object, ByVal e As System.EventArgs)
Dim selectedValue As String = _
CType(FormView1.FindControl("DropDownList1"), DropDownList).SelectedValue
End Sub
FindControl will search in the current "NamingContainer" which makes it possible to return the drop down list, even though it's actually inside an EditItemTemplate or possible even inside, say an Html table.
I think that should do the trick.
One other thing: this code is not very efficient:
Dim var_ddl1 As New DropDownList
var_ddl1 = CType(Me.FormView1.FindControl("DropDownList1"), DropDownList)
First, you declare a variable named var_ddl1 (which, IMO, is a very confusing name; all variables are variables, so there is no need to prefix them with var; do yourself a favor (and all your (future) colleagues as well) and use something like lstEmployeeNumber, or lstState or even State or ddlState; these names are much easier to read *and* understand) and then you assign it a *new* instance of a DropDownList, which will be created in memory for you.
The next line then assigns the return value of the FindControl method to var_ddl1 (did I already mention that's a confusing name?) effectively overwriting the first one. SO, the first one is actually never used and is just a waste of Cpu cycles and memory.
This is a lot better:
Dim var_ddl1 As DropDownList
var_ddl1 = CType(Me.FormView1.FindControl("DropDownList1"), DropDownList)
or even:
Dim var_ddl1 As DropDownList = CType(Me.FormView1.FindControl("DropDownList1"), DropDownList)
or yet even better:
Dim lstState As DropDownList = CType(Me.FormView1.FindControl("lstState"), DropDownList) .... ;)
HtH,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Freaky Styley by Red Hot Chili Peppers (Track 6 from the album: Freaky Styley) What's This?
|

May 30th, 2006, 12:08 PM
|
|
Registered User
|
|
Join Date: May 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi, Imar.
How would I write the same code in ASP .NET C# in order to set the visible property value to false on a button in the item_template. Thanks.
|

May 30th, 2006, 02:49 PM
|
 |
Wrox Author
Points: 33,554, Level: 80 |
|
|
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,228
Thanks: 7
Thanked 203 Times in 201 Posts
|
|
C# doesn't use CType, but you can put the name of the control type in parentheses before an object to cast it. Something like this would work:
Button myButton = (Button) FormView1.FindControl("Button1");
myButton.Visible = false;
For more help with code converting issues, you may want to take a look at the code converter at Dot Net Taxi: http://www.dotnettaxi.com/Tools/Converter.aspx
HtH,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |