Good afternoon.
I have a simple gridview with a field like this:
<asp:TemplateField HeaderText="USD Full Load Machine">
<ItemTemplate>
<asp:Label ID="lblUSDFullLoad" runat="server" Text='<%# Bind("USDFullyLoadMachine") %>' />
</ItemTemplate>
</asp:TemplateField>
The InsertCommand and Insertparameters are set up like this:
<asp:SqlDataSource (code omitted)
InsertCommand="Insert Into [xxxxTable]
([SubmissionID],USDFullyLoadMachine]) Values(@newSubID,@usdflm)">
<InsertParameters>
<asp:ControlParameter Name="newSubID" ControlID="lblNewSubmissionID" />
<asp:ControlParameter Name="usdflm" ControlID="gridConversionTotals$ctl02$lblUSDFullLoad" />
</InsertParameters>
</asp:SqlDataSource>
When I change the gridConversionTotals$ctl02$lblUSDFullLoad label’s text using client-side javascript, and I execute the insert method on the sql datasource control in code behind, the value sent to SQL is NOT the new value of the label, i.e. is not the value shown on the browser, it is the value the label had when it was last databound.
I initially thought that this was because the server, upon executing the insert command, could not possibly know what the client was showing, but then this is not true, because if I change the label to a textbox and leave all the code intact, the value sent to the database is indeed the one shown on the client.
Can someone please explain why the insert method, in this instance, treats a label’s and a textbox’s client-side content differently?
And does this mean that generally when I want to change values client-side and then send them to the database I will need to always use textboxes?
Thank you.
After some further research I found this post https://stackoverflow.com/a/10324721/1266732, which explains that the label will not post the value back to the webserver, and therefore it is recommended to use textboxes and styling them as labels for this purpose.