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 followsException Details: System.InvalidCastException: Specified cast is not valid.
Source Error:
|
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 |
![]() |
Hi,
try using HtmlSelect instead of DropDownList
@ ((DropDownList)c).DataSource = mdt;
((DropDownList)c).DataTextField = "ClassID";
((DropDownList)c).DataValueField = "Course";
((DropDownList)c).DataBind();
![]() |
0 |
![]() |
Dragon.X.Dragon,
Thanks for your excellent help. My aspx page is now working.
![]() |
0 |
![]() |