About the author

Vijay Kodali
E-mail me Send mail

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2024

Page cannot be displayed error with Asp.Net FileUpload

 The File Upload server control provided by ASP.NET allows users to upload one or more files to the server. By default, ASP.NET permits only files that are 4,096 KB (or 4 MB) or less to be uploaded to the Web server. To upload larger files, you must change the maxRequestLength parameter of the <httpRuntime> section in the Web.config file.


<system.web>
  <httpRuntime executionTimeout="240" maxRequestLength="20480" />
</system.web>  

 The value of maxrequestlength is in KB. In this case it's allowed up to 20 MB


Categories: ASP.NET
Posted by vijay on Friday, November 16, 2007 3:13 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Java Script function to only allow integers in the textbox

Here is sample code for TextBox Validation to allow Numbers only.

[code:c#]

<script type = "text/javascript" language = "javascript">

function CheckTextBox(i)
{
    if(i.value.length>0)
    {
    i.value = i.value.replace(/[^\d]+/g, '');
    }
}

</Script>

[/code]

In Code behind add this function to "onkeyup" event for TextBox

[code:c#]

TextBox1.Attributes.Add("onkeypress", "CheckTextBox(this)");

[/code]


Posted by vijay on Wednesday, November 14, 2007 7:54 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Auto refresh a web form asp.net

Some times we need to auto-refresh page after few seconds. you can do it with Meta Refresh tags

<head><meta http-equiv="Refresh" content="180" /></head>

Where content '180' is the number of seconds.

Another approach is by addding HTTP header..

[code:c#]

Response.AppendHeader("Refresh", "180");

[/code]


Posted by vijay on Sunday, October 14, 2007 3:24 PM
Permalink | Comments (1) | Post RSSRSS comment feed

To add Tool Tip for DropDownList items in Asp.Net

Here is sample code for adding tooltip to DropDownList. The key here is adding "title" attribute to the list items in DataBound event.

<asp:DropDownList ID="DropDownList1" runat="server" ondatabound="DropDownList1_DataBound">
</asp:DropDownList>
In code behind ..
protected void DropDownList1_DataBound(object sender, EventArgs e)
{
    DropDownList ddl = sender as DropDownList;
    if (ddl != null)
    {
        foreach (ListItem li in ddl.Items)
        {
           li.Attributes["title"] = li.Text; // setting text value of item as tooltip
        }
    }

}
 

Tags:
Posted by vijay on Thursday, October 11, 2007 9:15 PM
Permalink | Comments (1) | Post RSSRSS comment feed

Limit number of items to be selected in Asp.Net ListBox

Here is an example to validate ListBox. This validation will check user to select less than10 items 
 
<script type="text/javascript" language="javascript">
function CustomValidator1_ClientValidate(source,args)
{ 
var listbox = document.getElementById("<%=ListBox1.ClientID %>");
var total = 0;
var i=0;
for( i = 0; i < listbox.options.length; i++ )
{
 if (listbox.options[i].selected) 
  {
   total ++; 
    if (total >10) {
      args.IsValid = false;
      return;
  }
  } 
 } 
 args.IsValid = true; return;
}
</script>

 

<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple" Height="100%"
Width="100%"></asp:ListBox>
<asp:Button ID="Button3" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:CustomValidator ID="CustomValidator1" runat="server" Display="Dynamic" ErrorMessage="Please select 10 items only."
ClientValidationFunction="CustomValidator1_ClientValidate" OnServerValidate="CustomValidator1_ServerValidate">
</asp:CustomValidator>

Code Behind:

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) 
{ 
  int count = 0; 
  for (int i = 0; i < ListBox1.Items.Count; i++) 
  { 
        if (ListBox1.Items[i].Selected) 
            count++; 
  } 
 args.IsValid = (count > 10) ? false : true; 
} 



Tags:
Posted by vijay on Wednesday, September 19, 2007 9:45 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Click event for Listbox

In Asp.Net ListBox, there are no events for Click action. The follwing code is useful in this regard..

 

[code:c#]

protected void Page_Load(object sender, EventArgs e)

{

if (Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"] == "submit")    {       

//your code for click action

                                                      }

ListBoxNames.Attributes.Add("ondblclick", ClientScript.GetPostBackEventReference(ListBoxNames, "submit"));

}

[/code]


Categories: ASP.NET | C#
Posted by vijay on Wednesday, September 12, 2007 12:57 PM
Permalink | Comments (2) | Post RSSRSS comment feed

to call server side function from Javascript function?

Some times we need to call server side function from Javascript function.For example,callig a PoupWindow(with value from code behind) on HyperLink click. 

This must be implemented by creating a additional request to the web server and that’s the only way to invoke methods on the server. Page methods are used for this.

What is PageMethod?

A PageMethod is a public static method that is exposed in the code-behind of a page and can be called from the client script. PageMethods are annotated with [System.Web.Services.WebMethod] attribute.

The code is simple..

Server side function

[code:c#]

[System.Web.Services.WebMethod] public static void Score(string ID, string name, string Role) {
         try {
                //Some data manipulations 
                  return finalScore ; 
              }
         catch (Exception ex) { 
               //Response.Write(ex.ToString());
                                  } 
            } 

[/code]

//Client Side function 

[code:c#]

function score()
              {               
                  PageMethods.Score(txtID.value,txtName.value,txtRole.value);            
               } 

[/code]

<asp:HyperLink ID="btn_Test" runat="server" Text="Calculate" OnClientClick="javascript:return score();"/> 

Tags:
Categories: AJAX | ASP.NET | C#
Posted by vijay on Friday, August 24, 2007 12:23 PM
Permalink | Comments (0) | Post RSSRSS comment feed

ListSearchExtender AJAX control

The ListSearchExtender lets you search for items in a ListBox or DropDownList by typing.  The extender performs an incremental search within the ListBox based on what has been typed so far.

the code is simple 

 <ajaxToolkit:ToolkitScriptManager ID="ScriptManager1" EnablePartialRendering="true" EnableScriptGlobalization="true" EnableScriptLocalization="true" runat="Server" />

ScriptManger is required for all AJAX controls..

 <ajaxToolkit:ListSearchExtender ID="ListSearchExtender1" runat="server" TargetControlID="ListBox1" PromptCssClass="ListSearchExtenderPrompt" PromptPosition="Top"></ajaxToolkit:ListSearchExtender/>

  • PromptText - Message to display when the ListBox is given focus. 
  • TargetControlID - Control to be searched
  • PromptPosition - Indicates whether the message should appear at the Top or Bottom of the ListBox.  The default is Top.

Categories: AJAX | ASP.NET
Posted by vijay on Saturday, August 18, 2007 6:09 PM
Permalink | Comments (2) | Post RSSRSS comment feed

Read-only Textbox View state problem in ASP.NET

For a Textbox with Read-only property set to true, text is not be posted back to the server side.

From MSDN..

The Text value of a TextBox control with the ReadOnly property set to true is sent to the server when a postback occurs, but the server does no processing for a read-only text box. This prevents a malicious user from changing a Text value that is read-only.

To set a Textbox read-only and keep the text on post back, use the following code..

We can get it from the Request object as shown below…

  TextBox1.Text = Request[TextBox1.UniqueID]; //Another approach… 
  TextBox1.Text = Request.Form[TextBox1.ClientID.Replace("_", "$")];

Posted by vijay on Tuesday, July 31, 2007 2:20 PM
Permalink | Comments (2) | Post RSSRSS comment feed