使用ajax分配文本框值 - 页面仍然刷新,分配的值已消失

时间:2022-06-24 21:14:44

I am trying to fill a part of my web page by using AJAX instead of C# since the webpage should not be refreshed for this action. All I need to do is to run a SELECT query and get the current client from SQL DB and fill the related form without refreshing the page. I managed to get the related client and to change the related textbox, however, the page is still refreshing and all the textboxes becomes null again!

我试图通过使用AJAX而不是C#来填充我的网页的一部分,因为不应该为此操作刷新网页。我需要做的就是运行SELECT查询并从SQL DB获取当前客户端并填写相关表单而不刷新页面。我设法获得相关的客户端并更改相关的文本框,但是,页面仍然刷新,所有文本框再次变为空!

My code is on the below:

我的代码如下:

    <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 occured 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;
        }

What should I do friends? I have to prevent page refreshing and keep the textbox values that assigned by success part of the ajax. Thank you!

我该怎么做朋友?我必须阻止页面刷新并保留由ajax的成功部分分配的文本框值。谢谢!

1 个解决方案

#1


1  

Please try to add return false to your BindClientSummaryForm function after the ajax call, also modify the way you are calling the function to this: OnClientClick="return BindClientSummaryForm();"

请尝试在ajax调用后向您的BindClientSummaryForm函数添加return false,同时修改调用函数的方式:OnClientClick =“return BindClientSummaryForm();”

If that does not work, try removing return false from the function and using this instead: OnClientClick="BindClientSummaryForm(); return false;"

如果这不起作用,请尝试从函数中删除return false并改为使用它:OnClientClick =“BindClientSummaryForm(); return false;”

#1


1  

Please try to add return false to your BindClientSummaryForm function after the ajax call, also modify the way you are calling the function to this: OnClientClick="return BindClientSummaryForm();"

请尝试在ajax调用后向您的BindClientSummaryForm函数添加return false,同时修改调用函数的方式:OnClientClick =“return BindClientSummaryForm();”

If that does not work, try removing return false from the function and using this instead: OnClientClick="BindClientSummaryForm(); return false;"

如果这不起作用,请尝试从函数中删除return false并改为使用它:OnClientClick =“BindClientSummaryForm(); return false;”