<

[Webinar] Streamline your web hosting managementRegister Today

x

Some ASP.NET Practical tips

Published on
11,037 Points
3,637 Views
4 Endorsements
Last Modified:
Approved
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful.

Problem:
After closing a pop-up window, the parent page should be refreshed automatically.

Solution:
In the parent page, call the method to pop-up a user defined page on any click event.
  var url = 'ManageFormsPopup.aspx';
 //Show the category window to manage categories
 var prefrence="height=400,width=500,status=yes,toolbar=no,menubar=no";
 window.open(url, null, prefrence);

Open in new window

In the child page, trigger the onbeforeunload method.
window.onbeforeunload = RefreshParentPage;
//  ... 
function RefreshParentPage()
{
   // Refresh the parent page.
   window.opener.location.href = window.opener.location.href;
}

Open in new window


Problem:
Inside an asp:panel control, RequiredFieldValidator is not working

Solution:
You can keep the RequiredFieldValidator outside of the panel.  This will not place the error message next to the text box. To place error message near to the text box, we have to validate explicitly using a Javascript function, like so:

function Validate()
{
  var categoryName = document.getElementById('<%=txtCategoryName.ClientID%>').value;

  if(categoryName.toString() == "")
  {
    document.getElementById('<%=lblValidate.ClientID%>').innerHTML = "Please enter Category Name";
    return false;
  }
  else {
    document.getElementById('<%=lblValidate.ClientID%>').innerHTML = "";
  }
  return true; 
}

Open in new window


Problem:
While displaying a PDF file in a page, Reponse.End() throws an exception.

Solution:
Instead of Response.End() we could use Response.Close() to avoid the exception.

if (System.IO.File.Exists(FilePathVal))
{
    FileInfo fi = new FileInfo(FilePathVal); 
    long size = fi.Length;
    Response.AddHeader("Content-Length", size.ToString());
    Response.ContentType = "application/pdf";
    Response.BinaryWrite(File.ReadAllBytes(FilePathVal));
    Response.Flush();
    Response.Clear();
}

Open in new window


Problem:
Before deleting a grid record, the user must be presented with a confirmation dialog.

Solution:
In the grid, use LinkButton template column.
<asp:TemplateColumn HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="lblDelete" CommandName="Delete" runat="server" Text="Delete"
 OnClientClick="javascript:return confirm('Are you sure you want to delete ?');"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>

Open in new window


Problem:
In a Grid link button (for example, clicking on Filename link button), a single click is not working sometimes.  Instead double clicking is working.

Solution:
The events should be bound to the link button while data bound itself using onItemDataBound event.
In client side:
<asp:DataGrid ID="grdResources" runat="server" AutoGenerateColumns="False"
 Width="100%" DataKeyField="ResID" OnItemDataBound="grdResources_ItemDataBound">

Open in new window

In server side:
protected void grdResources_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
Private const URL = "some file name";
   if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==  ListItemType.AlternatingItem)
  {
    lnkSelect.Attributes.Add("onclick", "javascript:window.open('" + URL + "'); return true;");
  }
}

Open in new window


Tip:
From a string array, you would like to check existence of a string quickly?
Create a List item for the string array and use Contains method of it.

private const string FormsAdmin = "FormsAdmin";
string[] Roles = cUser.Roles; 
List<string> RoleList = new List<string>(Roles);
if (RoleList.Contains(FormsAdmin))
{
  // Found the string.

}

Open in new window

4
Comment
2 Comments
 
LVL 16

Author Comment

by:Easwaran Paramasivam
Happy Reading! Happy coding!!
0
 
LVL 1

Expert Comment

by:myaspnet
thank you
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Join & Write a Comment

As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
Other articles by this author
Suggested Courses
Course of the Month8 days, 5 hours left to enroll

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month