一些ASP.NET实用技巧

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.

在ASP.NET应用程序中,我遇到了一些技术问题。 在本文中,我列出了它们并显示了找到的解决方案。 我希望它会有用。

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.

在父页面中,调用该方法以在任何click事件上弹出用户定义的页面。

  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);
window.onbeforeunload = RefreshParentPage;
//  ... 
function RefreshParentPage()
{
   // Refresh the parent page.
   window.opener.location.href = window.opener.location.href;
}

Problem:

问题:

Inside an asp:panel control, RequiredFieldValidator is not working

在asp:panel控件中,RequiredFieldValidator无法正常工作

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:

您可以将RequiredFieldValidator保留在面板之外。 这不会将错误消息放在文本框旁边。 要将错误消息放在文本框附近,我们必须使用Javascript函数显式验证,如下所示:

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; 
}

Problem:

问题:

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

在页面中显示PDF文件时,Reponse.End()引发异常。

Solution:

解:

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

可以使用Response.Close()代替Response.End()来避免异常。

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();
}

Problem:

问题:

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

在删除网格记录之前,必须向用户显示一个确认对话框。

Solution:

解:

In the grid, use LinkButton template column.

在网格中,使用LinkBut​​ton模板列。

<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>

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.

事件应该绑定到链接按钮,而数据使用onItemDataBound事件绑定到自身。

In client side:

在客户端:

<asp:DataGrid ID="grdResources" runat="server" AutoGenerateColumns="False"
 Width="100%" DataKeyField="ResID" OnItemDataBound="grdResources_ItemDataBound">
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;");
  }
}

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";

私有常量字符串FormsAdmin =“ FormsAdmin”;

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

}

翻译自: https://www.experts-exchange.com/articles/4148/Some-ASP-NET-Practical-tips.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值