Reputation: 671
Problem: There are 4 text boxes. If the user does not wish to fill all of them out, then it should display "0" and get saved.
For example: I only fill out two textboxes out of the four. I hit Save. It should automatically save the default values ("0") for those two empty textboxes.
Here is the code (javascript):
This is the Textbox:
<td width="20%">
asp:TextBox ID="left" runat="server"></asp:TextBox>
px
</td>
This is the save button:
<asp:Button ID="save" Text="Save"
OnClick="btn_save_click" OnClientClick="return Validate();" runat="server"/>
This is the Validate() function:
function Validate() {
var value5 = document.getElementById('<%=left.ClientID%>').value;
if (value5 == '') {
alert("Please enter the missing fields");
return false;
}
}
Upvotes: 0
Views: 535
Reputation: 1364
Perform a back end check of the text boxes.
string valueToBeSaved="0";
if(!String.IsNullOrEmpty(txtBox.Text))
{
valueToBeSaved = txtBox.Text;
}
You could even put the logic in a function like so.
public string ChecTextBoxValue(TextBox textBox)
{
string valueToBeSaved="0";
if(!String.IsNullOrEmpty(textBox.Text))
{
valueToBeSaved = textBox.Text;
}
return valueToBeSaved;
}
This way all you have to do is:
sqlCommand.Parameters.Add("@yourParam", ChecTextBoxValue(yourTextBox));
Please keep in mind this is a small example. If you post your code I may possibly have a better suggestion.
Also keep in mind that you will have to perform validation on the backend as well. JavaScript may be turned off on the user's browser and they will end up passing an unexpected value.
Upvotes: 1