Hello,
I have a panel which contains various textboxes and dropdownlists.
I want to loop through each control inside the panel and detected if it is a DropDownList or a TextBox so I can access its SelectedValue or Text.
How can I do this?
Thanks,
Miguel
Hope this helps:
Dim frmCtrl As Control
For each frmCtrl in oPanel.Controls
If TypeOf frmCtrl Is TextBox Then
End If
etc, etc
Next
or C#
foreach (Control ctrl in Panel.Controls)
{
if (ctrl is TextBox)
{
}
}
try this code:
for each c as control in myPanel.controls
if typeof c is TextBox then
'get text
elseif typeof c is DropDown then
'get selected value
endif
next
Hello Miguel my friend,
I have done this page for you to show you how.
<%
@.PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default" %><!
DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><
htmlxmlns="http://www.w3.org/1999/xhtml"><
headrunat="server"><title>Untitled Page</title></
head><
body><formid="form1"runat="server"><div><asp:PanelID="pnlTest"runat="server">
<asp:TextBoxID="txtName"runat="server"Text="Scott"/>
<asp:DropDownListID="ddlColours"runat="server"><asp:ListItemValue="1">Red</asp:ListItem><asp:ListItemValue="2">Blue</asp:ListItem><asp:ListItemValue="3">Green</asp:ListItem></asp:DropDownList>
</asp:Panel>
<asp:Buttonid="btnTest"runat="server"OnClick="btnTest_Click"Text="Try"/>
</div></form>
</
body></
html>
using
System;using
System.Data;using
System.Configuration;using
System.Web;using
System.Web.Security;using
System.Web.UI;using
System.Web.UI.WebControls;using
System.Web.UI.WebControls.WebParts;using
System.Web.UI.HtmlControls;public
partialclass_Default : System.Web.UI.Page{
protectedvoid Page_Load(object sender,EventArgs e){
}
protectedvoid btnTest_Click(object sender,EventArgs e){
foreach (Control ctlin pnlTest.Controls){
if (ctl.ID !=null){
if (ctl.ID.StartsWith("txt")){
TextBox txtBox = (TextBox)ctl;Response.Write(txtBox.Text +
"<br/><br/>");}
else{
if (ctl.ID.StartsWith("ddl")){
DropDownList ddlList = (DropDownList)ctl;Response.Write(ddlList.SelectedItem.Value +
"<br/><br/>");}
}
}
}
}
}
Kind regards
Scotty
Thank you all!
It works just fine.
Thanks,
Miguel
0 comments:
Post a Comment