In my code behind I fill the text box with content from a table On Page Load.
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim connectionString As String = DirectCast(ConfigurationManager.ConnectionStrings("IT_CentralConnectionString").ConnectionString, String)
Dim conn As New SqlConnection(connectionString)
Dim comm As New SqlCommand("SELECT * FROM [ITC_Standards] WHERE NewsID = 2 ", conn)
comm.Connection.Open()
Dim myDataAdapter As New SqlDataAdapter(comm)
Dim myDataSet As New DataSet
Dim dtData As New DataTable
Dim dtRow As DataRow
myDataAdapter.Fill(myDataSet)
conn.Close()
For Each dtRow In myDataSet.Tables(0).Rows
Dim NewsID = dtRow.Item("NewsID")
Dim NewsText = dtRow.Item("NewsText")
EditedNews.Value = NewsID
TextBox1.Text = NewsText
myDataSet.Tables.Clear()
Next
End Sub
The above works correctly.
I then edit the text and submit the changes.
Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Dim NewsID As Integer = EditedNews.Value
Dim NewsText = TextBox1.Text
Dim sql As String
Dim strConnString As [String] = System.Configuration.ConfigurationManager.ConnectionStrings("IT_CentralConnectionString").ConnectionString()
sql = "UPDATE ITC_Standards SET NewsText = @NewsText WHERE NewsID = @NewsID"
Dim cn As New SqlConnection(strConnString)
Dim cmd As New SqlCommand(sql, cn)
cmd.Parameters.Add("@NewsText", SqlDbType.NVarChar, 8000).Value = NewsText
cmd.Parameters.Add("@NewsID", SqlDbType.Int).Value = NewsID
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
End Sub
The issue is that no matter what I type in the textbox, the text is not updated.
After testing this by hard coding the values, it appears that what is happening is the the TestBox1.Text value is not actually being aquired from the ASPX page but is actually the value that is in the Page Load Textbox1.Text.
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim connectionString As String = DirectCast(ConfigurationManager.ConnectionStrings("IT_CentralConnectionString").ConnectionString, String)
Dim conn As New SqlConnection(connectionString)
Dim comm As New SqlCommand("SELECT * FROM [ITC_Standards] WHERE NewsID = 2 ", conn)
comm.Connection.Open()
Dim myDataAdapter As New SqlDataAdapter(comm)
Dim myDataSet As New DataSet
Dim dtData As New DataTable
Dim dtRow As DataRow
myDataAdapter.Fill(myDataSet)
conn.Close()
For Each dtRow In myDataSet.Tables(0).Rows
Dim NewsID = dtRow.Item("NewsID")
Dim NewsText = dtRow.Item("NewsText")
EditedNews.Value = NewsID
TextBox1.Text = NewsText
myDataSet.Tables.Clear()
Next
End Sub
Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Dim NewsID As Integer = EditedNews.Value
Dim NewsText = TextBox1.Text
Dim sql As String
Dim strConnString As [String] = System.Configuration.ConfigurationManager.ConnectionStrings("IT_CentralConnectionString").ConnectionString()
sql = "UPDATE ITC_Standards SET NewsText = @NewsText WHERE NewsID = @NewsID"
Dim cn As New SqlConnection(strConnString)
Dim cmd As New SqlCommand(sql, cn)
cmd.Parameters.Add("@NewsText", SqlDbType.NVarChar, 8000).Value = NewsText
cmd.Parameters.Add("@NewsID", SqlDbType.Int).Value = NewsID
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
End Sub
How can I ensure that the value is coming from the updated text box and not the previous declaration's value?
Thank you.
Allan Browning
Vision Quest Integrated Technologies, Inc.
www.VisionQuestIT.com
If you solve your issue, please post the solution.
You need to put the code you have in page_load, the code that initialized the textbox, inside of an If (Not IsPostBack) block; otherwise you'd be resertting the value on every postback.
Yes, Metal is correct. Page_Load runs on every postback, even those generated by a button click, so it's going to re-run all your code again. You want this:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Not Page.IsPostback Then
' your code goes here
End If
End Sub
IsPostback will be False on the first page load, and True for every postback.
abrowning
Member
39 Points
68 Posts
Declaration Seepage
May 18, 2011 11:06 PM|LINK
Odd subject line but I was not sure what else to call it. :)
I have a basic ASPX page with a textbox and a button:
<form id="form1" runat="server"> <input type="hidden" name="EditedNews" runat="server" id="EditedNews" /> <telerik:RadScriptManager ID="ScriptManager1" runat="server" /> <p> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </p> <p> <asp:Button ID="Button1" runat="server" Text="Button" /> </p> </form>In my code behind I fill the text box with content from a table On Page Load.
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load Dim connectionString As String = DirectCast(ConfigurationManager.ConnectionStrings("IT_CentralConnectionString").ConnectionString, String) Dim conn As New SqlConnection(connectionString) Dim comm As New SqlCommand("SELECT * FROM [ITC_Standards] WHERE NewsID = 2 ", conn) comm.Connection.Open() Dim myDataAdapter As New SqlDataAdapter(comm) Dim myDataSet As New DataSet Dim dtData As New DataTable Dim dtRow As DataRow myDataAdapter.Fill(myDataSet) conn.Close() For Each dtRow In myDataSet.Tables(0).Rows Dim NewsID = dtRow.Item("NewsID") Dim NewsText = dtRow.Item("NewsText") EditedNews.Value = NewsID TextBox1.Text = NewsText myDataSet.Tables.Clear() Next End SubThe above works correctly.
I then edit the text and submit the changes.
Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click Dim NewsID As Integer = EditedNews.Value Dim NewsText = TextBox1.Text Dim sql As String Dim strConnString As [String] = System.Configuration.ConfigurationManager.ConnectionStrings("IT_CentralConnectionString").ConnectionString() sql = "UPDATE ITC_Standards SET NewsText = @NewsText WHERE NewsID = @NewsID" Dim cn As New SqlConnection(strConnString) Dim cmd As New SqlCommand(sql, cn) cmd.Parameters.Add("@NewsText", SqlDbType.NVarChar, 8000).Value = NewsText cmd.Parameters.Add("@NewsID", SqlDbType.Int).Value = NewsID cmd.Connection.Open() cmd.ExecuteNonQuery() cmd.Connection.Close() End SubThe issue is that no matter what I type in the textbox, the text is not updated.
After testing this by hard coding the values, it appears that what is happening is the the TestBox1.Text value is not actually being aquired from the ASPX page but is actually the value that is in the Page Load Textbox1.Text.
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load Dim connectionString As String = DirectCast(ConfigurationManager.ConnectionStrings("IT_CentralConnectionString").ConnectionString, String) Dim conn As New SqlConnection(connectionString) Dim comm As New SqlCommand("SELECT * FROM [ITC_Standards] WHERE NewsID = 2 ", conn) comm.Connection.Open() Dim myDataAdapter As New SqlDataAdapter(comm) Dim myDataSet As New DataSet Dim dtData As New DataTable Dim dtRow As DataRow myDataAdapter.Fill(myDataSet) conn.Close() For Each dtRow In myDataSet.Tables(0).Rows Dim NewsID = dtRow.Item("NewsID") Dim NewsText = dtRow.Item("NewsText") EditedNews.Value = NewsID TextBox1.Text = NewsText myDataSet.Tables.Clear() Next End Sub Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click Dim NewsID As Integer = EditedNews.Value Dim NewsText = TextBox1.Text Dim sql As String Dim strConnString As [String] = System.Configuration.ConfigurationManager.ConnectionStrings("IT_CentralConnectionString").ConnectionString() sql = "UPDATE ITC_Standards SET NewsText = @NewsText WHERE NewsID = @NewsID" Dim cn As New SqlConnection(strConnString) Dim cmd As New SqlCommand(sql, cn) cmd.Parameters.Add("@NewsText", SqlDbType.NVarChar, 8000).Value = NewsText cmd.Parameters.Add("@NewsID", SqlDbType.Int).Value = NewsID cmd.Connection.Open() cmd.ExecuteNonQuery() cmd.Connection.Close() End SubHow can I ensure that the value is coming from the updated text box and not the previous declaration's value?
Thank you.
Vision Quest Integrated Technologies, Inc.
www.VisionQuestIT.com
If you solve your issue, please post the solution.
MetalAsp.Net
All-Star
112168 Points
18255 Posts
Moderator
Re: Declaration Seepage
May 18, 2011 11:10 PM|LINK
You need to put the code you have in page_load, the code that initialized the textbox, inside of an If (Not IsPostBack) block; otherwise you'd be resertting the value on every postback.
Hua-Jun Li -...
All-Star
75950 Points
5608 Posts
Re: Declaration Seepage
May 24, 2011 07:45 AM|LINK
Hi abrowning,
You can try to use the following code to get the new value of textbox.
string text=Request.Form["formid"].ToString();
You can check whether you forbid the viewstate it cause the textbox value do't update.
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework
Dave Sussman
All-Star
37716 Points
5005 Posts
ASPInsiders
MVP
Re: Declaration Seepage
May 24, 2011 07:48 AM|LINK
Yes, Metal is correct. Page_Load runs on every postback, even those generated by a button click, so it's going to re-run all your code again. You want this:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load If Not Page.IsPostback Then ' your code goes here End If End SubIsPostback will be False on the first page load, and True for every postback.