Convert VB code to C#

G

Guest

Private Sub Button1_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Dim cntrlTester As Control
Dim chkTester As CheckBox
Dim sb As New System.Text.StringBuilder
For Each cntrlTester In _
Page.Controls(1).Controls
If TypeOf cntrlTester Is _
System.Web.UI.WebControls.CheckBox Then
chkTester = cntrlTester
sb.Append(chkTester.ID & _
" : " & chkTester.Checked.ToString & _
"<br>")
End If
Next
Label1.Text = sb.ToString
End Sub

<form id="Form1" method="post" runat="server"><p><asp:CheckBox id="CheckBox1" runat="server"
text="checkbox1"></asp:checkbox></p><p><asp:CheckBox id="CheckBox2" runat="server"
text="checkbox2"></asp:checkbox></p><p><asp:CheckBox id="CheckBox3" runat="server"
text="checkbox3"></asp:checkbox></p><p><asp:Label id="Label1" runat="server"></asp:label></p><p><asp:Button id="Button1" runat="server" text="button"></asp:button></p></form

TO C# code behind for Visual Studio .NE

Thanks

bebop
 
M

Morten Wennevik

Code is untested but this is might work:

// this line needs to be added to the button creation
Button1.Click += new EventHandler(Button1_Click);

private Button1_Click(object sender, System.EventArgs e)
{
Control cntrlTester;
CheckBox chkTester;
System.Text.StringBuilder sb = new System.Text.StringBuilder();

foreach(cntrlTester in Page.Controls[1].Controls)
{
if(cntrlTester is System.Web.UI.WebControls.CheckBox)
{
chkTester = cntrlTester;
sb.Append(chkTester.ID + " : " + chkTester.Checked.ToString() + "<br>");
}
}

Label1.Text = sb.ToString();
}

The rest should be the same.
 
G

Guest

I tried putting the converted C# code into the code behind

Button1.Click += new EventHandler(Button1_Click); //Where should this line be in the code behind

private Button1_Click(object sender, System.EventArgs e

Control cntrlTester
CheckBox chkTester
System.Text.StringBuilder sb = new System.Text.StringBuilder()

foreach(cntrlTester in Page.Controls[1].Controls) //I get message: Type and identifier are both required in a foreach statemen


if(cntrlTester is System.Web.UI.WebControls.CheckBox

chkTester = cntrlTester
sb.Append(chkTester.ID + " : " + chkTester.Checked.ToString() + "<br>")



Label1.Text = sb.ToString()


Any help would be appreciated

Thanks

bebop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top