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.

Thursday, November 29, 2007

How to implement Ajaxified details view hide on PageIndexChanging

Previously I showed how to enable DetailsView with a Select button (link). But here is how to get rid of the DetailsView if Paging is enabled by using the PageIndexChanging method.

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
// get rid of the details view
DetailsView1.DataSource = null;
DetailsView1.DataBind();

GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
To Ajaxify it within an update panel, add a Trigger for the Gridview1 object and the PageIndexChanging event.

Generate Random numbers

Here's a quick one on how to generate random numbers. link

Random rnd = new Random();
int myRandom = rnd.Next();

Implementing Dynamic Hyperlink of Current Page

Here's how to implement a dynamic HyperLink of the current page based on the current pages title and current URL without the query string parameters. This is located in the Master Page.


protected void Page_Load(object sender, EventArgs e)
{
hlPageTitle.Text = Page.Title.ToString();
// old way would fetch query string paramaters
//hlPageTitle.NavigateUrl = Request.Url.AbsoluteUri.ToString();
hlPageTitle.NavigateUrl = Request.Url.GetLeftPart(UriPartial.Path);
}
Reference link.

Monday, November 26, 2007

Fixing WebForms.PageRequestManagerServerErrorException Status code 500

Here's a fix for the error WebForms.PageRequestManagerServerErrorException Status code 500.
I modified the MasterPage's ScriptManager as follows:

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="false">
</asp:ScriptManager>


UPDATE: This might fix the error, but it limits the functionality by not allowing Partial Page Rendering. This may not be acceptable. Here is a hint to avoid breaking the AJAX:

Hint: Be careful when changing Control names. The IDE won't catch the name change in the Extenders section of the TargetControlID.

Sunday, November 25, 2007

How to implement Select on a GridView1 with Paging enabled.

How to implement Select on a GridView1 with Paging enabled based on a DataTable.

protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
// e.NewSelectedIndex -> 0 to n
// GridView1.PageCount -> (n-1) always
// GridView1.PageIndex -> 0 to Page#
int rowN = 1 + ((GridView1.PageCount-1) * GridView1.PageIndex + e.NewSelectedIndex);
DetailsView1.HeaderText = "Details View of Row "+ rowN.ToString();
DetailsView1.PageIndex = rowN - 1;
GridView1.SelectedIndex = rowN - 1;
DetailsView1.DataSource = Cache["dt"];
DetailsView1.DataBind();
}

How to enable paging on a GridView that is bound to a DataTable.

How to enable paging on a GridView that is bound to a DataTable. On the GridView, simply

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = Cache["dt"];
GridView1.DataBind();
}


link

Saturday, November 17, 2007

Using Yahoo Mail Web Service hints

This should help you use Yahoo Mail Web Service for C# .NET.
First, go to link and download code sample for C#.
Second, use the APIs at link. I prefer the html link.
Third, get an application id at link. Be sure that this is the destination page where you will acquire the credentials using Request.QueryString[xTag].
Fourth, the above doesn't really explain a detail that will let the Web Service actually return Messages. Make sure that the xTagIsSpecified boolean is set before the request!
NOTE: The code sample below only works with premium accounts. Accounts link.


// <ListMessages>
// ListMessages, lists the messages in a given folder.
ListMessages listRequest = new ListMessages();
listRequest.fid = "Inbox";
listRequest.startMid = 0;
listRequest.startInfo = 0;
listRequest.numMid = 10;
listRequest.numInfo = 1;
listRequest.startMidSpecified = true;
listRequest.startInfoSpecified = true;
listRequest.numMidSpecified = true;
listRequest.numInfoSpecified = true;
ListMessagesResponse listResponse = ymwsInstance.ListMessages(listRequest);
retVal += string.Format("<br />Listed folder <b>{0}</b>, found <b>{1}</b> message IDs.<br />",
listResponse.folder.folderInfo.name, listResponse.mid.Length);
string[] listedMed = ymwsInstance.ListMessages(listRequest).mid;
// </ListMessages>

Wednesday, November 14, 2007

Well documented C# code sample

Well documented C# code sample demonstrates region, summary, and parameter documentation.


#region well documented sample method sample
/// <summary>
/// Saves the form size when it is resized.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SampleBrowser_SizeChanged(object sender, EventArgs e)
{
Properties.Settings.Default.FormClientSize = this.ClientSize;
}

/// <summary>
/// Saves all settings when form is closed.
/// </summary>
#endregion

Note: In addition to "param", one can use:

/// <returns>State of log in</returns>

Also: The #endregion must be on a new line!

Tuesday, November 13, 2007

Caching Methods in ASP.NET 2.0

How To Cache



























Method Implementation Sample uses

Programatic Caching


Set:


Cache.Insert("UniqueKey", myData,
null, DateTime.Now.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration);



Get:

Cache["UniqueKey"].ToString();


Session State Caching


Set:


Session.Add("UniqueKey", myData);





Get:


Session["UniqueKey"];


Caching to Disk

Application Cache Block


Caching to Database

CodeProject
Article


link

Monday, November 12, 2007

Modifying labels in MasterPage From MasterPage user Page

First, here is how to modify a label in the MasterPage from the MasterPage user Page:


Label mpLabel = (Label)Master.FindControl("lblPageTitle");
if (mpLabel != null)
{
mpLabel.Text = this.Title.ToString();
}

link

Monday, November 5, 2007

Setup an email form for .NET 2.0

Here is how I setup the email action for .NET 2.0. I included "using System.Net.Mail;" and "using System.Net;"


string sTo = "foo@foo.com";
string sFrom = txtFrom.Text.Trim();
string sSubject = "inforequest message" + DateTime.Now.ToLongDateString();
string sBody = txtContent.Text.Trim();
lblStatus.Text = "Sending... please wait";
MailMessage msg = new MailMessage(sFrom, sTo, sSubject, sBody);
msg.IsBodyHtml = false;
SmtpClient mailObj = new SmtpClient("mail.foo.com");
mailObj.Credentials = new NetworkCredential("user", "secret");
mailObj.Send(msg);
lblStatus.Text = "Sent " + msg.Subject + " to " + msg.To;

references: link
Note: I used "localHost" instead of "mail.foo.com".