Hi People,
was hoping someone could shed some light on this issue for me. First off let me just say that im new to ajax so go easy lol.
Right got a webpage that inhertits its basic style from master.site. This master.site page has the ajax script manager in it, thus all pages inhertit this. Now this all works fine.
So i now ive added the following to the page a
- a dropdownlist which is not in the updatepanel.
- a updatepanel with a few editboxes and a button.
Right what ive done is added a trigger on the selected index change of the dropdownlist to reload the edit boxes in the update panel with selected data from the sql. This is done in the page_load event. So far everything is working ok....
Now what i want to do is on the button press is save the editbox values in the updatepanel back to sql. So using the button_Click event i save the data to sql, but the values are saved incorrectly. Now i believe this is some to do with the post back...... So i added the if Ispostback in my page_load function, i can now save the data correctly but have lost the data selection with the dropdownlist.........
How do i achieve both using ajax ? Below is the vb source for the page.
Imports System.DataImports System.Data.SqlClientImports System.ConfigurationImports System.Web.ConfigurationPartialClass ParamInherits System.Web.UI.PageProtected Sub Page_Load(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles Me.LoadDim indexAs Integer = 1'If Session("Authenticated") = 0 Then 'Response.Redirect("Login.aspx") 'End If 'If Not IsPostBack Then 'If IsInAsyncPostBack ThenDim connectionStringAs String = ConfigurationManager. _ ConnectionStrings("ConnectionString").ConnectionString Using connectionAs New SqlConnection(connectionString)Dim commandAs New SqlCommand("spGetMachineParam", connection) command.CommandType = CommandType.StoredProcedure'Check drop down index and set acordinglyIf DropDownList1.SelectedIndex <> -1Then index = DropDownList1.SelectedIndex + 1End If'Create a SqlParameters object to hold the input parameter values command.Parameters.Add("@dotnet.itags.org.index", SqlDbType.Int) command.Parameters("@dotnet.itags.org.index").Value = index connection.Open()Dim readerAs SqlDataReader = command.ExecuteReader()'Call Read before accessing data.While reader.Read() TextBox1.Text = reader.Item(7).ToString TextBox2.Text = reader.Item(2).ToString TextBox3.Text = reader.Item(8).ToString TextBox4.Text = reader.Item(3).ToString TextBox5.Text = reader.Item(9).ToString TextBox6.Text = reader.Item(4).ToString TextBox7.Text = reader.Item(10).ToString TextBox8.Text = reader.Item(5).ToString TextBox9.Text = reader.Item(11).ToString TextBox10.Text = reader.Item(6).ToStringEnd While'Call Close when done reading. command.Dispose()'Dispose of the Command. connection.Close()'Close the connection. reader.Close()'Close the readerEnd Using'End IfEnd Sub Protected Sub Button1_Click(ByVal senderAs Object,ByVal eAs System.EventArgs)Dim indexAs Integer = 1Dim resultAs Decimal Dim returnValueAs Boolean Dim connectionStringAs String = ConfigurationManager. _ ConnectionStrings("ConnectionString").ConnectionString Using connectionAs New SqlConnection(connectionString)Dim commandAs New SqlCommand("spSetMachineParam", connection) command.CommandType = CommandType.StoredProcedure'Check drop down index ans set acordinglyIf DropDownList1.SelectedIndex <> -1Then index = DropDownList1.SelectedIndex + 1End If'Create a SqlParameters object to hold the input parameter values command.Parameters.Add("@dotnet.itags.org.index", SqlDbType.Int) command.Parameters("@dotnet.itags.org.index").Value = index command.Parameters.Add("@dotnet.itags.org.value1", SqlDbType.Money) returnValue =Decimal.TryParse(TextBox2.Text, result) command.Parameters("@dotnet.itags.org.value1").Value = result command.Parameters.Add("@dotnet.itags.org.value2", SqlDbType.Money) returnValue =Decimal.TryParse(TextBox4.Text, result) command.Parameters("@dotnet.itags.org.value2").Value = result command.Parameters.Add("@dotnet.itags.org.value3", SqlDbType.Money) returnValue =Decimal.TryParse(TextBox6.Text, result) command.Parameters("@dotnet.itags.org.value3").Value = result command.Parameters.Add("@dotnet.itags.org.value4", SqlDbType.Money) returnValue =Decimal.TryParse(TextBox8.Text, result) command.Parameters("@dotnet.itags.org.value4").Value = result command.Parameters.Add("@dotnet.itags.org.value5", SqlDbType.Money) returnValue =Decimal.TryParse(TextBox10.Text, result) command.Parameters("@dotnet.itags.org.value5").Value = result connection.Open()Dim readerAs SqlDataReader = command.ExecuteReader()'Call Close when done reading. command.Dispose()'Dispose of the Command. connection.Close()'Close the connection. reader.Close()'Close the readerEnd UsingEnd SubEnd Class
Kind Regards
Lee
The way the code is writen to onload will overright the values posted back.
If the put a Not IsPost back around the code in the on_load and make sure the Button1_Click is inside the update panel and is set to cause partial postback. It should work.
Richard
Hi richard, the documentation forDecimal.TryParse says that the return value is if the convertion pass/failed.
Plus this section of the code works if i add IsPostBack to my page_load.
Sorry, miss read the code and was in the middle of editting when you replied
You didn't say exactly what was going wrong, but if what you're seeing is that the default values (instead of what was entered by the user) are saved, then I think the problem is the ordering.
Page load happens before events are processed, so you're setting all the textbox values and then processing the button click event, at which point you use the values in the textboxes (which just got reset).
Try moving that code out of page load and into prerender instead, which happens towards the end of the page lifecycle.
Hi Steve thanks for the reply,
yeah what is happening is if i change a value in one of the editboxes, and press the button, the value what is stored in sql is the old value....Thus when page_load fires and updates the editboxes the old vaule comes back.....
Yup, that's what I guessed. So did you try my fix?
No it still does the same. If i debug it and break here :-
-- >>> returnValue =Decimal.TryParse(TextBox2.Text, result)The TextBox2 Never passes the new value to result Varible, its still the old value even through it as been edited....
its like in VC++ where you have to save and validate using UpdataDate(true), to force the data exchange.
You have any more suggestions ?
Thanks Lee
Could you paste your latest code so I can take a look?
Yeah no problem,
Imports System.DataImports System.Data.SqlClientImports System.ConfigurationImports System.Web.ConfigurationPartialClass ParamInherits System.Web.UI.PageProtected Sub Page_Load(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles Me.LoadDim indexAs Integer = 1'If Session("Authenticated") = 0 Then 'Response.Redirect("Login.aspx") 'End If 'If Not IsPostBack Then 'If IsInAsyncPostBack ThenDim connectionStringAs String = ConfigurationManager. _ ConnectionStrings("ConnectionString").ConnectionString Using connectionAs New SqlConnection(connectionString)Dim commandAs New SqlCommand("spGetMachineParam", connection) command.CommandType = CommandType.StoredProcedure'Check drop down index ans set acordinglyIf DropDownList1.SelectedIndex <> -1Then index = DropDownList1.SelectedIndex + 1End If'Create a SqlParameters object to hold the input parameter values command.Parameters.Add("@.index", SqlDbType.Int) command.Parameters("@.index").Value = index connection.Open()Dim readerAs SqlDataReader = command.ExecuteReader()'Call Read before accessing data.While reader.Read()'TextBox1.Text = reader.Item(7).ToString TextBox2.Text = reader.Item(7).ToString'TextBox3.Text = reader.Item(8).ToString 'TextBox4.Text = reader.Item(8).ToString 'TextBox5.Text = reader.Item(9).ToString 'TextBox6.Text = reader.Item(9).ToString 'TextBox7.Text = reader.Item(10).ToString 'TextBox8.Text = reader.Item(10).ToString 'TextBox9.Text = reader.Item(11).ToString 'TextBox10.Text = reader.Item(11).ToStringEnd While'Call Close when done reading. command.Dispose()'Dispose of the Command. connection.Close()'Close the connection. reader.Close()'Close the readerEnd Using'End IfEnd Sub Protected Sub Button1_Click(ByVal senderAs Object,ByVal eAs System.EventArgs)Dim indexAs Integer = 1Dim resultAs Decimal Dim returnValueAs Boolean Dim connectionStringAs String = ConfigurationManager. _ ConnectionStrings("ConnectionString").ConnectionString Using connectionAs New SqlConnection(connectionString)Dim commandAs New SqlCommand("spSetMachineParam", connection) command.CommandType = CommandType.StoredProcedure'Check drop down index ans set acordinglyIf DropDownList1.SelectedIndex <> -1Then index = DropDownList1.SelectedIndex + 1End If'Create a SqlParameters object to hold the input parameter values command.Parameters.Add("@.index", SqlDbType.Int) command.Parameters("@.index").Value = index command.Parameters.Add("@.value1", SqlDbType.Money) returnValue =Decimal.TryParse(TextBox2.Text, result) command.Parameters("@.value1").Value = result'command.Parameters.Add("@.value2", SqlDbType.Money) 'returnValue = Decimal.TryParse(TextBox4.Text, result) 'command.Parameters("@.value2").Value = result 'command.Parameters.Add("@.value3", SqlDbType.Money) 'returnValue = Decimal.TryParse(TextBox6.Text, result) 'command.Parameters("@.value3").Value = result 'command.Parameters.Add("@.value4", SqlDbType.Money) 'returnValue = Decimal.TryParse(TextBox8.Text, result) 'command.Parameters("@.value4").Value = result 'command.Parameters.Add("@.value5", SqlDbType.Money) 'returnValue = Decimal.TryParse(TextBox10.Text, result) 'command.Parameters("@.value5").Value = result connection.Open()Dim readerAs SqlDataReader = command.ExecuteReader()'Call Close when done reading. command.Dispose()'Dispose of the Command. connection.Close()'Close the connection. reader.Close()'Close the readerEnd UsingEnd SubEnd Class
This code still has you setting all the values in Page_Load... I thought you tried moving those to PreRender?
Hi steve, i've just tried it again and it is now workin, using the page_prerend. i'll add the rest of the code back in and try it out......
Thank for your help steve more appreciated.
Lee
No comments:
Post a Comment