loop through controls to set attribute -tried recursive function-works when done statically

Hello All,

 I have a huge form and some of the textboxes are dynamic.

I want to loop through all controls, look for textboxes, and set the attribute.

When i set the attribute statically, as so:
caseDescriptiontxt.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + SaveOnlyBTN.UniqueID + "').click();return false;}} else {return true}; ")

Then it works fine... however, when I try to do the following loop to set the attributes, it does not set the attributes on any of the textboxes.. what am I doing wrong?

I call this on page load...    setEnterSubmit(Me)

Then i have the setEnterSubmit Sub.. and here it is...

Sub setEnterSubmit(ByVal page As Control)

For Each frmCtrl As Control In page.Controls

If TypeOf frmCtrl Is TextBox Then

'If Not CType(frmCtrl, TextBox).ID = "narrativetxt" Then

CType(frmCtrl, TextBox).Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + SaveOnlyBTN.UniqueID + "').click();return false;}} else {return true}; ")

'End If

End If

Next

End Sub

 

thanks in advance.....

0
it_professor
5/21/2007 12:40:41 AM
asp.net.web-forms 93655 articles. 5 followers. Follow

2 Replies
657 Views

Similar Articles

[PageSpeed] 24

Hello,

I am afraid you should use form1.Controls instead Page.Controls here : For Each frmCtrl As Control In page.Controls

Please refer to the following code sample which works fine


Partial Class DynamicButton
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
              Dim i As Integer = 0

        For i = 0 To 3
            Dim tb As New TextBox
            tb.ID = "txt" + i.ToString()
            form1.Controls.Add(tb)

        Next

    End Sub
       Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        For Each tb As Control In form1.Controls

            If (TypeOf tb Is TextBox) Then

                Dim tbt As TextBox = CType(tb, TextBox)
                tbt.Attributes.Add("onclick", "javascript:alert('ok');")

            End If


        Next
    End Sub
End Class

Hope it helps,

Jessica


Jessica Cao
Sincerely,
Microsoft Online Community Support


“Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
0
Jessica
5/22/2007 1:44:00 AM

thanks for your input.. but just changing to form1.controls don't fix it.

and your example ... well.. do this.. do the loop on page init.
and...on your example.. in the aspx page, add a place holder for those textboxes that you dynamically create. Then above that placeholder, and above your submit button.. in the aspx page add a separate dropdown that is handled by a separate submit button that the rest of the page.. then tell me if when your loop code works.. it won't.. somehow that other button, being before the textboxes, is messing things up and when I click enter the loop code is not stopping the submit.. but the submit is a submit for that top button event...

0
it_professor
5/29/2007 4:32:14 AM
Reply:

Similar Artilces:

loop through controls to set attribute- was working.. now with new button on form... the attributes are not being set
I am only opening a new thread, because the old thread is old and I am not getting a response. Please see old thread... http://forums.asp.net/t/1113116.aspx Basically, I am setting a javacript on each textbox, to keep them from posting the form when you click the enter key. The solution worked, however, since then I added dropdown and button before all the textboxes, in the form, and this button's job is to grab the data of previously posted forms and repopulate all the text fields on this massive form. Well, ever since I added this dropdown and new button, the loop to add the javascr...

Help! loop through controls to set attribute- was working..Help! now with new button on form... the attributes are not being set
Hello.. I really need some help on this! I am only opening a new thread, because the old thread is old and I am not getting a response. Please see old thread... http://forums.asp.net/t/1113116.aspx Basically, I am setting a javacript on each textbox, to keep them from posting the form when you click the enter key. The solution worked, however, since then I added dropdown and button before all the textboxes, in the form, and this button's job is to grab the data of previously posted forms and repopulate all the text fields on this massive form. Well, ever since I added this dropdown...

Implementing .Net Form into an APS.net web form, can this be done
I am new here, but have been searching for a while, and may not have the correct lingo to find what I am looking for.  I am tasked with implementing a .exe application that was writen vb6 then converted to .net into a new website my team is developing. The idea is to put each of the 3 different forms in this .exe application of 3 different .aspx pages. I have attempted multiple things to get this into the page and even started to just rewrite it as an ASP.net web form, but even then I can't reuse any of the code since the System.Web.UI.Page doesn't inherit the sa...

Looping through controls in VB.Net Web Form
I need to disable every imagebutton. In VB6, I would have done it like this: 1 Dim iButton As ImageButton 2 For Each iButton In Me.Controls 3 iButton.Enabled = False 4 Next In .Net, I get a "Specified cast is not valid" errror on line 2. Using Page.controls in place of Me.Controls also produces the same error. So, I tried this (and multiple variations): 1 Dim ibControl As Object 2 For Each ibControl In Me.Controls 3 If TypeOf ibControl Is ImageButton Then 4 ...

Is there any web form control that can work as IFRAME html control?????
in html pages, we can use iframe control in which we can load another pages and/or folder explorer. now, i'm developing a web page in which i like to show the folder explorer when user clicks a link. as a prototype, i developed an html page like below: <html> <head><title>Using iFrame to browse a folder</title> </head> <body bgcolor=cyan> <table left=5px top=5px id=tabMain width=90% height=90% border=1 cellspacing=1 cellpadding=1> <tr height=100%> <td width=20% align=center valign=middle> <INPUT TYPE="Butto...

Controlling web control instances on Web Form
I have created a composite control, extending functionality of the Hidden Control. However, I would like to prevent the user from adding this control to the page multiple times. I only want one instance of the control on the Web Form. What setting in my project to I adjust to enable this functionality? I can not find it in any of the books that I have referenced. Is this a setting or is this code that has to be added to the inheriting class. Looks like there is no standard apporach. But I'd consider using static field to count number of created objects and throw exception if second object ...

How to set tabindex to the controls if the total number of the controls on a web form is not sure?
Hi,I have a web form which has a multiview control along with other web controls. Each view of this multiview control has different number of web controls. So if user selects 1. the first view to load then the form has total 10 controls on it (5 control of the form, and 5 controls of the view selected by user)2. the second view to load then the form has total 8 controls on it (5 control of the form, and 3 controls of the view selected by user)3. the third view to load then the form has total 20 controls on it (5 control of the form, and 15 controls of the view selected by user)As i dont kn...

Web Form Controls do not work
Here is my problem. I have the professional version of .Net 1.0. Even though it says professional version in the about box, I though I have the student version. I have a 1.8GHz with 256 MB RAM. When I create an ASP Web Application, my Web Form tools do not appear in the brower when I invoke the browser. However, the contols do appear on the form during design time. This problem is consistent with all of my web form controls where just they only appear at design time and not at run time. Is there something that I must in order to get the web form controls to appear during run ...

Shell not working in web form but working with windows forms.
Hi! I want to run some exe files (Exa: Internet explorer) using shell command in asp.net. Its not working if its a web form. But working fine with a windows form. Pls. extend ur help. Thanx. What are you expecting to happen? How are you trying to run the exe files? Where do you expect it to run? Anything run through server side code will run on the server, and in an invisible window, at that. The program will run under the security context of the ASPNET user by default, and so likely will not have the proper authority to do much of what you want, unless you midify its rights...

how to call controls of web control form in coding of other forms?
hello everyone i have one problem i have created one webcontrol shoppingcart in which there are two labels one of qty and other of amt which are initially enable =falsenow on other page(aspx page) i have one txtbox of qty as txtqty i want tat as soon as user enters qty txtqty and clicks on add cart button it shd be display d value in lblqty of web control shopping cart make sure i m using datalist on aspx page so give me suggestions on tat basis and my coding is in vb. plz give ur suggestions as fast as possible. Neeti what do you mean by webcontrol.... it this user control or normal ...

How to access a control on a web form from anoter web form
hi all I have this scenarios i have two web forms, one called form1 and the other called form2 form1 has a label with id "lbl1" is it possible to access lbl1 from form2, change its text permenantly? thanks.   refer: http://dotnetslackers.com/Community/blogs/haissam/archive/2007/11/26/ways-to-pass-data-between-webforms.aspxSSNPlease remember to click "Mark as Answer" on the post that helps you. MinaSamy: hi all I have this scenarios i have two web forms, one called form1 and the other called form2 form1 has a label with id "lbl1" is it p...

with a theme set in a child page, setting attributes of a control in the master page don't work
I have noticed that if I dont't use a theme in a child page, the attributes such as bold, font-size, color of a control flow through from the Master Page. For example, if I have a label in a master page that has attributes set, it will show correctly when linking with the child pages if I don't have a theme for the child page, if I do have a them set, it shows standard black text with a default font.  Any help would be very much appreicated.  well, in your theme you might be overwrinting styles, more specially, the stylesheet. And if you're using skind, same is...

superreview granted: [Bug 288906] Return null for MSAA name when no name explicitly set for HTML form control : [Attachment 179538] If no name explictly set for form control, use SetIsVoid(null)
neil@parkwaycc.co.uk <neil.parkwaycc.co.uk@myrealbox.com> has granted Aaron Leventhal <aaronleventhal@moonset.net>'s request for superreview: Bug 288906: Return null for MSAA name when no name explicitly set for HTML form control https://bugzilla.mozilla.org/show_bug.cgi?id=288906 Attachment 179538: If no name explictly set for form control, use SetIsVoid(null) https://bugzilla.mozilla.org/attachment.cgi?id=179538&action=edit ...

superreview requested: [Bug 288906] Return null for MSAA name when no name explicitly set for HTML form control : [Attachment 179538] If no name explictly set for form control, use SetIsVoid(null)
Aaron Leventhal <aaronleventhal@moonset.net> has asked neil@parkwaycc.co.uk <neil.parkwaycc.co.uk@myrealbox.com> for superreview: Bug 288906: Return null for MSAA name when no name explicitly set for HTML form control https://bugzilla.mozilla.org/show_bug.cgi?id=288906 Attachment 179538: If no name explictly set for form control, use SetIsVoid(null) https://bugzilla.mozilla.org/attachment.cgi?id=179538&action=edit ...

.Net Windows Control in Web Form
Hi all,How can I use a .Net windows control in a web form? I use object tag but it doesn't work and displays empty.Thanks in advance cheers Hi, you can check this article out: Hosting a Windows Control in a Web Form. This shows only how to host the control. If you want interaction with the client you can do this by setting the correct CAS (Code Access Security) settings on the client pc. Grz, Kris.Read my blog. Handy Firefox plugins for web developers.Workaround for non working Mark as answer buttons. Thanks guys for your replycheers...

Web resources about - loop through controls to set attribute -tried recursive function-works when done statically - asp.net.web-forms

Fancy Hotel Wedding Planner Doesn’t Understand How Email’s “CC” Function Works
... inappropriate email to everyone in the entire office” moment, but there’s a difference between slipping up and hitting the “reply all” function, ...

How to dress for after-work functions
Avoiding casual calamities when invited to an out-of-work-hours function is quite an art.

Chlorinated Indium Tin Oxide Electrodes with High Work Function for Organic Device Compatibility
* To whom correspondence should be addressed. E-mail: michael.helander{at}utoronto.ca (M.G.H.); zhibin.wang{at}utoronto.ca (Z.B.W.); zhiwei.liu{at}utoronto.ca ...

Booze-less Christmas functions? In support of drinking at work parties
A booze-less work Christmas party? I recoiled in abject horror when I first heard this could be the prospect for some Australians. It was the ...

Study: Brain exercises work to improve mental function in HIV/AIDS patients
Click on the link within at the bottom and exercise your own brain.


Twitter Has Changed A Core Function Of How Twitter Works
Twitter has opened up the possibility of changing how its core function works. Dan Frommer at Quartz says Twitter updated the page that defines ...

DualShock 4 works with Windows PC for 'basic functions'
... confirmed the PlayStation 4 controller will be able to perform "basic functions" on computers and the "analog sticks and buttons will work just ...

Malicious Work with the modify-function-return-value Hack
The modify-function-return-value hack is particularly dangerous because it can be used to intercept function calls in code, user libraries, and ...

Can Holacracy Work? How Medium Functions Without Managers
This story-sharing and blogging platform doesn't need bosses to hover over its staff. After Ev Williams first started working on Twitter, he ...

Resources last updated: 1/14/2016 7:02:16 AM