如何从WebMethod加载ascx

时间:2017-11-02 03:20:24

标签: c# asp.net .net webmethod aspx-user-control

在WebMethod完成SQL任务后,我一直在努力加载.ascx(用户控件)。

WebMethod由Ajax函数

触发
$(document).ready(function () {
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_endRequest(function () {
        $(".use-address").on("click", function () {
            var $row = $(this).closest("tr");
            var $text = $row.find(".nr").text();
            var $table = $(this).closest('table').attr('id')

            $.ajax({
                type: "POST",
                url: "wbfMain.aspx/register_activity",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify({ id_permission: $text, _selected_table: $table }),
                dataType: 'json'
            });
        });
    });
});

public partial class wbfMain : System.Web.UI.Page
{
    public static Control myControl = new Control();
    public static Control myControl_E = new Control();
    public static Control myControl_L = new Control();
    public static PlaceHolder plch = new PlaceHolder();

    protected void Page_Load(object sender, EventArgs e)
    {
        plch = placeHolder;
        myControl_E = Page.LoadControl("wbucEntradas.ascx");
        myControl_L = Page.LoadControl("wbucSalidas.ascx");
        placeHolder.Controls.Clear();
        myControl = Page.LoadControl("wbucWelcome.ascx");
        placeHolder.Controls.Add(myControl);
    }

    protected void lkLoadEntradas_Click(object sender, EventArgs e)
    {
        plch.Controls.Clear();
        plch.Controls.Add(myControl_E);
    }

    protected void lkLoadSalidas_Click(object sender, EventArgs e)
    {
        plch.Controls.Clear();
        plch.Controls.Add(myControl_L);
    }

    [WebMethod]
    public static void register_activity(int id_permission, string _selected_table)
    {
        wbfMain main = new wbfMain();
        clsSQLCommands SQLCommands = new clsSQLCommands();
        SQLCommands.SQL_register_activity_time(id_permission, (_selected_table == "detailTable") ? 1 : 2);
        if(_selected_table== "detailTable")
        {
            //Doesn't load the User control
            plch.Controls.Clear();
            plch.Controls.Add(myControl_L);
        }
        else
        {
            //Doesn't load the user control
            plch.Controls.Clear();
            plch.Controls.Add(myControl_E);
        }
    }
}

在WebMethod触发用户控件之后,User控件内部有一个Page_Load,它从数据库中请求一个HTML字符串来创建一个表

public partial class wbucSalidas : System.Web.UI.UserControl
    {
        clsSQLCommands SQLCommands = new clsSQLCommands();
        protected string MyString { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            string test = SQLCommands.SQL_SP_Get_Daily_Activity(1);
            MyString = test;
        }
    }

用户控件Page_Load触发以下两种方法

public string SQL_SP_Get_Daily_Activity(int option)
        {
            DataTable data = new DataTable();
            string htmlInfo = "";
#if DEBUG
            using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["EAMStringDebug"].ConnectionString))
#else
            using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["EAMString"].ConnectionString))
#endif
            using (var cmd = new SqlCommand("sp_get_daily_activity", conn))
            using (var adapter = new SqlDataAdapter(cmd))
            {
                cmd.Parameters.Add("@option", SqlDbType.Int).Value = option;
                cmd.CommandType = CommandType.StoredProcedure;
                adapter.Fill(data);
                htmlInfo = ConvertDataTableToHTML(data);
            }
            return htmlInfo;
        }

        public static string ConvertDataTableToHTML(DataTable dt)
        {
            string html = "";
            html += "<thead><tr>";
            for (int i = 0; i < dt.Columns.Count; i++)
                html += "<td>" + dt.Columns[i].ColumnName + "</td>";
            html += "<td>" + "Registro de Actividad" + "</td>";
            html += "</tr></thead>";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                html += "<tbody><tr>";
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    if(j == 0)
                    {
                        html += "<td class='nr'>" + dt.Rows[i][j].ToString() + "</td>";
                    }
                    else
                        html += "<td>" + dt.Rows[i][j].ToString() + "</td>";
                    if (j + 1 == dt.Columns.Count)
                    {
                        html += "<td>" + "<button type='button' class='btn btn-primary use-address' runat='server'>Seleccionar</button>" + "</td>";
                    }
                }
                html += "</tr></tbody>";
            }
            return html;
        }

我试图做的是加载(&#34; wbucSalidas.ascx&#34;)如果_selected_table中收到的值是&#34; detailTable&#34;,问题是一旦进入if条件没有任何反应,我非常感谢你能给我的任何帮助。

注意:单击ASP链接按钮时加载用户控件没有问题,问题出在WebMethod

0 个答案:

没有答案