asp.net file upload inside formview always return false (not in updatepanel) - asp.net

I am using fileupload control inside formview edittemplate
<asp:FileUpload ID="fileup_profilfoto" runat="server" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Sadece şu formatlar (.jpg, .bmp, .png, .gif)" ValidationExpression="^.*\.(jpg|JPG|png|PNG|bmp|BMP|gif|GIF)$" ControlToValidate="fileup_profilfoto" ForeColor="#00C0CC"></asp:RegularExpressionValidator>
It was working.But I added an updatepanel then it didnt work,and then I remowed update panel.But it's still return false (hasfile)
protected void frmviewProfil_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
try
{
FileUpload fileup_profilfoto = (FileUpload)frmviewProfil.FindControl("fileup_profilfoto");
if (fileup_profilfoto.HasFile)
{
//do something
}
else
{
//do something
}
}
}
always goes else scopes.

hi use triggers to achive that
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="conditional">
<Triggers>
<asp:PostBackTrigger ControlID="Button1" />
</Triggers>
<ContentTemplate>
<asp:Label ID="Label1" runat="server"></asp:Label><br /><br />
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" />
</ContentTemplate>
</asp:UpdatePanel>
code behind
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
Label1.Text = FileUpload1.FileName;
}
}

Did you do anything to the properties of the fileupload control, for example setting the autopost back value to false? Try setting this to true if it is false.

I came across this question when I had this problem and figured I'd post what my problem and solution were as well.
Make sure the file you are trying to upload is larger than 0 bytes. I was trying to upload some blank text files for testing and each file had the FileName property set correctly, but HasFile was always false. Adding some text to the files gave it some content and the file was able to be uploaded successfully.

Related

Image URL not changing in aspx page

I have got the following code snippet in my page
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="testButton" />
</Triggers>
<ContentTemplate>
<asp:Image ID="testImage" runat="server" ImageUrl="~/triangle.png" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button runat="server" ID="testButton" Text="Change Image" OnClick="testButton_Click" />
and have got the following in my code behind
protected void testButton_Click ( object sender , EventArgs e ) {
testImage.ImageUrl = "";
}
When I click on the button, the image is vanished since its URL has been set to empty string in code behind - I understand that. But when I view the page source, I see the original image URL as initialized in the asp page where I badly need URL to be set to empty string.
I have changed the ImageUrl value of your asp image in your markup as below:
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="testButton" />
</Triggers>
<ContentTemplate>
<asp:Image ID="testImage" runat="server" ImageUrl="<%#changeableImageUrl %>" />
</ContentTemplate>
Code Behind:
public string changeableImageUrl;
protected void Page_Load ( object sender , EventArgs e ) {
changeableImageUrl= "~/triangle.png"; // initial image URL
this.DataBind (); // since data have been bound in the page by data binding syntax
// <%#changeableImageUrl %>
}
protected void testButton_Click ( object sender , EventArgs e ) {
changeableImageUrl= ""; // to set html-rendered src attribute of img element to ""
this.DataBind(); //bind data to the page
}
changeableImageUrl must be declared public within the page. Now, you can change changeableImageUrl to change the image URL.
This is caused because the browser does not reload all source of page but if you change parameter in ScriptManager, In IDE, (not runtime). If you set to false EnablePartialRendering = false; this may solve your problem and always have new html code.

Asp.net C# - checkbox control won't stay true after code-behind runs

Front HTML
<asp:CheckBox ID="chkSend" runat="server" Text="Send?" />
The code that triggers the code behind
<asp:DropDownList ID="ddlStatus" OnSelectedIndexChanged="ddlStatus_SelectedIndexChanged" AutoPostBack="true"
runat="server" DataValueField="Id" DataTextField="Status" />
The code that runs in the code behind
protected void ddlStatus_SelectedIndexChanged(object s, EventArgs e)
{
this.chkSend.Checked = True;
}
When get back to the front end , the checkbox isn't checked. Any Ideas?
If it helps, this particular view is using Multi-views to which I'm new to.

DotNetNuke CustomValidator strange behavior

I have a strange problem with a dotnetnuke module I'm developing.
I want to use a asp custom validator to validate some input. To keep it simple I'll check only if the field was not empty and at least a few characters long. (I know there are other standard validators that I can use).
The problem is that my code works ok locally (development), but not on production.
The only difference I know is that I use DNN 6 instead of DNN 5.
No matter what I type in on production site, it always shows me the validation error message.
These are the relevant parts of the webpage:
ASCX:
<div>
<asp:UpdatePanel ID="UpdatePanelValidationSummaryHome" ChildrenAsTriggers="False" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:ValidationSummary ID="ValidationSummary1" CssClass="validationSummary" runat="server"
EnableClientScript="False" ShowSummary="true" ShowMessageBox="false" />
<asp:CustomValidator ID="CustomValidatorActiveTab" runat="server" Display="None"
ErrorMessage="Error the field ... was not correct..." OnServerValidate="CustomValidatorActiveTab_ServerValidate"></asp:CustomValidator>
<asp:Button ID="btnZoeken" CssClass="btnZoeken" CausesValidation="true" runat="server" Text="<%$ Resources:GLOBAL, btnZoeken %>"
OnClick="btnZoeken_Click" />
Code behind
private bool ValidateTab_Ondernemingsnummer()
{
if (!String.IsNullOrEmpty(txtOndernemingsnummer.Text) && txtOndernemingsnummer.Text.Length >= 3)
{
return true;
}
return false;
}
protected void CustomValidatorActiveTab_ServerValidate(object source, ServerValidateEventArgs e)
{
int activeTab = GetActiveIndexAccordion();
switch (activeTab)
{
//Zoeken op ondernemingsnummer
case 0:
if (!ValidateTab_Ondernemingsnummer())
{
e.IsValid = false;
}
else
{
e.IsValid = true;
}
break;
}
Thanks for any help.
Any ideas?
SOLVED:
I used dotnetnuke logging to see when and why e.isValid is set to false.
My custom validator control was being called twice!!
The first time it was validated ok, the second time it was not.
My solution was to disable custom server validator control in the markup and enable it just after doing the submit (and don't forget to turn it off).
Like this:
<asp:UpdatePanel ID="UpdatePanelValidationSummaryHome" ChildrenAsTriggers="False"
UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:ValidationSummary ID="ValidationSummary1" CssClass="validationSummary" runat="server"
EnableClientScript="False" ShowSummary="true" ShowMessageBox="false" />
<asp:CustomValidator ID="CustomValidatorActiveTab" runat="server" Display="None"
EnableClientScript="false" Enabled="false" ErrorMessage="ERROR ONDERNEMINGSNUMMER"
OnServerValidate="CustomValidatorActiveTab_ServerValidate"></asp:CustomValidator>
Enabled = false is important here!
Then in the button click
protected void btnZoeken_Click(object sender, EventArgs e)
{
CustomValidatorActiveTab.Enabled = true;
CustomValidatorActiveTab.Validate();
if (Page.IsValid)
{
CustomValidatorActiveTab.Enabled = false;
I still don't know why the CustomValidatorActiveTab_ServerValidate was being called twice.
It has something to do with DNN 5 I suppose (and maybe it was fixed in DNN 6).
I hope this helped someone.
The reason the validator is called twice is due to the link button.
Generating the GridView onBubbleEvent, which also causes the validator to validate.

Devexpress ASPxGridViewExporter not working

I have the following code but when I press the export button all I get is a download popup for the asp.net page, this case default.aspx
<dx:ASPxButton ID="btnPdfExport" runat="server" Text="Export to PDF" UseSubmitBehavior="False"
OnClick="btnPdfExport_Click" />
And the grid and exporter
<dx:ASPxGridViewExporter ID="gridExport" GridViewID="grid" runat="server">
</dx:ASPxGridViewExporter>
<dx:ASPxGridView ID="grid" runat="server"
CssFilePath="~/App_Themes/Office2010Black/{0}/styles.css" OnAutoFilterCellEditorCreate="grid_AutoFilterCellEditorCreate"
OnAutoFilterCellEditorInitialize="grid_AutoFilterCellEditorInitialize" KeyFieldName="ProductCode" OnProcessColumnAutoFilter="grid_ProcessColumnAutoFilter"
CssPostfix="Office2010Black" DataSourceID="SqlDataSource1" Font-Size="Small">
The code behind
protected void btnPdfExport_Click(object sender, EventArgs e)
{
gridExport.Landscape = true;
gridExport.WritePdfToResponse("view");
}
The strange thing is that this exact code works perfectly on another page, when I press the button I get view.pdf, any ideas
James
I tried with your code.It is working.There is no any error or problem.
Try this:
<asp:UpdatePanel runat="server">
<Triggers>
<asp:PostBackTrigger ControlID="btnPdfExport" />
</Triggers>
</asp:UpdatePanel>

How to update a control outside of an updatepanel?

I am going to show some text in a TextBox, which is located outside of an updatepanel, after checking a CheckBox but I cannot make it work. please help me out ?
Here is my code:
<asp:UpdatePanel runat="server" ID="uplMaster">
<ContentTemplate>
<asp:CheckBox ID="cbShowText" runat="server" Text="Show Some Text" AutoPostBack="true"
OnCheckedChanged="cbShowText_CheckedChanged" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:TextBox ID="txtBox" Text="Empty" runat="server" />
Code Behind:
protected void cbShowText_CheckedChanged(object sender, EventArgs e)
{
txtBox.Text = "Some Text";
}
Thanks in advance :D
P.S. As you might have guessed, I have resembled my problem and that is why I don't want to put the TextBox in the UpdatePanel
I put the TextBox in another UpdatePanel and then called the Update method:
Here is my new code:
<asp:UpdatePanel runat="server" ID="uplMaster" UpdateMode="Always">
<ContentTemplate>
<asp:CheckBox ID="cbShowText" runat="server" Text="Show Some Text" AutoPostBack="true"
OnCheckedChanged="cbShowText_CheckedChanged" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel runat="server" ID="uplDetail" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="txtBox" Text="Empty" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
Code Behind:
protected void cbShowText_CheckedChanged(object sender, EventArgs e)
{
txtBox.Text = "Some Text";
uplDetail.Update();
}
Hope this helps
The textbox has to be in update panel also.
*Edit:
I am sorry I didn't read your question properly. Perhaps write a javascript function, and call the function from codebehind?
I know its been a while since this was asked, but here is what I did. Like #bla said write a javascript function and call it from code behind.
So in your checked changed call this. The changeText is a javascript function on your page in the header or in a script file.
protected void cbShowText_CheckedChanged(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(), "Show Different Text", "changeText();", true);
}
Sample Javascript. Just gets called when checked changed event fires from code behind.
<script type="text/javascript">
function changeText() {
var txt= document.getElementById('<%= txtBox.ClientID %>');
var chk = document.getElementById('<%= cbShowText.ClientID %>');
if (chk.checked === true) {
txt.Text = "Something";
} else {
txt.Text = "Somethingelse";
}
}
</script>
Hope this helps someone.

Resources