Marina said:
I mistyped if I said HtmlInput, I meant HtmlInputText - that is what the
type of the object is.
ToString method in this case returns the type of object. It is not coded to
return the text in the input control. Not sure why you thought it would? I
think it should have a Value property, try that.
Again, I urge you to step back and read up on these objects and how they
work. Had you looked up the HtmlInputText class in the documentation, you
would have see all the properties/methods it has, and probably not assumed
ToString would give you the text in the control.
I think what is happening now, you are sort of guessing as to how things
should work, and so you will have constant issues and problems. You are
coding hoping that something will work - instead of knowing it will. I think
it's important to be familiar with the objects you are using, their
propeties and methods, and the appropriate ways to alter their behavior.
Let me explain what I know and understand so maybe we can close this issue.
On the aspx page I've got:
<input id="edit_name" type="text"
value='<%#DataBinder.Eval(Container.DataItem, "name")%>' runat="server" />
I understand that input is an html control, which has an ID of edit_name.
On the codebehind page, i've got:
Dim test3 As TextBox
test3.Text = CType(e.Item.FindControl("edit_name"), TextBox)
I understand here that it is (should) find the control with the ID of
edit_name, and take that object and try to cast it to an object of type
TextBox (from HTMLInputText, which is an <input>, right?). However,
VisualStudio 2005 puts a big blue underline under the
CType(e.Item.FindControl("edit_name"), TextBox) saying "Value of type
'System.Web.Ui.WebControls.TextBox' cannot be converted to 'String'". If
I add .text at the end of that ctype function, like so:
test3.Text = CType(e.Item.FindControl("edit_name"), TextBox).Text
The underline goes away and all seems well. However, in my SQL string, I
put:
SQL = "UPDATE phonetest SET name = '" & test3 & "' WHERE ID = " & TKEY
and, of course, the "SQL = "UPDATE phonetest SET name = '" & test3"
portion gets underlined saying, "Operator '&' is not defined for
'String' and 'System.Web.UI.WebControls.TextBox'". What?? Why can't I
put a string there? So, if I just add .Text to the end of test3, like so:
SQL = "UPDATE phonetest SET name = '" & test3.Text & "' ....
The Underline goes away. Then, I get a green underline under the test3
portion of the line:
test3.Text = CType(e.Item.FindControl("edit_name"), TextBox).Text
Saying, "Variable 'test3' is used before it has been assigned a value. A
null reference exception could occur at runtime".
Now, correct me if I'm wrong, but I thought I was assigning a value at
that line, well, a value for the text property of the test3 object. In
any case, when I run update routine, I get the ol' error:
Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlInputText'
to type 'System.Web.UI.WebControls.TextBox'.
So, I understand that the 2.0 framework is telling me that it cannot
cast an htmlinputtext object (the <input> html from the aspx page) to a
textbox object. I don't understand why that is though.
The HtmlInputText object does not have a text property like the textbox
object does. I did try this:
Dim test3 As HtmlInputText
test3.Value = CType(e.Item.FindControl("edit_name"), HtmlInputText).Value
SQL = "UPDATE phonetest SET name = '" & test3.Value & "' WHERE ID = " & TKEY
BUt, I get the "Object reference not set to an instance of an object"
error. I had to put the .Value at the end of the cast function because
it gave me an error otherwise saying that "Value of type
'System.Web.UI.HtmlControls.HtmlInputText' cannot be converted to 'String'.
In the error: "Object reference not set to an instance of an object", is
it saying that "edit_name" isn't an object instance?
All I want to do is get the value of that control (the text) assigned
into a variable so I can use the contentes of that variable in my SQL
statement. So, yea, I'm shooting in the dark trying to make it work.