assign value to a text box

M

Mark Kurten

i have the code below:

for starters the value of txtempid = 1
after i go through this routine (i put a break point on the txtname.value
line), the value of txtEmpID is 2 (Just like it is suppose to be)
However, when i come into this function again, txtEmpID = 1 again
For some reason the txtEmpID.value isn't retaining its actual value of 2

any ideas?
Try

Dim row As DataRow = ds.Tables("TestTable").Select("emp_id > " &
txtEmpID.Value)(0)

With row

txtEmpID.Value = .Item("emp_id").ToString

txtFName.Value = .Item("Fname").ToString

txtLName.Value = .Item("LName").ToString

End With

Catch err As Exception

lblError.Text = "No Previous records " & err.ToString()

End Try
 
K

Kevin Spencer

You didn't post a function; you posted a block of code. It would have helped
if you DID post the function, because the problem lies in how you
initialized "txtEmpID" and I strongly suspect that you initialized it in the
function definition. This would result in its being initialized every time
the function is called.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
M

Mark Kurten

Here is the function..

Private Sub btnNext_ServerClick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnPrev.ServerClick


Try

Dim row As DataRow = ds.Tables("TestTable").Select("emp_id > " &
txtEmpID.Value)(0)

With row

txtEmpID.Value = .Item("emp_id").ToString

txtFName.Value = .Item("Fname").ToString

txtLName.Value = .Item("LName").ToString

End With

Catch err As Exception

lblError.Text = "No Nextrecords " & err.ToString()

End Try

End Sub
 
K

Kevin Spencer

Okay, it looks like when you say "when I come into this function again"
you're talking about a PostBack? If so, is your TextBox control's
EnableViewState property set to True?

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
M

Mark Kurten

I should have mentioned this earlier, I am using a HTML text box, not a web
form text box. when i use a web form text box, everything works fine, but i
want to try a HTML text box.
the html text box doesn't have the enableviewstate property.

thanks
 
M

Mark Kurten

also, to text this more simply, I just put a html text box and a html button
on a form and put the following code behind it..when i first enter a "1" in
the text box and hit the button, the value is 1. then if i put a 2 in the
box and hit the button, the value remains 1

Private Sub Button1_ServerClick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.ServerClick

Dim a As Integer = txtEmpID.Value

end Sub
 
K

Kevin Spencer

Well, there's your problem. You need a Control that Maintains State across
PostBacks.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
M

mark kurten

would this be a html control or a web control?
I'm assuming there is a control that does this....
thanks.
 
M

mark kurten

Or do I use a session state object?

thanks.


Kevin Spencer said:
Well, there's your problem. You need a Control that Maintains State across
PostBacks.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

but
 
S

Steven Cheng[MSFT]

Hi Mark,


Thanks for posting in the community!
From your description, you used some HtmlInputText controls(set ast
runat=server) on an ASP.NET web page. However, you found that when you
posted back the page, the values in the HtmlInputText Control can't remain,
will return back to the original value, yes?
If there is anything I misunderstood, please feel free to let me know.

As for this problem, I'd like to confirm some further things on it:
1.You mentioned that when you input a value "2" in the html textbox and
post back the page, after that the value returned to "1", yes? Are you sure
there is no other code in codebehind will modify the textbox's value?

2.As you mentioned, you tested use the numeric value such as "1","2". Have
you tried using string value. For example, put a string like "Hello World"
and post back to see whether this remains?

3.I still feel this problem a bit strang, would you please try creating
another new project and do the same test in it? Also, do be sure that you
have'nt disable the ViewState of the page at PageLevel(@Page directive) or
Application Level(web.config) or ..

And here is a example page I used to test, maybe you can also try testing
on this page to see whether the problem remains:

-----------------------------------aspx
pape--------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>TestValue</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="500" align="center">
<tr>
<td><INPUT id="txtHtml" type="text" name="txtHtml" runat="server"></td>
</tr>
<tr>
<td><INPUT id="btnHtml" type="button" value="Html Button"
name="btnHtml" runat="server">
<asp:Button id="Button1" runat="server" Text="ASP.NET
Button"></asp:Button></td>
</tr>
</table>
</form>
</body>
</HTML>

-----------------------------code behind page class-------------------------
Public Class TestValue
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents txtHtml As System.Web.UI.HtmlControls.HtmlInputText
Protected WithEvents btnHtml As
System.Web.UI.HtmlControls.HtmlInputButton

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub

Private Sub btnHtml_ServerClick(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnHtml.ServerClick
Response.Write("<br>txtHtml's value is " & txtHtml.Value)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Response.Write("<br>txtHtml's value is " & txtHtml.Value)
End Sub
End Class
-----------------------------------------------------------

Please try out the preceding suggestions. If you feel any thing unclear or
need any help, please feel free to post here.



Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
K

Kevin Spencer

Hi Mark,

Either HtmlControls or WebControls can maintain state.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
S

Steven Cheng[MSFT]

Hi Mark,


Have you had a chance to try out my suggestions or have you resolved the
problem in this issue? If you need any further assistance, please feel free
to let me know.



Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
R

Ravichandran J.V.

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

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top