编写WebMethod并通过AJAX调用它

时间:2015-06-29 21:02:43

标签: c# jquery asp.net ajax twitter-bootstrap

我有一个问题,我的问题是我有一个Bootstrap模态,需要在点击链接按钮时显示,当按下链接按钮时,它会触发一个方法,填充模态体中的表格查询的结果集。

我认为唯一的解决方案是将方法设为WebMethod并在ASP.NET页面中通过AJAX调用它。

我将在下面发布我的代码:

ASP.NET

<asp:LinkButton href="#viewemydevices" data-toggle="modal" ID="ViewMyDevices" runat="server" OnClick="btnViewMyDevices_Click">
<div class="panel-footer announcement-bottom">
     <div class="row">
          <div class="col-xs-6">
          View Devices
          </div>
          <div class="col-xs-6 text-right">
              <i class="fa fa-arrow-circle-right"></i>
          </div>
     </div>
</div>
</asp:LinkButton>

<div class="modal fade" id="viewemydevices" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4>This is a section</h4>
            </div>
            <div class="modal-body">
                <table id="MyDevicesTable" class="table tbody" runat="server" visible="false">
                    <tbody>
                        <tr>
                            <td>
                                <asp:DataGrid ID="MyDevicesGrid" runat="server" CssClass="table table-striped tbody" Visible="false"
                                    AutoGenerateColumns="True"
                                    ForeColor="black"
                                    HeaderStyle-Font-Bold="true"
                                    HeaderStyle-ForeColor="black"
                                    GridLines="None"
                                    EnableViewState="false"
                                    AllowSorting="True"/>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <div class="modal-footer">
                <a class="btn btn-primary" data-dismiss="modal">Close</a>
            </div>

        </div>
    </div>
</div>

C#

protected void btnViewMyDevices_Click(object sender, EventArgs e)
    {
        string selectMyDevices = "My Query, it works fine";

        sqlCmd = new SqlCommand(selectMyDevices, sqlConn);
        sqlConn.Open();

        SqlDataReader rdrMyDevices = sqlCmd.ExecuteReader();

        //reads row into datagrid
        if (rdrMyDevices.HasRows)
        {
            //sets table/grid to visible
            MyDevicesGrid.Visible = true;
            MyDevicesTable.Visible = true;

            //adds data to grid
            MyDevicesGrid.DataSource = rdrMyDevices;
            MyDevicesGrid.DataBind();
        }
        sqlConn.Close();
    }

我正在寻找的是帮助我为我的下方方法标记WebMethod以及如何在我的ASP.NET中调用它...我知道它需要是一个静态方法... < / p>

谢谢!

0 个答案:

没有答案