Archive for March, 2013

FileUpload1.HasFile is always returning false with updatepanel

 

Html Code
<asp:UpdatePanel ID=”fileUpload” runat=”server”>
<ContentTemplate>
<asp:FileUpload ID=”FileUpload1″ runat=”server” />
<asp:Button ID=”btnUploadFile” Text=”Upload File” runat=”server”
onclick=”btnUploadFile_Click” />
<asp:RegularExpressionValidator ID=”revImage” ControlToValidate=”FileUpload1″ ValidationExpression=”^.*\.((j|J)(p|P)(e|E)?(g|G)|(g|G)(i|I)(f|F)|(p|P)(n|N)(g|G))$” Text=”Invalid image type” runat=”server” />
</ContentTemplate>
</asp:UpdatePanel>

Code Behind Code
if (FileUpload1.HasFile)
{
try
{
FileUpload1.SaveAs(Server.MapPath(“~/Images/” + System.IO.Path.GetExtension(FileUpload1.FileName)));
}
catch (Exception Ex)
{
String he = Ex.Message;
}
}
ASP.NET’s “AJAX” thing doesn’t support file uploads in UpdatePanels out of the box. Change that trigger into a PostBackTrigger (which causes a full page load) or use something else to upload the file.

Solution :

Add a trigger for your UpdatePanel
<Triggers>
<asp:PostBackTrigger ControlID=”btnUploadFile” />
</Triggers>

 

 

 

To Find Control in GridView on RowCommand event in asp.net

Method : 1
protected void gvsearch_RowCommand(object sender, GridViewCommandEventArgs e)
{
 try
 {
 if (e.CommandName == "SRCSELREC")
 {
GridViewRow gvRow = (GridViewRow)((Control)e.CommandSource).NamingContainer;
 Int32 rowind = gvRow.RowIndex;
DropDownList ddlState =
 (DropDownList)gvRow.FindControl("ControlNameHere");
 }
}
catch (Exception ex)
 {
 General.MessageBox(this.Page, "Error at Gridview Row Command : " + ex.Message.ToString());
 return;
 }
}
================================================================
Method : 2

Html code

<asp:TemplateField HeaderText="Action3" Visible="false">
 <ItemStyle HorizontalAlign="Center"></ItemStyle>
 <ItemTemplate>
<asp:LinkButton ID="lnkretqty" runat="server" Text="Return Qty" CommandName="RETQTY" ToolTip="Click here to Add Return Qty Entry"
 CommandArgument='<%# Container.DataItemIndex %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Code Behind code

protected void gvsearch_RowCommand(object sender, GridViewCommandEventArgs e)
 {
 try
 {
 if (e.CommandName == "SRCSELREC")
 {
Int32 rowind = Convert.ToInt32(e.CommandArgument.ToString());
string val = ((Label)gvgpitemdtl.Rows[rowind].FindControl("d")).Text.ToString();


 }
}
catch (Exception ex)
 {
 General.MessageBox(this.Page, "Error at Gridview Row Command : " + ex.Message.ToString());
 return;
 }
}
 

How to access control inside user control for validation in html

Use below code :

user control id on page : control name

Example :

Control on page :

<uc1:UCDateTime ID=”ucgpdt” runat=”server” _SetEnable=”false” />

Validate  txtdate textbox which is inside UCDateTime user control (in html)

Syntax : ucgpdt:txtdate 

 

<asp:RequiredFieldValidator ID=”rfvUCretexpdt” runat=”server” ErrorMessage=”Select Expected date of Return”
Display=”None” SetFocusOnError=”true” ControlToValidate=ucgpdt:txtdate
ValidationGroup=”GPHEADER”></asp:RequiredFieldValidator>

Sys.WebForms.PageRequestManager ParserErrorException: during reportdocument.ExportToHttpResponse in asp.net c#

Error : Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed during reportdocument.ExportToHttpResponse in asp.net c#
Solution : The AJAX update panel requires a return that is un-filtered by any HttpModule or HttpHandler. Since the response objet uses an HttpHandler to handle the HttpResponse, the updatepanel will fire the error as a security precaution.
Solution :
There are two solution I am writing here.

1) Add postback trigger in your update panel with control id.

</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID=”PostbackButtonNameHere” />
</Triggers>
2) on your report export page, add below code.

((ScriptManager)Master.FindControl(“ScriptManager1”)).RegisterPostBackControl(PostbackButtonNameHere);