Thursday 14 March 2013

Calling code behind function from a html button

HTML button click call code behind in asp.net



<input type="button" id="btnuplod" runat="server" value="Upload" onclick="__doPostBack('btnSubmit','Submit');">

Code Behind
 
protected void Page_Load(object sender, EventArgs e)
{
   if (IsPostBack)
   {
      if (Convert.ToString(Request.Form["__EVENTARGUMENT"]) == "Submit")
      {
          btnuplod_Click();//submit action here
      }
   }
}

Your Function


Public void btnuplod_Click()
{
      if (FileUploadPDF.HasFile)
        {
            string fileName = Server.HtmlEncode(FileUpload1.FileName);
            string extension = System.IO.Path.GetExtension(fileName);
            if (extension == ".pdf")
            {

            }
            else
            {
                lblerror.InnerText = "Please upload PDF Files Only";
            }
        }
}
 

Thursday 7 March 2013

Disable Browser Back Button Functionality using JavaScript in ASP.Net

Solutions 1:  
 
Suppose there are two pages Main1.aspx and Main2.aspx and Main1.aspx redirects
 to Main2.aspx

Hence to prevent user from visiting Main1.aspx using Back Button you will need
to place the above script in the head section of Main1.aspx as shown below. 

<script type = "text/javascript" >
         function preventBack() { window.history.forward(); }
         setTimeout("preventBack()", 10);
         window.onunload = function() { null };
 

    </script>


I have tested the script in the following browsers:

1.     Internet Explorer 5.55
2.     Internet Explorer 6
3.     Internet Explorer 7
4.     Mozilla Firefox 19.0
5.     Opera 9
6.     Safari 4
7.     Google Chrome

Friday 1 March 2013

Dynamically add HTML (div / table / all HTML ) element using ASP.Net and C#

Solutions 1:  

HtmlGenericControl mydiv=new System.Web.UI.HtmlControls.HtmlGenericControl("div");
mydiv.Style.Add(HtmlTextWriterStyle.MarginLeft,"360px"); 

How do I get current index in “for each” loop

Solutions 1:  
foreach (DataRow row in dtGroups.Rows)
{
    string grpTitle = row["MainCategory"].ToString();
    int  id = dtGroups.Rows.IndexOf(row);   // (row no)
}