I recently developed a project at my job which discloses candidate information to the public on cases where the filer was bought to hearing.
The form I designed used a ton of CSS to make the form look like a desktop application. It will be published in April 2009 at http://www.pdc.wa.gov/home/enforcement/searchdb. My problem was that the agency felt that some fields should not be visible to the public. My task was a simple solution.
I figure internal users will use the same page as external users. If I can capture the ip address of the internal user then I can display the labels and textboxes, other users will get the default setting of visible=false;
Here's the code.
string sAddress = Request.ServerVariables["REMOTE_ADDR"].ToString();
if (sAddress == "127.0.0.1")
sAddress = "192.168.1.127"; // handles ip ranges for debugging
int iAddress = sAddress.Length;
string sRemoteAddress = sAddress.Remove(10, iAddress - 10);
if (sRemoteAddress == "192.168.1")
{
//Do something
}
else
{
Response.Redirect("~/default.aspx");
}
Here's how it works.
Request.ServerVariables["REMOTE_ADDR"] gets the current connections ip address and returns the ipaddres to the string sAddress.
Because I was using the application in my visual studio project I didn't want to keep getting redirected to my home page, so I added some code to check to see if I have a local ip of 127.0.0.1. If so, then manually give me the ip address of 192.168.1.127.
Here's were it gets tricky.
int iAddress = sAddress.Length
sAddress.Length returns the number of characters in the ipaddress. I did this because the length could vary in an ipaddress. It passes the length to iAddress which is type int.
Next I drop all the characters from the beginning of the 10th character to the end of iaddress length - 10.
This will drop the last octate of the ip address and then check the string sRemoteAddress value against my ip address in my if statement.
This works really great to redirect users if you have a page published to the internet where both internal and external users are visiting the site, but you don't want external users seeing the page.
Now, I can add to my statement something like this instead of the long if statement:
if(sRemoteAddress != "192.168.1")
Response.Redirect("~/default.aspx");
or if I wanted to hide a few controls like labels and textboxes add them to the list:
if(sRemoteAddress == "192.168.1")
{
Label.Visible = true;
TextBox1.Visible = true;
}
or
set a global variable of type bool like this:
bool _diplayobjects = false;
Now in my if statement do this:
if(sRemoteAddress == "192.168.1")
{
_displayobjects = true;
}
else
{
// redirect the page
}
Label1.Visible = _displayobjects;
TextBox.Visible = _displayobjects;
Hi everyone. A programmers notebook started out as a place for me to post things that I find on the internet for myself. I guess you could say that these have proven to be beneficial to everyone. Enjoy!
Friday, January 30, 2009
Wednesday, January 28, 2009
C# codebehind Dynamic Hyperlinks
I was sitting at my desk one day and trying to figure out how to take some of the web page updating off my plate. One of the pages on our site is updated with a link every year. For example:
2009 Litigation Report
2008 Litigation Report
2007 Litigation Report
it links to a pdf document for which I've already designed an upload panel to handle sending the file to the appropriate places on the server. I just needed a way to create the link dynamically every year at a certain time.
So like in my previous post, I used the timespan class to create a block of time.
Anyway, the code is very simple: I created a separate project to do my testing. I added a placeholder to the page. In codebehind I wrote this:
protected void Page_Load(object sender, EventArgs e)
{
const int _year = 2008;
int _currentYear = DateTime.Now.Year;
for (int i = _currentYear; i >= _year; i--)
{
HyperLink ln = new HyperLink();
ln.ID = "link" + i;
ln.Text ="
ln.NavigateUrl = "litigation.aspx?litigationyear=" + i;
PlaceHolder1.Controls.Add(ln);
}
PlaceHolder1.DataBind();
}
Enjoy
2009 Litigation Report
2008 Litigation Report
2007 Litigation Report
it links to a pdf document for which I've already designed an upload panel to handle sending the file to the appropriate places on the server. I just needed a way to create the link dynamically every year at a certain time.
So like in my previous post, I used the timespan class to create a block of time.
Anyway, the code is very simple: I created a separate project to do my testing. I added a placeholder to the page. In codebehind I wrote this:
protected void Page_Load(object sender, EventArgs e)
{
const int _year = 2008;
int _currentYear = DateTime.Now.Year;
for (int i = _currentYear; i >= _year; i--)
{
HyperLink ln = new HyperLink();
ln.ID = "link" + i;
ln.Text ="
" + i.ToString() + " Litigation Report
";ln.NavigateUrl = "litigation.aspx?litigationyear=" + i;
PlaceHolder1.Controls.Add(ln);
}
PlaceHolder1.DataBind();
}
Enjoy
C# codebehind Populating Dropdownlist with years
I was working on a project at work which is a little lenghty in describing, but basically I needed a way to get out of constantly being interrupted by staff when one or a dozen pdf files need to be updated or added to the website.
At my job there are only two developers. Bob has over 30 years experience programming, but no C#. So I was tasked with helping him. It wasn't difficult because he knows Java, but getting him to understand how that interacts with asp.net web page controls took time.
Because we are few in number, anything that has to do with programming we do it. That also includes updates daily to the website. When I first arrive 70% of my interruptions were due to web updates, deletions and so forth. My boss wanted me to get out of the web page maintenance job, unfortunately there wasn't any money to hire anyone else.
So I came up with a solution, well, many solutions. Create pages that are dynamic and interactive.
Here is one solution I solved:
In the past all pdf documents were stored in one single directory. Naming conventions varied based on who created the pdf and copied it to the web server. Links on the site broke constantly, files were overwritten without regard to who may need them.
What I did was created a web page to manage the content. Created several tables in the database to manage the information. The pages contained controls such as Labels, Upload Control, TextBox Controls and Dropdownlist.
I provided the user of the control with a visual of what the name of the pdf should be based on their selections from the dropdownlist Controls, TextBox Controls and so forth. This help to keep a consistent naming standard.
I also had the page load method check for the current year, then pass that value into if(!Directory.Exists) statement to see if that year exists, if not, then I tell it to create a new folder and pass the path to the Upload Control and the insert statement and now we have our solution.
Here is a way to dynamically populate a dropdownlist control with years:
In your method or page load method:
int _currentyear;
const int _year = 2008
Keep in mind the Datetime.Now.Year returns an int.
protected void Page_Load(object sender, EventArgs e)
{
int _currentyear;
const int _year = 2008;
_currentyear = DateTime.Now.Year;
for (int s = _year; s <= _currentyear; s++)
{
DropDownList1.Items.Add(new ListItem((s).ToString(),(s).ToString()));
}
DropDownList1.DataBind();
}
Make sure you drop a dropdownlist control on your webpage or this will not work;
Now if you want to do the years in reverse order change your for statement:
for(int s = _currentyear; s >=_year; s--)
If year is 2009 and you want that year to be the selected text, you will need to change your code slightly to block the code in a if(!IsPostBack)
Then add: DropDownList1.SelectedValue = _currentyear;
At my job there are only two developers. Bob has over 30 years experience programming, but no C#. So I was tasked with helping him. It wasn't difficult because he knows Java, but getting him to understand how that interacts with asp.net web page controls took time.
Because we are few in number, anything that has to do with programming we do it. That also includes updates daily to the website. When I first arrive 70% of my interruptions were due to web updates, deletions and so forth. My boss wanted me to get out of the web page maintenance job, unfortunately there wasn't any money to hire anyone else.
So I came up with a solution, well, many solutions. Create pages that are dynamic and interactive.
Here is one solution I solved:
In the past all pdf documents were stored in one single directory. Naming conventions varied based on who created the pdf and copied it to the web server. Links on the site broke constantly, files were overwritten without regard to who may need them.
What I did was created a web page to manage the content. Created several tables in the database to manage the information. The pages contained controls such as Labels, Upload Control, TextBox Controls and Dropdownlist.
I provided the user of the control with a visual of what the name of the pdf should be based on their selections from the dropdownlist Controls, TextBox Controls and so forth. This help to keep a consistent naming standard.
I also had the page load method check for the current year, then pass that value into if(!Directory.Exists) statement to see if that year exists, if not, then I tell it to create a new folder and pass the path to the Upload Control and the insert statement and now we have our solution.
Here is a way to dynamically populate a dropdownlist control with years:
In your method or page load method:
int _currentyear;
const int _year = 2008
Keep in mind the Datetime.Now.Year returns an int.
protected void Page_Load(object sender, EventArgs e)
{
int _currentyear;
const int _year = 2008;
_currentyear = DateTime.Now.Year;
for (int s = _year; s <= _currentyear; s++)
{
DropDownList1.Items.Add(new ListItem((s).ToString(),(s).ToString()));
}
DropDownList1.DataBind();
}
Make sure you drop a dropdownlist control on your webpage or this will not work;
Now if you want to do the years in reverse order change your for statement:
for(int s = _currentyear; s >=_year; s--)
If year is 2009 and you want that year to be the selected text, you will need to change your code slightly to block the code in a if(!IsPostBack)
Then add: DropDownList1.SelectedValue = _currentyear;
C# Timespan in Web page
A few weeks back we had some serious weather problems here in the Northwest. Snow normally doesn't last a day on the ground, but during these few weeks snow continued to blanket the ground.
This complicated driving for many of my Washington neighbors. Because I came from Detroit Michigan, driving in snow is like riding a bike. Once you learned how, you can do it when necessary.
My job is located downtown right off the Puget Sound, which is simply a valley, so when to get downtown from any direction accessible, you have to brave the hills downward. When the snow came the agency that I work for needed a banner that would tell the public that we are closed due to inclement weather. It would only need to display on a certain day between certain hours and disappear.
This is the code I wrote:
</asp:Panel ID="Weather" runat="server" Visible="false">
<div style="color: Red; padding-left: 5px; padding-right: 5px; padding-bottom: 5px;padding-top: 5px; background-color: Black">
<span style="color: Yellow; font-weight: bold">Weather Alert!</span></br >
Due to Inclement Weather the Public Disclosure Commission will be Closing 12 noon
until 5pm Monday December 22, 2008.
</div>
</asp:Panel>
I placed a panel control with an ID="Weather" on the web page and set the visible default value to false so that it will not show when visitors first come to the site. I used style sheets to style the alert.
Next in the codebehind at the Page_Load level I used the Timespan class and created an instance of the class... heres the code.
TimeSpan ts = DateTime.Now.TimeOfDay;
if(ts.Hours >= 10 && ts.Hours <= 17 && DateTime.Now.Day == 22)
{
Weather.Visible = true;
}
As you can see the time span is between 10am and 5pm on 22nd of the month; it's important that you remove this completely from your page_load later otherwise it will appear every month on the 22nd. You can modify the code to include the month like this:
DateTime.Now.Month which is a type int;
Anyways, this worked great. Peace.
This complicated driving for many of my Washington neighbors. Because I came from Detroit Michigan, driving in snow is like riding a bike. Once you learned how, you can do it when necessary.
My job is located downtown right off the Puget Sound, which is simply a valley, so when to get downtown from any direction accessible, you have to brave the hills downward. When the snow came the agency that I work for needed a banner that would tell the public that we are closed due to inclement weather. It would only need to display on a certain day between certain hours and disappear.
This is the code I wrote:
</asp:Panel ID="Weather" runat="server" Visible="false">
<div style="color: Red; padding-left: 5px; padding-right: 5px; padding-bottom: 5px;padding-top: 5px; background-color: Black">
<span style="color: Yellow; font-weight: bold">Weather Alert!</span></br >
Due to Inclement Weather the Public Disclosure Commission will be Closing 12 noon
until 5pm Monday December 22, 2008.
</div>
</asp:Panel>
I placed a panel control with an ID="Weather" on the web page and set the visible default value to false so that it will not show when visitors first come to the site. I used style sheets to style the alert.
Next in the codebehind at the Page_Load level I used the Timespan class and created an instance of the class... heres the code.
TimeSpan ts = DateTime.Now.TimeOfDay;
if(ts.Hours >= 10 && ts.Hours <= 17 && DateTime.Now.Day == 22)
{
Weather.Visible = true;
}
As you can see the time span is between 10am and 5pm on 22nd of the month; it's important that you remove this completely from your page_load later otherwise it will appear every month on the 22nd. You can modify the code to include the month like this:
DateTime.Now.Month which is a type int;
Anyways, this worked great. Peace.
Subscribe to:
Posts (Atom)