0

我有一个 web 表单,其中三个用户控件加载在三个 asp:placeholders 中,其中包含需要从一组总共 20 个问题中随机化的问题(因此,20 个用户控件):

const string randomQuestion1 = "~/Qset1";
const string randomQuestion2 = "~/Qset2";
const string randomQuestion3 = "~/Qset3";
const string randomQuestion4 = "~/Qset4";
const string randomQuestion5 = "~/Qset5";


void Page_Load(object sender, EventArgs e)
{ 
    string questionPath1 = GetRandomQuestionPath1();
    string questionPath2 = GetRandomQuestionPath2();
    string questionPath3 = GetRandomQuestionPath3();

    Control question1 = Page.LoadControl(questionPath1);
    Control question2 = Page.LoadControl(questionPath2);
    Control question3 = Page.LoadControl(questionPath3);

    PlaceHolder1.Controls.Add(question1);
    PlaceHolder2.Controls.Add(question2);
    PlaceHolder3.Controls.Add(question3);
}


protected void btnSubmit_Click(object sender, EventArgs e)
{
    // add data to database...       
}

}

private string GetRandomQuestionPath1() 
{
    Random rnd = new Random(); 
    string[] files = Directory.GetFiles(MapPath(randomQuestion1), "*.ascx");
    string questionPath = Path.GetFileName(files[rnd.Next(files.Length)]); 

    return Path.Combine(randomQuestion1, questionPath);
} 

private string GetRandomQuestionPath2() 
{
    Random rnd = new Random(); 
    string[] files = Directory.GetFiles(MapPath(randomQuestion2), "*.ascx");
    string questionPath = Path.GetFileName(files[rnd.Next(files.Length)]); 

    return Path.Combine(randomQuestion2, questionPath);
}

private string GetRandomQuestionPath3() 
{
    Random rnd = new Random(); 
    string[] files = Directory.GetFiles(MapPath(randomQuestion3), "*.ascx");
    string questionPath = Path.GetFileName(files[rnd.Next(files.Length)]); 

    return Path.Combine(randomQuestion3, questionPath);
}

如何在一个 asp:placeholder 中放置三个用户控件并从一个目录而不是几个目录中随机化这些用户控件?

谢谢 :)

编辑:

另外,我想获得三个随机用户控件的属性。这些用户控件只包含一个文本框。如果用户控件是静态添加的,我可以获得该属性。动态添加时失败。在 aspx 文件中使用 <%@ Reference Control="~/Qset1/qOne.ascx" %>。

4

0 回答 0