How to access dynamically Html controls (client side controls) using C# server side scripts

Folks,
I have problem try to to access dynamically Html controls (client side controls) using C# server side scripts. Please give me helps. 
   The error is as follows

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error:

Line 79:         {
Line 80:             Control c = Page.FindControl("ddlTC" + i);
Line 81:             ((DropDownList)c).DataSource = mdt;
Line 82:             ((DropDownList)c).DataTextField = "ClassID";
Line 83:             ((DropDownList)c).DataValueField = "Course";

My aspx page source codes:

...
<script runat="server">
   
   
    void Page_Load(object s, EvenArgs e)
    {
        ...
 DataTable dt= new DataTable();
 ...
 // after I connect o db and fill in table dt, I call the following function
        fillTiedCoursesDropDownLists(dt); // when page first loading--> select top 1 CourseID, ClassID
    }
    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    void fillTiedCoursesDropDownLists(DataTable mdt)
    {
        //NOTE: ddlTCX where X=1,10 are client sides Select control. See below in Form body. runat=server for them are specified       
        int numDDLs = 10;
        for (int i = 1; i <= numDDLs; i++)
        {
            Control c = Page.FindControl("ddlTC" + i);
            ((DropDownList)c).DataSource = mdt;
            ((DropDownList)c).DataTextField = "ClassID";
            ((DropDownList)c).DataValueField = "Course";
            ((DropDownList)c).DataBind();
        }
    }
   
</script>

<form id="Form1" runat="server">

     <table>
                    <tr>
                       
                        <td><asp:TextBox id=CourseDescription1 Width="350" runat="server"></asp:TextBox></td><td><asp:DropDownList ID=ddlIB1 runat=server></asp:DropDownList></td><td><select id=ddlTC1 runat=server></select></td><td><input type=button id=btnTC1  value="Pick..." onclick="btnACClick('ddlTC1')" class=clsBtn style="width:40px" /></td>
                       
                    </tr>
                    <tr>   
                        <td><asp:TextBox id=CourseDescription2 Width="350"  runat="server"></asp:TextBox></td><td><asp:DropDownList ID=ddlIB2 runat=server></asp:DropDownList></td><td><select id=ddlTC2 runat=server></select></td><td><input type=button id=btnTC2 value="Pick..." onclick="btnACClick('ddlTC2')" class=clsBtn style="width:40px" /></td>
                       
                    </tr>
                    <tr>   
                        <td><asp:TextBox id=CourseDescription3 Width="350"  runat="server"></asp:TextBox></td><td><asp:DropDownList ID=ddlIB3 runat=server></asp:DropDownList></td><td><select id=ddlTC3 runat=server></select></td><td><input type=button id=btnTC3 value="Pick..." onclick="btnACClick('ddlTC3')" class=clsBtn style="width:40px" /></td>
                       
                    </tr>
                    <tr>                          
                        <td><asp:TextBox id=CourseDescription4 Width="350"  runat="server"></asp:TextBox></td><td><asp:DropDownList ID=ddlIB4 runat=server></asp:DropDownList></td><td><select id=ddlTC4 runat=server></select></td><td><input type=button id=btnTC4 value="Pick..." onclick="btnACClick('ddlTC4')" class=clsBtn style="width:40px" /></td>
                       
                    </tr>
                    <tr>                       
                        <td><asp:TextBox id=CourseDescription5 Width="350" runat="server"></asp:TextBox></td><td><asp:DropDownList ID=ddlIB5 runat=server></asp:DropDownList></td><td><select id=ddlTC5 runat=server></select></td><td><input type=button id=btnTC5 value="Pick..." onclick="btnACClick('ddlTC5')" class=clsBtn style="width:40px" /></td>
                       
                    </tr>
                    <tr>                       
                        <td><asp:TextBox id=CourseDescription6 Width="350" runat="server"></asp:TextBox></td><td><asp:DropDownList ID=ddlIB6 runat=server></asp:DropDownList></td><td><select id=ddlTC6 runat=server></select></td><td><input type=button id=btnTC6 value="Pick..." onclick="btnACClick('ddlTC6')" class=clsBtn style="width:40px" /></td>
                       
                     </tr>  
                    <tr>                       
                        <td><asp:TextBox id=CourseDescription7 Width="350"  runat="server"></asp:TextBox></td><td><asp:DropDownList ID=ddlIB7 runat=server></asp:DropDownList></td><td><select id=ddlTC7 runat=server></select></td><td><input type=button id=btnTC7 value="Pick..." onclick="btnACClick('ddlTC7')" class=clsBtn style="width:40px" /></td>
                       
                    </tr><tr>
                        <td><asp:TextBox id=CourseDescription8 Width="350"  runat="server"></asp:TextBox></td><td><asp:DropDownList ID=ddlIB8 runat=server></asp:DropDownList></td><td><select id=ddlTC8 runat=server></select></td><td><input type=button id=btnTC8 value="Pick..." onclick="btnACClick('ddlTC8')" class=clsBtn style="width:40px" /></td>
                       
                    </tr><tr>                       
                        <td><asp:TextBox id=CourseDescription9 Width="350"  runat="server"></asp:TextBox></td><td><asp:DropDownList ID=ddlIB9 runat=server></asp:DropDownList></td><td><select id=ddlTC9 runat=server></select></td><td><input type=button id=btnTC9 value="Pick..." onclick="btnACClick('ddlTC9')" class=clsBtn style="width:40px" /></td>
                       
                    </tr><tr>                       
                        <td><asp:TextBox id=CourseDescription10 Width="350" runat="server"></asp:TextBox></td><td><asp:DropDownList ID=ddlIB10 runat=server></asp:DropDownList></td><td><select id=ddlTC10 runat=server></select></td><td><input type=button id=btnTC10 value="Pick..." onclick="btnACClick('ddlTC10')" class=clsBtn style="width:40px" /></td>
                       
                    </tr>
                   
         </table>

</form>
</body>

 

 

0
johnaspnet
8/7/2006 10:01:24 PM
asp.net.web-forms 93655 articles. 5 followers. Follow

2 Replies
1847 Views

Similar Articles

[PageSpeed] 21

Hi,

  try using HtmlSelect instead of DropDownList

@         ((DropDownList)c).DataSource = mdt;
            ((DropDownList)c).DataTextField = "ClassID";
            ((DropDownList)c).DataValueField = "Course";
            ((DropDownList)c).DataBind();

0
Dragon
8/8/2006 2:34:27 AM
Dragon.X.Dragon,
  Thanks for your excellent help. My aspx page is now working.
0
johnaspnet
8/8/2006 1:43:54 PM
Reply:

Similar Artilces:

Is there a correspondance for each server side control to be equivalent to a client side html control
Where can find the list and can use that equivalence and what is its usage plsThanksThanks a lot, I appreciate your taking the time to help me. What exactly do you mean? A correspondence of what?All controls have a ClientID property, which for most controls tells the DOM element ID that will be used in the HTML rendering. Aside from that certain controls might offer additional client-side functionality, but there's no one standard.Thanks,EilonBlog: http://weblogs.asp.net/LeftSlipper/ I mean. When I am using html control in my aspx page like:  <input type=text ..>  and...

What are diff Server-side control and Client-side control ?
What are diff Server-side control and Client-side control ? and diff server -side coding and client -side coding ? please help I have answered the first part of your question in your previous posting. Server-side coding languages are ASP, ASP.NET, PHP, CGI (Perl) and many, many more. They are used to create dynamic web content (based on additional information that is attached to the request) and are often used to extract information from databases. Client-side coding languages are JavaScript, VBScript, EMACScript and are used to create dynamic content that responds to the u...

Combining a server-side control with a client-side control!
Hi all, I am trying to combine a server-side control with a client-side control. I have created a listbox in both sides, the client-side control's code is: <select size="4" name="lstFruit" id="Select1" ondblclick="lst_dblClick" visible="false"> <option value="Banana">Banana</option> <option value="Apple">Apple</option> <option value="Mango">Mango</option> <option value="Orange">Orange</option> </select> The other one is the server-side control which has this code:   I wanted to add the ondblClick attribute to the se...

How to access the properties and methods of the ActiveX control on the client side in Web Form using javascript or vbscript
Hi All, I have looked almost every where for this, and I'll be really appreciated if someone can help me with this. I found a article in MS KB article, #317392, but it is not extactly what I wanted. How do you reference an ActiveX control's methods and properties with javascript in the ASPX? Following is what I have. Some how the following line in the btnclick() does not seems to be working. AlarmCtl.StartTimer() I have done this with just .HTML and it works just fine. Can any one help. Thanks in advance. //===========================...

Passing Server Side Control to Client Side Script
Hi All, I have an asp:DropDownList and an asp:TextBox on my form. I want to disable or enable the textbox depending on the selection in the dropdown. I am using master pages and the problem is that when I use the onChange event for the dropdown on the client side, I don't know the ID of the the textbox on the client side to disable or enable it.  How can something like this be done? Thanks,AKA You can use the ClientID property of a control to determine the ID, something like:  <script type="text/javascript"> function ShowMeTheID(){ alert('...

Client side clock using server side controls
Is it possible to create a client side clock using a server side control (label or textbox) in C# without accessing JavaScript?  If so, how? In principal it is possible, it depends how often you need to reload this clock ... if every second than: 1. It is no sens to do I mean 2. You need than to make setTimeout client script, to refresh page every second With FastPage it seems more real task ... http://fastpage.more.at...

Validation Controls in .NET ( Client side or server side?)
 i have a very basic question. We have all these validation controls in asp.net ( required field, range validator and others)..my question is the required field validator is a server side control. So does that mean that it does a post back?..is it client side or server side control  The only controls that post back are buttons and other inputs where you set AutoPostback="True", such as a dropdownlist or a textbox.  Another way of posting back is using an UpdatePanel and placing a trigger in it for a specified control and/or event. All controls are considered clien...

server-side control inside another server-side control
Hi everyone. I have a requirement where I need to create a custom server side control which will contain a series of ASP controls -- no problem there. However, this component also needs to itself be placed inside a server side control, and then placed into the page...so I'll have a custom server-side component inside a custom server-side component. Is that even possible? Everytime I start to think about it I get a headache. Thanks Matt  It's perfectly possible. Just give it a try and you'll see that it works.-- "Mark As Answer" if my reply helped you -- Yup...

Web Server Controls and Client-side scripting
Hi guys, I jus twant to ask something. While using Html server controls I can access the Web Form elements using Javascript :<script language=javascript>      alert(document.<formID>.elementName.value); //elementName is just the ID value of a control like txtName for a textbox control.</script> But using Web Server Controls I can not access the Web Form elements.When I type like alert(document.StepByStep1_2.txtName.Value) , txtName doesn't appear in the intellisence drop down menu but using Html server controls all the controls pl...

web forms with client and server side controls??
Hi, I have a form that has client and server side controls. I have a problem in that any client controls (textbox) within the form runat="server" can not be accessed with client side code. Anyone got any ideas why and what work arounds are possible. Regards, Graham. once a form element is inside a <FORM> tag, you can no longer refer to it by its ID alone. you have to refer to it by formname.elementid or document.all.elementid. use document.getElementById(yourtextboxid) to get a ref to the element in the form. using this allows u to bypass the formn...

How to access Client Side control in server side button click event?
Hi all, I need to access client side control from server side code. For an example <form id="form1" runat="server"> <table> <tr> <td>UserName:</td> <td><input type="text" id="textBoxUserName" /></td> <td><asp:Button ID="buttonShow" runat="server" Text="show" OnClick="buttonShow_Click" /></td> </tr> </table>   </form> I need to access the textBoxUsername in the server side button click event. How to do that? Than...

How to access all the methods of server controls and infragistics controls from client-side javascript.
I have some web controls on my page, which i want to manipulate without going to the server, that is, from a client-side javascript. I know how to call them with the documents.forms.... but i do not know how to call their methods. For the text box, the method to manipulate it is 'value'. It's a property i think. for the html <select> it is the 'options' array. But for the others, I dont know.My question is the following:I have an infragistics control: the UltraWebTree.This is its code in html: <ignav:ultrawebtree id="UltraWebTree1" style="Z-INDEX: 108; LEFT: 352p...

validation, is it better to have client side or use server side validator controls
 Hi All. I want to do validation on text boxes etc. Is it better to do client side validation or just use the server side validation controls?   Hi!  If for server side controls you mean RequiredFieldValidator, CompareValidator, RegularExpressionValidator, etc.. i can tell you that they use client and server validation. For instance if you use a RequiredFieldValidator, on the client will be a validation, but on the server this condition will be evaluated again. Probably one of the objetives of this is deal with client with javascript disabled. Of course I ...

server side controls in a non-server side form
I want to use server side controls in a non-server side form.I use server side controls (like dropdownlists) so I can fill them with the right options from the database.My question is, what is the best way to approach this?CAN I use server-side controls in a client side form? If so, how can I get the values contained in the controls when I do a post? (not in a querystring).Here's a sample of my code: <form id="frmAdvancedSearch" method="post" action="advancedSearch.aspx"> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <input type="submit" /> </form>...

Web resources about - How to access dynamically Html controls (client side controls) using C# server side scripts - asp.net.web-forms

Placebase team at Apple file “Schematic Maps” patent dynamically detailing important data
In a report from Computerworld way back in 2009, we learned that Apple had quietly acquired Maps API company Placebase. Then, founder of Placebase ...

Apple's Lightning port dynamically assigns pins to allow for reversible use
... a closer look at how Apple's new Lightning connector works, and has come to the conclusion that the 8 pins on each side of the plug are dynamically ...

In a dynamically typed language, is it a bad idea to return different data types?
In some cases, returning different types from a single function works.

Urlbox Launches Screenshots-as-a-Service Platform to Dynamically Update Galleries
... changes. Launched yesterday, Urlbox allows anyone with a gallery displaying website screenshots to automate the process and keep them dynamically ...

Everything.me launcher dynamically adapts to what you’re doing right now
... phone according to what you’re doing. Perform a search for ‘football’ and your background will change to the NFL logo. A launcher that dynamically ...


A YouTube Experiment Resizes the Player Dynamically
There's a new YouTube experiment that changes the video player's size, depending on the browser window's size. When resizing Chrome's window, ...

Apple Granted “Dynamically Changing Cursor for User Interface” Patent, Originally Filed in 2003
... a utility patent that covers the use of the mouse cursor that changes according to the context of the task it is engaged in. Called a “Dynamically ...

Atmel's FPSLIC II Dynamically Reconfigurable SoC Supports "Silicon-Sharing" For Peripherals & Interfaces ...
Silicon-Sharing Fits 200K Gate Design into 40K Gate FPSLIC II's, Cuts Power Drain by 97%

Everything.me Launcher Wants to Dynamically Change Phones Based on Location, Interests, and Mental State ...
What if your phone’s launcher dynamically changed based upon interests, your location, or topics you had in mind at a specific moment? A new ...

Resources last updated: 11/25/2015 6:39:04 AM