Avoid execution of code while you refresh the page

Posted by Venkat | Labels:

Hi , here we have to discuss the how to avoid the execution of code While you refresh the page.. suppose if you have written the code for sending email to users.. after clicking the button the code has been executed suppose if you , Refresh the same page once again the Email sent to the user so here we have to avoid

the execution on code on page_Refresh
this is the code in VB.NET and C#

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not IsPostBack Then
Session("CheckRefresh") = Server.UrlDecode(System.DateTime.Now.ToString())
End If
End Sub


Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
If Session("CheckRefresh").ToString() = ViewState("CheckRefresh").ToString() Then
Label1.Text = "Hello"
Session("CheckRefresh") = Server.UrlDecode(System.DateTime.Now.ToString())
Else
Label1.Text = "Page Refreshed"
End If

End Sub

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As EventArgs)
ViewState("CheckRefresh") = Session("CheckRefresh")
End Sub


Here in this code i m saving system date and time in session varaible when the page gets load
and in the page prerender event , which occurs after page_Load event , i am
assigning the value if session variable to viewstate varaible

Now in Button_Click Event i m checking if both the values in session
variable and in ViewState varaible are same than page in not refreshed
if they are not same that means page has got refreshed

This is code for C#.NET


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["CheckRefresh"] = Server.UrlDecode(System.DateTime.Now.ToString());
}
}


protected void Button1_Click(object sender, EventArgs e)
{
if (Session["CheckRefresh"].ToString() == ViewState["CheckRefresh"].ToString())
{
Label1.Text = "Hello";
Session["CheckRefresh"] = Server.UrlDecode(System.DateTime.Now.ToString());
}
else
{
Label1.Text = "Page Refreshed";
}

}

protected void Page_PreRender(object sender, EventArgs e)
{
ViewState["CheckRefresh"] = Session["CheckRefresh"];
}

Upload Large files using asp.net

Posted by Venkat | Labels:

To upload large files in asp.net you have to increase the MaxrequestLength and Executiontimeout of the httpruntime settings of your web.config file.By Default it will upload 4MB

suppose if i want to upload upto 8 MB you have to include this tag in you web.config file , don't change anything in your machine.config files.

httpruntime  maxRequestLength="1048576" executionTimeout="450"


here 1048576 - 1 GB

Validation for FileUploadControl

Posted by Venkat | Labels: ,

To validate the fileupload control use Custom Validator and call this function on ClientValidateFunction property of Custom validator.

You can pass the file extension , Watever you want to allow..

function ValidateSFrontImageUpload(Source, args)
{
var fuData = document.getElementById('<%= FrontSImage_FileUpload.ClientID %>');
var FileUploadPath = fuData.value;

if(FileUploadPath =='')
{
// There is no file selected
args.IsValid = false;
}
else
{
var Extension = FileUploadPath.substring(FileUploadPath.lastIndexOf('.') + 1).toLowerCase();

if (Extension == "jpg" || Extension == "jpeg" || Extension == "png" || Extension == "gif")
{
args.IsValid = true; // Valid file type
}
else
{
args.IsValid = false; // Not valid file type
}
}
}

Code for Logout button

Posted by Venkat | Labels:

If you are not using master pages then you have to paste this code of all the pages (on load event).


Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();

On logout

You have to write

Session.Abandon();
Session.Clear();

Use If condition inside the Gridiview or Datalist

Posted by Venkat | Labels:

Here the Html Code , html was not allowed here so i have conver < - &lt and > - &gt


&lt asp
:HyperLink ID="HyperLink1" Visible=" &lt %# isFilePathEmpty(Eval("filepath")) % &gt " NavigateUrl="~/AddPost.aspx?ForumID=18#" runat="server" &gt
&lt %# Eval("PartNumber") &gt
&lt /asp:HyperLink &gt

then create a method called isFilePathEmpty(string filePath) in the code behind which returns a boolean.

Validation of ViewSate MAC Failed

Posted by Venkat | Labels:

If you are getting this error .

Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster

Write the below code..

Update your web.config with following attributes.

pages validateRequest="false" enableEventValidation="false" viewStateEncryptionMode ="Never" enableViewStateMac="false"

Setting default button by using panel

Posted by Venkat | Labels: ,

Here We have to discuss about how to set the default button in Masterpage
for ex: in Master i have Search_textbox and Search_button when the user enter some text in the Textbox and pres Enter so the Search_button should trigger..

Html Code

<asp:Panel ID="SearchStyle_Panel" runat="server" width="100%">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
                        <tr>
                              <td width="188" align="right" valign="middle" class="white_text">
                                   SEARCH
                              </td>
                               <td width="235" align="right" valign="middle">
                                   <asp:TextBox ID="search_textbox" runat="server" value="ENTER KEYWORD" onfocus="if (this.value == 'ENTER KEYWORD') {this.value = '';}"
                                      onblur="if (this.value == '') {this.value = 'ENTER KEYWORD';}" CssClass="textbox"></asp:TextBox>
                               </td>
                             <td width="5" align="right" valign="middle">
                          </td>
                           <td width="40" align="right" valign="bottom">
                                 <asp:Button ID="search_button" runat="server" Text="GO" CssClass="go_btn"/>
                              </td>
                           </tr>
                      </table>
                     </asp:Panel>

since the button is nested inside of a control that implememts INamingContainer, its UniqueId will not match its server ID

for that reason, you should set the DefaultButton property in code.

myPanel.DefaultButton = myButton.UniqueId

then on page_load

SearchStyle_Panel.DefaultButton = search_button.UniqueID

Publish a Web project

Posted by Venkat | Labels:

Here we are going to see how to publish a web site project . While doing so , you can convert to assemblies, also on you published website folder it contains only .aspx file you cant able to view the code behind file (.aspx.vb , .ascx.vb , .vb , .cs files)

This is for the Security purpose no one able to modify you code..

these are the steps :
  1. On the Build menu, click Publish Web Site.
  2. In the Publish Web Site dialog box, click the ellipsis button (…) to browse to the location to which you want to publish the Web site.
    You can write the Web site output to a local or shared folder, to an FTP site, or to a Web site that you access with a URL. You must have Create and Write permissions in the target location.
  3. To be able to change the layout (but not the code) of .aspx files after publishing the Web site, select the Allow this precompiled site to be updateable check box.
  4. To name strongly named assemblies using a key file or a key container, select the Enable strong naming on precompiled assemblies check box, and then click OK.
    Publishing status is displayed in the taskbar. Depending on the connection speed, the size of the site and the types of content files, publishing time can vary. When publishing is completed, the status of Publish succeeded is displayed.
  5. Make any configuration changes that are necessary for your site. For more information, see How to: Configure Published Web Sites. You might also want to encrypt specific configuration settings. For more information, see Encrypting Configuration Information Using Protected Configuration.
Ref : 
http://msdn.microsoft.com/en-us/library/20yh9f1b(VS.80).aspx
http://msdn.microsoft.com/en-us/library/1y1404zt(VS.80).aspx
http://msdn.microsoft.com/en-us/library/aa983453(VS.80).aspx

Get FreeCaptcha control with installation steps

Posted by Venkat | Labels: ,

Here you can get the Free Captcha Control...

Ref:
http://www.mondor.org/captcha.aspx

pass the imagename to ImageUrl

Posted by Venkat | Labels: ,

Hi generally we are binding image to datalist..

so suppose if we store the image in the project folder

we have to get the name of the file and pass it on the src attribute of

img tag.. Ex:

<img src='< %#"BigImages/" & Eval("imagename") % > ' width ="485px" height="613px"  border="0" />
here bigimages is the foldername and imagename that we will get it from database..

so it will run properly..

Suppose if we do some task ie: hiding images - for that we have to

write the code in Datalist_ItemDatabound and also same for Gridview.. event has

been changed ie:ItemCommand..

At that if we use runat="server" id="Image1" for the above tag , while we get the

image through code it shows error

Unable to cast the html image to webcontrol image

for that use asp:image this is way you have to apply imageurl so it display the

image as per imagename - from the folder

<asp:Image ID="ProductImg"
CssClass="ProductImg"
runat="server"
ImageUrl=' < %# Eval("BigImages","imagename/{0}").Trim() % >'
AlternateText=' < %# Eval("imagename","").Trim() % >' 


PayOffers.in