Quantcast
Channel: Web Forms
Viewing all articles
Browse latest Browse all 23244

Display multiple user controls in one asp:placeholder with randomizing

$
0
0

I have a webform with three user controls loaded in three asp:placeholders containing questions that needs to be randomized from a set of total 20 questions(so, 20 user controls):

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);
}


How can i place three user controls in one asp:placeholder and randomize these user controls from one directory instead of several?

Thanks in advance! 


Viewing all articles
Browse latest Browse all 23244

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>