protected void btnOpen_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtFileName.Text)) txtFileName.Text="c:/temp/temp.txt";
StreamReader sr = null;
try
{
sr = new StreamReader(txtFileName.Text);
txtFileContent.Text = sr.ReadToEnd();
sr.Close();
displayMessage("Successfully opened file");
}
catch (Exception ex)
{
displayMessage(ex.Message);
}
finally
{
sr.Close();
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtFileName.Text)) txtFileName.Text = "c:/temp/temp.txt";
StreamWriter sw = null;
try{
sw = new StreamWriter(txtFileName.Text);
string[] ar;
ar = txtFileContent.Text.Split(Environment.NewLine.ToCharArray());
foreach (string sIn in ar)
{
if(!string.IsNullOrEmpty(sIn))
sw.Write(sIn.Trim()+Environment.NewLine);
}
}catch (Exception ex)
{
displayMessageLine(ex.Message);
}
finally{
sw.Close();
}
}
Programming Journal C#, Java, SQL and to a lesser extent HTML, CSS, XML, and regex. I made this so other programmers could benefit from my experience.
Monday, June 23, 2008
Opening, Reading, and Writing a file
Here is an example of Opening/Reading a file in btnOpen_Click and Writing in btnSave_Click:
Saturday, June 21, 2008
Using a nullable boolean.
It is possible to assign a nullable boolean in C#. The trick is to use ? in the declaration. Once declared, one can use the HasValue() method. An example follows:
protected void Page_Load(object sender, EventArgs e)
{
bool? isUser = null;
lblStatus.Text = isUser.HasValue.ToString(); //displays false
// an int example: Nullable<int> i = null;
}
protected void btnCheckStatus_Click(object sender, EventArgs e)
{
bool? isUser = false;
lblStatus.Text = isUser.HasValue.ToString(); //displays true
}
Thursday, June 5, 2008
Clearing controls in GridView row using delete
Here is how to clear controls in GridView row using delete. Be careful that GridViewDeleteEventArgs is not GridViewDeletedEventArgs!
Notice the OnRowDeleting set to x_RowDeleting function and AutoGenerateDeleteButton. Now for the delete function:
<asp:GridView ID="gvStolenStatusInfo" runat="server" AutoGenerateColumns="false" AllowPaging="true"
EmptyDataText="No records found." PageSize="10" CssClass="gridView"
OnRowDeleting="gvStolenStatusInfo_RowDeleting" AutoGenerateDeleteButton="true">
<Columns>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:TextBox ID="txtuCode" runat="server" Text='<%# Bind("Code") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Comment">
<ItemTemplate>
<asp:TextBox ID="txtuComment" runat="server" Text='<%# Bind("Comment") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
References: http://www.codeproject.com/KB/webforms/Editable_GridView.aspx
protected void gvStolenStatusInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int rowID = e.RowIndex;
((TextBox)gvStolenStatusInfo.Rows[rowID].FindControl("txtuCode")).Text = string.Empty;
((TextBox)gvStolenStatusInfo.Rows[rowID].FindControl("txtuDate")).Text = string.Empty;
}
Monday, May 5, 2008
Multiple PostBack problem with GridView
I noticed my SelectedIndexChanged method being called twice in IE7 when I selected a gridview row. I used solution 2 at http://www.codeproject.com/KB/aspnet/GVImageCommandButtonProb.aspx to solve the problem.
Resetting a StringBuilder
Here is how to reset a StringBuilder:
References: http://forums.asp.net/t/1193708.aspx
myStringBuilder.Remove(0, myStringBuilder.Length) also works, but why bother with that one?
myStringBuilder.Length = 0;
References: http://forums.asp.net/t/1193708.aspx
Friday, April 25, 2008
Creating a GUID from scatch
Sometimes you might want a psuedo-random string value. This can be done with:
System.Guid.NewGuid().ToString();
System.Guid.NewGuid().ToString();
Saturday, April 5, 2008
Changing the default login page for use with LoginStatus control
Sometimes it is desirable to have a different default login page. For example, a simple site where administrators login at http://foo.com/admin/Login.aspx versus the standard http:foo.com/Login.aspx. The LoginStatus control will hyperlink back to the default login page. To change this default login page, change the web.config file as follows:
Reference: http://erlend.oftedal.no/blog/?blogid=55
<authentication mode="Forms" >
<forms loginUrl="~/admin/Login.aspx"></forms>
</authentication>
Subscribe to:
Posts (Atom)