I'm currently working on updating a section of my webpage using AJAX instead of C#, as I don't want the page to refresh. All I need to do is execute a SELECT query to retrieve the current client from the SQL database and populate the corresponding form without refreshing the entire page. I've been able to successfully fetch the client data and update the textbox, but unfortunately, the page still refreshes afterwards and all the textboxes end up empty again!
Below is the code snippet I'm using:
<asp:Panel ID="pnlClientSummaryDetails" runat="server" Width="360px" Visible="True">
<asp:TextBox runat="server" ID="txtDateOpened"></asp:TextBox>
<!--other text boxes -->
</asp:Panel>
<asp:Button ID="bnSelectClient" runat="server" CssClass="auto-style7" Text="Select Client" ToolTip="Click the view client button to view selected clients information" Width="282px" OnClientClick="BindClientSummaryForm()" />
function BindClientSummaryForm() {
$.ajax({
type: "POST",
url: "Clients.aspx/GetClientSummaryData",
contentType: "application/json;charset=utf-8",
data: {},
dataType: "json",
success: function(data) {
document.getElementById('<%= txtDateOpened.ClientID %>').value = data.d.DateFileOpened;
// other textboxes will be filled like that.
},
error: function (result) {
alert("Error occurred while filling ClientSummary part.");
}
});
}
[WebMethod]
public static MyClient GetClientSummaryData() //GetData function
{
//FillClientSummaryGridView();
int intClientID = 0;
Int32.TryParse(clientID, out intClientID);
MyClient client = new MyClient();
string sQuery = "SELECT ClientID,FirstName,DateFileOpened FROM dbo.Client where ClientID = " + intClientID;
String sConnectionString = ConfigurationManager.ConnectionStrings["myDatabase"].ConnectionString;
SqlConnection con = new SqlConnection(sConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand(sQuery, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dtGetData = new DataTable();
da.Fill(dtGetData);
foreach(DataRow dtRow in dtGetData.Rows)
{
client.Firstname = dtRow.ItemArray[1].ToString();
client.DateFileOpened = dtRow.ItemArray[2].ToString();
}
return client;
}
I'm seeking advice on how to prevent the page from refreshing and retain the values in the textboxes assigned by the AJAX success function. Any help would be greatly appreciated. Thank you!