【问题标题】:How to call C# code from javascript function如何从javascript函数调用C#代码
【发布时间】:2017-05-23 04:25:45
【问题描述】:

在我的应用程序中,我需要调用 aspx.cs 方法来从 javascript 函数调用中绑定 Gridview 我该怎么做?我搜索并找到了一些代码,但它对我不起作用

我试过代码:

客户端:

<script>
 function MyHeader() {      

           PageMethods.BindHeaderGrid(); //I tried this one also
           var x = document.getElementById('HeaderDiv');
           if (x.style.display === 'none') {
               x.style.display = 'block';
               img2.src= "minus.gif";               

           } else {
               x.style.display = 'none';
               img2.src= "plus.gif";
           }      
       }       
</script>

<img id='img2' width="9px" border="0" src="plus.gif" onclick="MyHeader()"/> Header <div id="HeaderDiv" style="display:none">         
          <asp:GridView ID="GrdHeader" runat="server" AutoGenerateColumns="false">
              <Columns>
                  <asp:BoundField  DataField="SenderID" HeaderText="SenderID" />
                  <asp:BoundField  DataField="ReceiverID" HeaderText="ReceiverID" />
                  <asp:BoundField DataField="Transactiondate" HeaderText="Transactiondate" />
                  <asp:BoundField DataField="RecordCount" HeaderText="RecordCount" />
                  <asp:BoundField DataField="DispositionFlag" HeaderText="DispositionFlag" />
              </Columns>
          </asp:GridView></div>

服务器端:aspx.cs

[WebMethod]
    public void BindHeaderGrid()
    {        
        GrdHeader.DataSource = ds.Tables[0];
        GrdHeader.DataBind();

    }

谢谢

【问题讨论】:

  • 这是一种错误的做法。您不能从 Javascript 强制 ASPX Datagrid 到 Bind()。这需要回发。我建议你看看 scriptmanager 和 updatepanel。
  • @uɐpuɐɥƆ,我不想从代码隐藏中调用 javascript 函数。我需要 .cs 方法从 javascript 函数中调用。
  • @uɐpuɐɥƆ,我也试过了,但我的函数没有调用
  • 在 .cs 和 javascript 中进行调试并提及您遇到的错误

标签: javascript c# asp.net


【解决方案1】:

您不能这样做 - 从客户端调用服务器端的函数。

我看到了你的代码,我有两个选择:

  1. 如果您只想显示或隐藏 Gridview #GrdHeader,那么您始终可以在页面加载(或相同)时调用“BindHeaderGrid”

  2. 1234563标签

【讨论】:

  • 你选择哪个?
  • 当我点击图片(+)时我需要加载并显示gridview。当图像(-)点击隐藏gridview
  • ServerSide: aspx.cs protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindHeaderGrid() } }
【解决方案2】:

检查一下。

JavaScript

        $.ajax({
                type: "POST",
                dataType: "json",
                data: {data: data}, // if no data available leave this out
                url: "example.asmx/exampleMethod", //if you wanna call a function in the page just include the function name in here
                success: function (data) {

                    // do something if the function is success

                }
            });

C# 网络服务

 public class Example: System.Web.Services.WebService
    {

        [WebMethod]
        public void exampleMethod(Parameters)
        {
            // Something you wanna do

           //If you wanna return something to javascript 
            Context.Response.ContentType = "text/HTML";
            var js = new JavaScriptSerializer();
            Context.Response.Write(js.Serialize(returnDataObject));
        }
}

在你的情况下,我认为他可能会工作。

        $.ajax({
                type: "POST",
                dataType: "json",
                url: "BindHeaderGrid()", 
                success: function (data) {

                }
            });

【讨论】:

    猜你喜欢
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 1970-01-01
    • 2022-08-17
    • 1970-01-01
    • 2013-05-26
    相关资源
    最近更新 更多