执行虚拟补丁ascx

时间:2018-04-18 07:57:30

标签: c# asp.net ascx

我的代码

protected void SendButton_Click(object sender, EventArgs e)
    {
        UserControl uc = new UserControl();
        bool Ok = false;
        string userControl = "";
        string ID = "0";
        ID = UCid.Text;
        if(ID == "1")
        {
            userControl = Server.MapPath(@"~\UCtest_one.ascx");
            Ok = true;
        }
        else if (ID == "2")
        {
            userControl = Server.MapPath(@"~\UCtest_two.ascx");
            Ok = true;
        }
        else
        {
            Response.Write("Eror!!! Enter only 1 or 2");
            Ok = false;
        }

        if (Ok)
        {
         //here
     uc = LoadControl(userControl) as UserControl;
            ViewUC.Controls.Add(uc);
        }
        else
        {
            Response.Write("Eror!!!");
        }
    }
  

System.Web.HttpException         的HResult = 0x80004005的    消息=' ... / UCtest_one.ascx'   是物理路径,但预计会有虚拟路径   Source = ASCXruntime StackTrace:at   ASCXruntime.Default.SendButton_Click(Object sender,EventArgs e)in   ... \ Default.aspx.cs:行   43

1 个答案:

答案 0 :(得分:0)

它的真实。

protected void SendButton_Click(object sender, EventArgs e)
{
    UserControl uc = new UserControl();
    bool Ok = false;
    string userControl = "";
    string ID = "0";
    ID = UCid.Text;
    if(ID == "1")
    {
        userControl = MapPath(@"~\UCtest_one.ascx");
        Ok = true;
    }
    else if (ID == "2")
    {
        userControl = MapPath(@"~\UCtest_two.ascx");
        Ok = true;
    }
    else
    {
        Response.Write("Eror!!! Enter only 1 or 2");
        Ok = false;
    }

    if (Ok)
    {

 uc = LoadControl(userControl) as UserControl;
        ViewUC.Controls.Add(uc);
    }
    else
    {
        Response.Write("Eror!!!");
    }
}