Showing posts with label named. Show all posts
Showing posts with label named. Show all posts

Saturday, March 31, 2012

Text Box Problem

Hello,
I have a master page where my form has the Runat="server".
The master page as a control place holder, named "cphContent".
Then in a page which inherits from the master page I added a custom
control which I created.
This custom control is added to "cphContent" at runtime.
The custom control creates and adds 2 textboxes using
CreateChildControls.
I am getting an error:
System.Web.HttpException: Control 'ctl00_cphContent_by27cf_tbName' of
type 'TextBox' must be placed inside a form tag with runat=server.
I have no idea why do I get this error.
The form with Runat="server" is in the master page.
Anyway, can someone tell me how can I figure what is going on?
Thanks,
MiguelHere is my code.
I am adding and creating all controls at runtime in my pages.
---
Base.master:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="fBase" runat="server"></form>
</body>
</html>
Base.master.vb:
Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
Handles Me.Init
Dim pContainer As New Panel
AddHandler pContainer.Init, AddressOf pContainer_Init
Me.Controls.Add(pContainer)
Dim cphContent As New ContentPlaceHolder
AddHandler cphContent.Init, AddressOf cphContent_Init
pContent.Controls.Add(cphContent)
End Sub
MyPage.aspx:
<%@. Page Language="VB" CodeFile="Default.aspx.vb" Inherits="_Default"
%>
MyPage.aspx.vb:
Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
Handles Me.Init
Dim by27fContactForm As New By27.Web.UI.Form.Contact
AddHandler by27fContactForm.Init, AddressOf by27fContactForm_Init
pContacts.Controls.Add(by27fContactForm)
End Sub
Class Contact in Namespace By27.Web.UI:
Namespace Web.UI.Form
' Content
<DefaultProperty("Text"), ToolboxData("<{0}:ContactForm
runat=server></{0}:ContactForm>")> _
Public Class Contact
Inherits WebControl
Dim by27cf_tbName As New WebControls.TextBox
Private Sub by27cf_tbName_Init(ByVal sender As Object, ByVal e As
System.EventArgs)
With by27cf_tbName
.CssClass = Me.TextBoxCssClass
.ID = "by27cf_tbName"
.Width = Unit.Percentage(100)
End With
End Sub
<Bindable(True), Category("Appearance"), DefaultValue(""),
Localizable(True)> Property TextBoxCssClass() As String
Get
If CStr(ViewState("TextBoxCssClass")) Is Nothing Then
Return String.Empty
Else
Return CStr(ViewState("TextBoxCssClass"))
End If
End Get
Set(ByVal Value As String)
ViewState("TextBoxCssClass") = Value
End Set
End Property
Protected Overrides Sub CreateChildControls()
AddHandler by27cf_tbName.Init, AddressOf by27cf_tbName_Init
MyBase.Controls.Add(by27cf_tbName)
' Create child controls
MyBase.CreateChildControls()
Me.ChildControlsCreated = True
End Sub
End Class
---
Thanks,
Miguel
shapper wrote:
> Hello,
> I have a master page where my form has the Runat="server".
> The master page as a control place holder, named "cphContent".
> Then in a page which inherits from the master page I added a custom
> control which I created.
> This custom control is added to "cphContent" at runtime.
> The custom control creates and adds 2 textboxes using
> CreateChildControls.
> I am getting an error:
> System.Web.HttpException: Control 'ctl00_cphContent_by27cf_tbName' of
> type 'TextBox' must be placed inside a form tag with runat=server.
> I have no idea why do I get this error.
> The form with Runat="server" is in the master page.
> Anyway, can someone tell me how can I figure what is going on?
> Thanks,
> Miguel
This is a bug of Visual Studio. A problem is that in content page you don't
have form
"shapper" <mdmoura@.gmail.com> wrote in message
news:1163016480.466822.19390@.m7g2000cwm.googlegroups.com...
> Hello,
> I have a master page where my form has the Runat="server".
> The master page as a control place holder, named "cphContent".
> Then in a page which inherits from the master page I added a custom
> control which I created.
> This custom control is added to "cphContent" at runtime.
> The custom control creates and adds 2 textboxes using
> CreateChildControls.
> I am getting an error:
> System.Web.HttpException: Control 'ctl00_cphContent_by27cf_tbName' of
> type 'TextBox' must be placed inside a form tag with runat=server.
> I have no idea why do I get this error.
> The form with Runat="server" is in the master page.
> Anyway, can someone tell me how can I figure what is going on?
> Thanks,
> Miguel
>

Text Box Problem

Hello,

I have a master page where my form has the Runat="server".
The master page as a control place holder, named "cphContent".
Then in a page which inherits from the master page I added a custom
control which I created.
This custom control is added to "cphContent" at runtime.
The custom control creates and adds 2 textboxes using
CreateChildControls.

I am getting an error:
System.Web.HttpException: Control 'ctl00_cphContent_by27cf_tbName' of
type 'TextBox' must be placed inside a form tag with runat=server.

I have no idea why do I get this error.
The form with Runat="server" is in the master page.

Anyway, can someone tell me how can I figure what is going on?

Thanks,
MiguelHere is my code.

I am adding and creating all controls at runtime in my pages.

----------------------

Base.master:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="fBase" runat="server"></form>
</body>
</html>

Base.master.vb:

Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
Handles Me.Init

Dim pContainer As New Panel
AddHandler pContainer.Init, AddressOf pContainer_Init
Me.Controls.Add(pContainer)

Dim cphContent As New ContentPlaceHolder
AddHandler cphContent.Init, AddressOf cphContent_Init
pContent.Controls.Add(cphContent)

End Sub

MyPage.aspx:

<%@. Page Language="VB" CodeFile="Default.aspx.vb" Inherits="_Default"
%>

MyPage.aspx.vb:

Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
Handles Me.Init

Dim by27fContactForm As New By27.Web.UI.Form.Contact
AddHandler by27fContactForm.Init, AddressOf by27fContactForm_Init
pContacts.Controls.Add(by27fContactForm)

End Sub

Class Contact in Namespace By27.Web.UI:

Namespace Web.UI.Form

' Content
<DefaultProperty("Text"), ToolboxData("<{0}:ContactForm
runat=server></{0}:ContactForm>")_
Public Class Contact
Inherits WebControl

Dim by27cf_tbName As New WebControls.TextBox
Private Sub by27cf_tbName_Init(ByVal sender As Object, ByVal e As
System.EventArgs)
With by27cf_tbName
.CssClass = Me.TextBoxCssClass
.ID = "by27cf_tbName"
.Width = Unit.Percentage(100)
End With
End Sub

<Bindable(True), Category("Appearance"), DefaultValue(""),
Localizable(True)Property TextBoxCssClass() As String
Get
If CStr(ViewState("TextBoxCssClass")) Is Nothing Then
Return String.Empty
Else
Return CStr(ViewState("TextBoxCssClass"))
End If
End Get
Set(ByVal Value As String)
ViewState("TextBoxCssClass") = Value
End Set
End Property

Protected Overrides Sub CreateChildControls()
AddHandler by27cf_tbName.Init, AddressOf by27cf_tbName_Init
MyBase.Controls.Add(by27cf_tbName)
' Create child controls
MyBase.CreateChildControls()
Me.ChildControlsCreated = True
End Sub

End Class

----------------------

Thanks,

Miguel

shapper wrote:

Quote:

Originally Posted by

Hello,
>
I have a master page where my form has the Runat="server".
The master page as a control place holder, named "cphContent".
Then in a page which inherits from the master page I added a custom
control which I created.
This custom control is added to "cphContent" at runtime.
The custom control creates and adds 2 textboxes using
CreateChildControls.
>
I am getting an error:
System.Web.HttpException: Control 'ctl00_cphContent_by27cf_tbName' of
type 'TextBox' must be placed inside a form tag with runat=server.
>
I have no idea why do I get this error.
The form with Runat="server" is in the master page.
>
Anyway, can someone tell me how can I figure what is going on?
>
Thanks,
Miguel


This is a bug of Visual Studio. A problem is that in content page you don't
have form

"shapper" <mdmoura@.gmail.comwrote in message
news:1163016480.466822.19390@.m7g2000cwm.googlegrou ps.com...

Quote:

Originally Posted by

Hello,
>
I have a master page where my form has the Runat="server".
The master page as a control place holder, named "cphContent".
Then in a page which inherits from the master page I added a custom
control which I created.
This custom control is added to "cphContent" at runtime.
The custom control creates and adds 2 textboxes using
CreateChildControls.
>
I am getting an error:
System.Web.HttpException: Control 'ctl00_cphContent_by27cf_tbName' of
type 'TextBox' must be placed inside a form tag with runat=server.
>
I have no idea why do I get this error.
The form with Runat="server" is in the master page.
>
Anyway, can someone tell me how can I figure what is going on?
>
Thanks,
Miguel
>
>

Text Box Problem

Hello,

I have a master page where my form has the Runat="server".
The master page as a control place holder, named "cphContent".
Then in a page which inherits from the master page I added a custom control which I created.
This custom control is added to "cphContent" at runtime.
The custom control creates and adds 2 textboxes using CreateChildControls.

I am getting an error:
System.Web.HttpException: Control 'ctl00_cphContent_by27cf_tbName' of type 'TextBox' must be placed inside a form tag with runat=server.

I have no idea why do I get this error.
The form with Runat="server" is in the master page.

Anyway, can someone tell me how can I figure what is going on?

Thanks,
MiguelYou may need to show some code of how do you set up the Content page.

Here is my code.

I am adding and creating all controls at runtime in my pages.

----------------------

Base.master:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="fBase" runat="server"></form>
</body>
</html
Base.master.vb:

Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init

Dim pContainer As New Panel
AddHandler pContainer.Init, AddressOf pContainer_Init
Me.Controls.Add(pContainer)

Dim cphContent As New ContentPlaceHolder
AddHandler cphContent.Init, AddressOf cphContent_Init
pContent.Controls.Add(cphContent)

End Sub

MyPage.aspx:

<%@. Page Language="VB" CodeFile="Default.aspx.vb" Inherits="_Default" %>

MyPage.aspx.vb:

Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init

Dim by27fContactForm As New By27.Web.UI.Form.Contact
AddHandler by27fContactForm.Init, AddressOf by27fContactForm_Init
pContacts.Controls.Add(by27fContactForm)

End Sub

Class Contact in Namespace By27.Web.UI:

Namespace Web.UI.Form

' Content
<DefaultProperty("Text"), ToolboxData("<{0}:ContactForm runat=server></{0}:ContactForm>")> _
Public Class Contact
Inherits WebControl

Dim by27cf_tbName As New WebControls.TextBox
Private Sub by27cf_tbName_Init(ByVal sender As Object, ByVal e As System.EventArgs)
With by27cf_tbName
.CssClass = Me.TextBoxCssClass
.ID = "by27cf_tbName"
.Width = Unit.Percentage(100)
End With
End Sub

<Bindable(True), Category("Appearance"), DefaultValue(""), Localizable(True)> Property TextBoxCssClass() As String
Get
If CStr(ViewState("TextBoxCssClass")) Is Nothing Then
Return String.Empty
Else
Return CStr(ViewState("TextBoxCssClass"))
End If
End Get
Set(ByVal Value As String)
ViewState("TextBoxCssClass") = Value
End Set
End Property

Protected Overrides Sub CreateChildControls()
AddHandler by27cf_tbName.Init, AddressOf by27cf_tbName_Init
MyBase.Controls.Add(by27cf_tbName)
' Create child controls
MyBase.CreateChildControls()
Me.ChildControlsCreated = True
End Sub

End Class

----------------------

Thanks,

Miguel


The controls are not being added between the form tags. Add a placeholder to the masterpage and add your controls (panel/contentholder) to that, instead of the page.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="fBase" runat="server">
<asp:PlaceHolder id="ControlHolder" runat="server"/>
</form>
</body>
</html>


In this line

pContent.Controls.Add(cphContent)

What and/or where is "pContent"? It isn't entirely clear where you're getting it from.


It should be pContainer and not pContent.

It was a mistake when coping it to here. I just copied parts of the code to give an idea


shapper:

Base.master:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="fBase" runat="server"></form>
</body>
</html
Base.master.vb:

Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init

Dim pContainer As New Panel
AddHandler pContainer.Init, AddressOf pContainer_Init
Me.Controls.Add(pContainer)

Dim cphContent As New ContentPlaceHolder
AddHandler cphContent.Init, AddressOf cphContent_Init
pContent.Controls.Add(cphContent)

End Sub

In the Page_Init above, you are adding a Panel to thepage, but not to theform. Therefore, cphContent is not in the form. And when you add the custom control to cphContent, then obviously that's not in the form either.

Monday, March 26, 2012

Text file question

For example i have a file named textfile.txt Now what i want to do isthat i open that file and read first line(somestring =readline("textfile.txt"); or smth)then second and then third line. Howcan i do it.
VB.NET
Dim strReader as New FileStreamReader("textfile.txt")
While (you still want to read)
somestring = strReader.ReadLine()
DoSomethingWith_somestring
End While
strReader.Close()
- Dan
You could try this code:
int counter = 0;
string line;
// Read the file and display it line by line.
System.IO.StreamReader file =
new System.IO.StreamReader("c:\\test.txt");
while((line = file.ReadLine()) != null)
{
Console.WriteLine (line);
counter++;
}
file.Close();
// Suspend the screen.
Console.ReadLine();
This code is for a winform, but you can simply use it as you want.
regards

You will want to use the StreamReader class. Below is the link to MSDN.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemiostreamreaderclasstopic.asp
Got it all solved... Thank you for your help!

Tuesday, March 13, 2012

TextBox and Property

Hello,

I am working on Library which will include various controls.

One of these controls has a TextBox.

I am using a property named Value to define the TextBox text:

' Value
Private _Value As String
Public Property Value() As String
Get
Return _Value
End Get
Set(ByVal value As String)
_Value = value
End Set
End Property ' Value

And the TextBox Init event is the following:

Private Sub tbText_Init(ByVal sender As Object, ByVal e As
EventArgs) Handles tbText.Init
tbText.ID = "tbText"
tbText.Text = _Value
End Sub

I added this control to a page and defined its value to "Hello".

I also added a button to the page that when clicked basically does
Response.Write(MyControl.Value)

What happens is that when I change the TextBox text to "Goodbye" and
click the button the text that Response.Write outputs is "Hello".

Why isn't the property returning the new value?

What is the right way to do this?

Thanks,

MiguelHowdy,

This is because you change the text of the text box in the Init event.
Button raises its Click event between Load and PreRender page's events
meaning after you take the value of the Value property. In addition, if the
value is set through the code or data binding its value will be lost because
it's not stored in the viewstate. You need to simplify your code to:

Public Property Value() As String
Get
Return tbText.Text
End Get
Set(ByVal value As String)
tbText.Text = value
End Set
End Property ' Value

Done

hope it helps
--
Milosz

"shapper" wrote:

Quote:

Originally Posted by

Hello,
>
I am working on Library which will include various controls.
>
One of these controls has a TextBox.
>
I am using a property named Value to define the TextBox text:
>
' Value
Private _Value As String
Public Property Value() As String
Get
Return _Value
End Get
Set(ByVal value As String)
_Value = value
End Set
End Property ' Value
>
And the TextBox Init event is the following:
>
Private Sub tbText_Init(ByVal sender As Object, ByVal e As
EventArgs) Handles tbText.Init
tbText.ID = "tbText"
tbText.Text = _Value
End Sub
>
I added this control to a page and defined its value to "Hello".
>
I also added a button to the page that when clicked basically does
Response.Write(MyControl.Value)
>
What happens is that when I change the TextBox text to "Goodbye" and
click the button the text that Response.Write outputs is "Hello".
>
Why isn't the property returning the new value?
>
What is the right way to do this?
>
Thanks,
>
Miguel
>

TextBox and Property

Hello,
I am working on Library which will include various controls.
One of these controls has a TextBox.
I am using a property named Value to define the TextBox text:
' Value
Private _Value As String
Public Property Value() As String
Get
Return _Value
End Get
Set(ByVal value As String)
_Value = value
End Set
End Property ' Value
And the TextBox Init event is the following:
Private Sub tbText_Init(ByVal sender As Object, ByVal e As
EventArgs) Handles tbText.Init
tbText.ID = "tbText"
tbText.Text = _Value
End Sub
I added this control to a page and defined its value to "Hello".
I also added a button to the page that when clicked basically does
Response.Write(MyControl.Value)
What happens is that when I change the TextBox text to "Goodbye" and
click the button the text that Response.Write outputs is "Hello".
Why isn't the property returning the new value?
What is the right way to do this?
Thanks,
MiguelHowdy,
This is because you change the text of the text box in the Init event.
Button raises its Click event between Load and PreRender page's events
meaning after you take the value of the Value property. In addition, if the
value is set through the code or data binding its value will be lost because
it's not stored in the viewstate. You need to simplify your code to:
Public Property Value() As String
Get
Return tbText.Text
End Get
Set(ByVal value As String)
tbText.Text = value
End Set
End Property ' Value
Done
hope it helps
--
Milosz
"shapper" wrote:

> Hello,
> I am working on Library which will include various controls.
> One of these controls has a TextBox.
> I am using a property named Value to define the TextBox text:
> ' Value
> Private _Value As String
> Public Property Value() As String
> Get
> Return _Value
> End Get
> Set(ByVal value As String)
> _Value = value
> End Set
> End Property ' Value
> And the TextBox Init event is the following:
> Private Sub tbText_Init(ByVal sender As Object, ByVal e As
> EventArgs) Handles tbText.Init
> tbText.ID = "tbText"
> tbText.Text = _Value
> End Sub
> I added this control to a page and defined its value to "Hello".
> I also added a button to the page that when clicked basically does
> Response.Write(MyControl.Value)
> What happens is that when I change the TextBox text to "Goodbye" and
> click the button the text that Response.Write outputs is "Hello".
> Why isn't the property returning the new value?
> What is the right way to do this?
> Thanks,
> Miguel
>

TextBox and Property

Hello,

I am working on Library which will include various controls.

One of these controls has a TextBox.

I am using a property named Value to define the TextBox text:

' ValuePrivate _ValueAs String Public Property Value()As String Get Return _ValueEnd Get Set(ByVal valueAs String) _Value = valueEnd Set End Property' Value

And the TextBox Init event is the following:

Private Sub tbText_Init(ByVal senderAs Object,ByVal eAs EventArgs)Handles tbText.Init tbText.ID ="tbText" tbText.Text = _ValueEnd Sub

I added this control to a page and defined its value to "Hello".

I also added a button to the page that when clicked basically does Response.Write(MyControl.Value)

What happens is that when I change the TextBox text to "Goodbye" and click the button the text that Response.Write outputs is "Hello".

Why isn't the property returning the new value?

What is the right way to do this?

Thanks,

Miguel

You have to use ViewState. means you have to save your property value in the ViewState, so that when page postbacks all the values are retrived from viewstate.

Public Property Value()As String
Get
Return ViewState["Value"]
End Get
Set(ByVal valueAs String)
ViewState["Value"] = value
End Set
End Property' Value

I hope this will resolve your problem.
Let me know in case of any further queries


So I will not need the variable _Value in this case?

And is this the only way to do this?

Thanks,

Miguel


Hello,

I also tried the following:

Private _ValueAs String Public Property Value()As String Get Return tbText.TextEnd Get Set(ByVal valueAs String) _Value = valueEnd Set End Property' Value

This works. How?

Should I use this way or the ViewState?

Thanks,

Miguel


No, you can directly save your values in ViewState.
I don't think there may be any other way, because whenever your form is posted on server all the values are stored in ViewState.

I hope it would help!


shapper:

Hello,

I also tried the following:

Private _ValueAs String Public Property Value()As String Get Return tbText.TextEnd Get Set(ByVal valueAs String) _Value = valueEnd Set End Property' Value

This works. How?

Should I use this way or the ViewState?

Thanks,

Miguel

Yes because Textbox.Text is stored in ViewState


Just one more question. When using the code you suggested:

Private _ValueAs String Public Property Value()As String Get Return ViewState("Value")End Get Set(ByVal valueAs String) ViewState("Value") = valueEnd Set End Property' Value

How should I know define the initial value of my textbox?

In this moment I have:

MyTextBox.Text = _Value

Of course I could use

MyTextBox.Text =Me.Value
However, sometime ago, also in Microsoft forum there was a thread that I started weather in the code should I use ... = Me.PropertyName or ... = _PropertyName
It seems most people use the variable _PropertyName.
How should I do it?
I suppose using  
MyTextBox.Text = ViewState("Value")
isn't a very nice way to do it.
Anyway, just looking for a standard way (or the closest to it) to do this.
Thanks,
Miguel 


Hi ,

THis problem is due to IsPostBack property. when you intially assigned the value Hello value check the property

If Not Page.IsPostBack Then

assign your assign intial value to control...

TextBox1.Text = "sample value"

End If


Public Property Value() As String
Get
Dim viewState As Object = ViewState("Value")

If viewState Is Nothing Then
' Return your default value here instead of an empty string
Return String.Empty
Else
Return CStr(viewState)
End If
End Get
Set(ByVal value As String)
ViewState["Value"] = value
End Set
End Property

NC...