...

View Full Version : Convert dynamic PHP include to C#



everah
07-14-2005, 06:59 PM
I have an application coded in PHP that accepts a user selection and passes that selection by URL back to itself to show something in the page. As an example, I have a list of calculators that a user can select. Each one is displayed as a URL. When the user clicks the URL the page is refreshed and shows the user the calculator they chose based on a value passed by the query string.



<?php
if (isset($_GET['calc_id']))
{
include($path_to_calcs . "calc_" . $_GET['calc_id'] . ".php");
}
else
{
//load a fresh page with a list of available selections as a post back
//use an array to store calc id's and link references
foreach ($calc_array as $calc_id => $calc_ref)
{
echo '<a href="' . $_SERVER['PHP_SELF'] . '?calc_id=' . $calc_id . '">' . $calc_ref . '</a>';
}
}
?>


How can this be done in C#? All help is appreciated.

Brandoe85
07-15-2005, 05:22 AM
Something like this? My form was WebForm1, I didn't make any form to accept a selection, but this should give you an idea...


// kinda like an associative array
SortedList calcs = new SortedList();
private void links()
{
// add values to sortedlist
calcs.Add("Calc1", "1");
calcs.Add("Calc2", "2");
calcs.Add("Calc3", "3");
// loop through list and create links
foreach(DictionaryEntry calc in calcs)
{
Response.Write("<a href=" + Request.ApplicationPath + "/WebForm1.aspx?calcId=" + calc.Value + ">" + calc.Key + "</a><br>");
}

}
private void Page_Load(object sender, System.EventArgs e)
{
// our calc id
string calcId = Request.QueryString.Get("calcId");
if(calcId != null)
{
// Write out the file...
Response.WriteFile("Calc" + calcId + ".html");
}
else
{
// load links...
links();
}
}

Note: Response.WriteFile() can write out HTML or client side scripts, not server side code. This should help explain... (http://support.microsoft.com/default.aspx?scid=kb;en-us;306575) Also, look into User Controls (http://www.google.com/search?hl=en&lr=&c2coff=1&q=asp.net+user+control&btnG=Search) Which is what you'll want to include server side pages. I'll try to post an example over the weekend.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum