FileUpload.HasFile give always false - c#

this is my code where my FileUpload control is outside of update panel but when I click on save button which is under update panel give fileUploadAttachment.HasFile = false
ASPX
<asp:Literal runat="server" ID="lblAttachment" Text="Attachment:" /><asp:FileUpload
ID="fileUploadAttachment" runat="server" Width="488px" />
<asp:UpdatePanel ID="updatePanelAction" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" ValidationGroup="Save" />
<asp:Button ID="btnTest" runat="server" Text="Test" Enabled="false" OnClick="btnTest_Click" />
<asp:Button ID="btnConfirmTest" runat="server" Text="Confirm Test" Enabled="false"
OnClick="btnConfirmTest_Click" />
<asp:Button ID="btnSend" runat="server" Text="Send" Enabled="false" OnClick="btnSend_Click" />
</ContentTemplate>
</asp:UpdatePanel>
CS
protected void btnSave_Click(object sender, EventArgs e)
{
CampaignBAL campaignBAL;
string tmpFileName = "";
User user;
Campaign campaignDetail = new Campaign();
int? campaignID;
if (fileUploadAttachment.HasFile) // return always false
{
tmpFileName = string.Format("{0}\\{1}{2}", Server.MapPath("TempUpload"), Guid.NewGuid(), Path.GetExtension(fileUploadAttachment.PostedFile.FileName));
fileUploadAttachment.PostedFile.SaveAs(tmpFileName);
}
}
please help me how can I fix it

You'll need to add postback triggers for controls that post within the UpdatePanel:
<asp:UpdatePanel ...>
<Triggers>
<asp:PostBackTrigger ControlID="btnSend" />
</Triggers>
...
</asp:UpdatePanel>

You Can Change Your Code in the ASP Page Like This
<asp:updatePanel>
<trigger>
<asp:PostBackTrigger ControlID="btnSend">
</trigger>
<\asp:updatePanel>

Related

Listbox OnSelectedIndexChanged doesn't work

I have a listbox inside my jquery dialog,
<div id="dialog">
<asp:UpdatePanel ID="updatePanel1" runat="server">
<ContentTemplate>
<asp:ListBox ID="lbxTitle" runat="server" style="width:400px;height:100px;" OnSelectedIndexChanged="lbxTitle_SelectedIndexChanged">
<asp:ListItem value="2" Text="abc" />
<asp:ListItem value="3" Text="def" />
</asp:ListBox>
<br />
<fieldset>
<asp:Label ID="lblContent" runat="server" style="height:200px;" />
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
</div>
And in the behind code, I have this function
protected void lbxTitle_SelectedIndexChanged(object sender, EventArgs e)
{
lblContent.Text = "test";
}
I dont put autopostback="true" because it will refresh the whole page, and close my dialog.
How to make it so that when user click item on the listbox, it will show "test" on the label inside dialog.
Now, it doesnt do anything if I change the selected item.
You should put the Autopostback then you can use AsyncPostBack to prevent whole page postback...
As per the following Code
<asp:UpdatePanel ID="updatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:ListBox ID="lbxTitle" runat="server" style="width:400px;height:100px;" OnSelectedIndexChanged="lbxTitle_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem value="2" Text="abc" />
<asp:ListItem value="3" Text="def" />
</asp:ListBox>
<br />
<fieldset><asp:Label ID="lblContent" runat="server" style="height:200px;" />
</fieldset>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="lbxTitle" />
</Triggers>
</asp:UpdatePanel>
Add AutoPostback and after you create your dialog move it back to the form:
$("#dialog").parent().appendTo($("form:first"));
Try this:
<script type="text/javascript">
$(function() {
<% if (Page.IsPostback) { %>
$('id of your listbox').change();
<% } %>
});
</script>

how use PopupControlExtender with conditions

i have an asp.net Button, i want when a user click on that button:
if there is any Session["id"] for that user, user will be redirect to
another page
2.if not show a PopupControlExtender and show some link
to user
we cant use PopupControlExtender in code behind how i should check this condition ?
thx
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" />
<br />
<br />
<asp:Panel ID="Panel1" runat="server" BackColor="#9933FF" BorderColor="#6666FF"
Height="132px" Width="329px">
<asp:Button ID="Button2" runat="server" Text="Button" />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</asp:Panel>
<asp:PopupControlExtender ID="PopupControlExtender1" runat="server"
TargetControlID="Button1" PopupControlID="Panel1"
>
</asp:PopupControlExtender>
</ContentTemplate>
</asp:UpdatePanel>
What you can do is conditionally register some javascript for opening the Popup when the button is clicked.
Lets say you define your popup like this:
<ajax:PopupControlExtender ID="popup" runat="server"
TargetControlID="textbox"
BehaviorID="mybehavior"
PopupControlID="panel"
Position="Bottom" />
Then, on the button click event:
if(Session["id"] == null)
{
var script = #"Sys.Application.add_load(function() { $find('mybehavior').showPopup(); });";
ScriptManager.RegisterStartupScript(this, GetType(), "ShowPopup", script, true);
}
else
{
//Redirect;
}

Autopostback is working only for the first time in ASP.NET?

Here i am using a radio button to calculate the date difference between two textboxes and i am showing it in another textbox.It is working only for the first time when i click the radio button after that it is not working..Here is my code
<asp:RadioButton ID="rdoSpecifiedDates" runat="server" class="bodycontent" GroupName="status"/>
<asp:RadioButton ID="rdoDateRange" runat="server" class="bodycontent"
GroupName="status" oncheckedchanged="rdoDateRange_CheckedChanged" AutoPostBack="true"
/>
<asp:UpdatePanel ID="Update" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtDays" runat="server" CssClass="bodycontent" MaxLength="6" ReadOnly="true"></asp:TextBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="rdoDateRange" />
</Triggers>
</asp:UpdatePanel>
and
protected void rdoDateRange_CheckedChanged(object sender, EventArgs e)
{
DateTime startdate=Convert.ToDateTime(txtOStartDate.Text);
DateTime enddate=Convert.ToDateTime(txtOEndDate.Text);
var result = (enddate - startdate).TotalDays;
txtDays.Text =Convert.ToString( result);
}
Any suggestion?
its happening because you are forcing only post back on rdoDateRange...and when the other rdoSpecifiedDates is clicked no postback occur so that's why you rdoDateRange dose not reflect any change..
So make the rdoSpecifiedDates AutoPostBack = true.
hmm...either you have to put both rdobuttons in your trigger.. like this
<asp:RadioButton ID="rdoSpecifiedDates" runat="server" AutoPostBack="true" class="bodycontent" GroupName="status" />
<asp:RadioButton ID="rdoDateRange" runat="server" class="bodycontent" GroupName="status"
OnCheckedChanged="rdoDateRange_CheckedChanged" AutoPostBack="true" />
<asp:UpdatePanel ID="Update" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtDays" runat="server" CssClass="bodycontent" MaxLength="6" ReadOnly="true"></asp:TextBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="rdoDateRange" />
<asp:AsyncPostBackTrigger ControlID="rdoSpecifiedDates" />
</Triggers>
</asp:UpdatePanel>
OR
Put both the radio buttons in update pannel like this..
<asp:UpdatePanel ID="Update" runat="server">
<ContentTemplate>
<asp:RadioButton ID="rdoSpecifiedDates" runat="server" AutoPostBack="true" class="bodycontent"
GroupName="status" />
<asp:RadioButton ID="rdoDateRange" runat="server" class="bodycontent" GroupName="status"
OnCheckedChanged="rdoDateRange_CheckedChanged" AutoPostBack="true" />
<asp:TextBox ID="txtDays" runat="server" CssClass="bodycontent" MaxLength="6" ReadOnly="true"></asp:TextBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="rdoDateRange" />
</Triggers>
</asp:UpdatePanel>
You Also can do..:
<div>
<asp:RadioButton ID="rdoSpecifiedDates" runat="server" class="bodycontent"
GroupName="status" oncheckedchanged="rdoSpecifiedDates_CheckedChanged" AutoPostBack="true" />
<asp:RadioButton ID="rdoDateRange" runat="server" class="bodycontent" GroupName="status"
OnCheckedChanged="rdoDateRange_CheckedChanged" AutoPostBack="true" />
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="Update1" runat="server" UpdateMode="Conditional" >
<ContentTemplate>
<asp:TextBox ID="txtDays" runat="server" CssClass="bodycontent" MaxLength="6" Enable="False"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
</div>
And
protected void rdoDateRange_CheckedChanged(object sender, EventArgs e)
{
DateTime startdate = DateTime.Now.AddHours(2);
DateTime enddate = DateTime.Now.AddHours(5);
TimeSpan result = enddate - startdate;
txtDays.Text = result.ToString();
Update1.Update();
}
protected void rdoSpecifiedDates_CheckedChanged(object sender, EventArgs e)
{
}
If your radio buttons are outside of the update panel (and need to be so), the selected/checked item will never fire due to not having any JavaScript on its <input> tag, which is logical in the sense that the page thinks the element is already selected so why would it need to fire off back to the server when it is clicked?
But it obviously causes issues in this scenario because, as triggers for an update panel, it never "repaints" itself to reflect that the selected item has changed.
My workaround for this is to "repaint" the radio buttons as well by having them in their own <asp:UpdatePanel> control:
<asp:UpdatePanel ID="RadioButtonUpdate" runat="server">
<ContentTemplate>
<asp:RadioButton ID="rdoSpecifiedDates" runat="server" class="bodycontent"
GroupName="status" OnCheckedChanged="rdoSpecifiedDates_CheckedChanged" AutoPostBack="true" />
<asp:RadioButton ID="rdoDateRange" runat="server" class="bodycontent"
GroupName="status" OnCheckedChanged="rdoDateRange_CheckedChanged" AutoPostBack="true" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="Update" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtDays" runat="server" CssClass="bodycontent" MaxLength="6" ReadOnly="true"></asp:TextBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="rdoDateRange" />
</Triggers>
</asp:UpdatePanel>
This feels to me like the cleanest solution outside of housing the radio buttons within the update panel you are wanting refreshed.

post back of Dropdownlist in web user control all other fields become empty

i have a aspx page in which i have placeholder inside the panel
as
<div>
<div>
<div>
<div>
<div>
<div>
<asp:UpdatePanel runat="server" ID="UpdatePanelLinks">
<ContentTemplate>
<center>
<asp:Button Text="ADD" ID="btnAdd" runat="server" Width="100" OnClick="btnAdd_Click" />
<asp:Button Text="Edit" ID="btnEdit" runat="server" Width="100" OnClick="btnEdit_Click" />
<asp:Button Text="Delete" ID="btnDelete" runat="server" Width="100" OnClick="btnDelete_Click" /></center>
<asp:LinkButton ID="usercontroldata" runat="server" Text="Branches"
OnClick="usercontrol_Click"></asp:LinkButton>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</div>
<div >
<asp:LinkButton ID="lnkDepartments" runat="server" Text="Departments" OnClick="lnkDepartments_Click"></asp:LinkButton>
</div>
<div >
</div>
<div >
<asp:LinkButton ID="lnkProjects" runat="server" Text="Projects" OnClick="lnkProjects_Click"></asp:LinkButton>
</div>
<div >
</div>
<div >
</div>
</div>
<div>
<div>
<asp:Label Text="" Visible="false" ID="lblmessage" runat="server" />
<div>
<asp:UpdatePanel ID="UpdatePanel" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="lnkBranches" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="lnkDepartments" />
<asp:AsyncPostBackTrigger ControlID="lnkProjects" />
<asp:AsyncPostBackTrigger ControlID="btnAdd" />
<asp:AsyncPostBackTrigger ControlID="btnEdit" />
<asp:AsyncPostBackTrigger ControlID="btnDelete" />
</Triggers>
<ContentTemplate>
<asp:Panel runat="server" ID="MainPanel">
<div class="padding_branch">
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</div>
</div>
</div>
</div>
</div>
<asp:UpdatePanel ID="UpdatePanel" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="lnkBranches" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="lnkDepartments" />
<asp:AsyncPostBackTrigger ControlID="lnkProjects" />
<asp:AsyncPostBackTrigger ControlID="btnAdd" />
<asp:AsyncPostBackTrigger ControlID="btnEdit" />
<asp:AsyncPostBackTrigger ControlID="btnDelete" />
</Triggers>
<ContentTemplate>
<asp:Panel runat="server" ID="MainPanel">
<div >
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
and on the aspx.cs page i am dynamically add the web user control on click of links
and on the add button click the new user control is loaded
protected void usercontrol_Click(object sender, EventArgs e)
{
if (ViewState["controlname"] != null)
{
PlaceHolder pl = MainPanel.FindControl(ViewState["controlname"].ToString()) as PlaceHolder;
if (pl.Controls.Count > 0)
{
pl.Controls.RemoveAt(0);
}
}
Control uc = (Control)Page.LoadControl("~/usercontrol_Data.ascx");
ViewState["path"] = "~/usercontrol_Data.ascx";
ViewState["controlname"] = "PlaceHolder1";
ViewState["name"] = "usercontrol";
PlaceHolder1.Controls.Add(uc);
}
i am also mainaining the web user control on Page_Load
if (Page.IsPostBack)
{
if (ViewState["path"] != null)
{
Control uc1 = (Control)Page.LoadControl(ViewState["path"].ToString());
PlaceHolder pl = MainPanel.FindControl(ViewState["controlname"].ToString()) as PlaceHolder;
pl.Controls.Add(uc1);
}
}
and there is dropdown list on the form user control when iam selecting any of the option from the user control it post back and all the other fields become empty for textboxes etc . this happans for the first time and for next time all the fields retain there values
please help
thanks
i have done this by assigning the same ID to the control every time when the control loads dynamically. this is because when the control added dynamically every time it get the new ID that is creating the problem for me
uc1.ID="Web_User_Control_ID";
every time the control load asign this ID to it will resole the problem
Try changing Page.IsPostBack to !Page.IsPostback in your Page_Load event.
Dynamically added controls are not persisted between post backs; you have to re-add your dynamically added controls on each post back. On each page request, asp.net will parse your aspx file, build the html structure of your page, then fill in any previously filled-in values via ViewState and posted values, then serve it up. That's it. It will never automatically re-add dynamically added content.
So once you run this code
protected void usercontrol_Click(object sender, EventArgs e)
{
//...
Control uc = (Control)Page.LoadControl("~/usercontrol_Data.ascx");
ViewState["path"] = "~/usercontrol_Data.ascx";
ViewState["controlname"] = "PlaceHolder1";
ViewState["name"] = "usercontrol";
PlaceHolder1.Controls.Add(uc);
}
you need to somehow know to re-run it every time the user posts back

ASP.NET: ModalPopupExtender prevents button click event from firing

Here is what I'm trying to do: Click a button on my page, which in turn makes (2) things happen:
Display a ModalPopup to prevent the user from pressing any buttons or changing values
Call my code behind method, hiding the ModalPopup when finished
Here is the ASP markup:
<asp:UpdatePanel ID="UpdatePanel2" runat="server" ChildrenAsTriggers="true"
UpdateMode="Always">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSaveData" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:Panel ID="pnlHidden" runat="server" style="display: none;">
<div>
<h1>Saving...</h1>
</div>
</asp:Panel>
<cc1:ModalPopupExtender ID="modalPopup"
BackgroundCssClass="modalBackground" runat="server"
TargetControlID="btnSaveData" PopupControlID="pnlHidden"
BehaviorID="ShowModal">
</cc1:ModalPopupExtender>
<asp:Button ID="btnSaveData" runat="server" Text="Save Data"
OnClick="btnSaveData_Click" />
</ContentTemplate>
</asp:UpdatePanel>
Now, here is my code behind C# code:
protected void btnSaveData_Click(object sender, EventArgs e)
{
UpdateUserData(GetLoggedInUser());
modalPopup.Enabled = false;
}
Why doesn't this work? The ModalPopup displays perfectly, but the btnSaveData_Click event NEVER fires.
UPDATE: The first suggestion did not work for me. I also tried your second suggestion (insofar as it applied to me). One minor difference on my end is that there is no button on my modal panel (pnlHidden) -- it's just a message. I did try using Javascript events on the client side, which at least did fire concurrently with my server-side event. This was good news, but I can't seem to find or grab a handle on the ModalPopupExtender or its BehaviorID. This doesn't work:
function showOverlay() {
var popup = $find('modalPopup');
popup.show();
}
popup is ALWAYS equal to null.
FINAL UPDATE: This is my final solution for getting this to work (Take specific note of the use of OnClientClick AND OnClick):
<asp:UpdatePanel ID="UpdatePanel2" runat="server" ChildrenAsTriggers="true"
UpdateMode="Always">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSaveData" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:Panel ID="pnlHidden" runat="server" style="display: none;">
<div>
<h1>Saving...</h1>
</div>
</asp:Panel>
<cc1:ModalPopupExtender ID="modalPopup"
BackgroundCssClass="modalBackground" runat="server"
TargetControlID="hdnField" PopupControlID="pnlHidden"
BehaviorID="ShowModal">
<asp:HiddenField ID="hdnField" runat="server" />
</cc1:ModalPopupExtender>
<asp:Button ID="btnSaveData" runat="server" Text="Save Data"
OnClientClick="showModal();" OnClick="btnSaveData_Click" />
</ContentTemplate>
</asp:UpdatePanel>
Using this Javascript function:
function showModal() { $find('ShowModal').show(); }
... And this code behind:
protected void btnSaveData_Click(object sender, EventArgs e)
{
UpdateUserData(GetLoggedInUser());
modalPopup.hide();
}
Try this.
Create a dummy hidden field:
<asp:HiddenField ID="hdnField" runat="server" />
Set the TargetcontrolID = "hdnField" in your Modal Popup declaration.
In your btnSaveData_Click event, do this:
modalPopup.Show();
Try this. It is 100% working
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Btnshow" runat="server" Text="Show" OnClick="Btnshow_Click" />
<asp:Button ID="BtnTarget" runat="server" Text="Target" Style="display: none" />
<asp:TextBox ID="TextBox1" runat="server">
</asp:TextBox>
<input type="button" value="Get" onclick="abc()" />
<asp:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="BtnTarget"
PopupControlID="Panel1">
</asp:ModalPopupExtender>
<asp:Panel ID="Panel1" runat="server" BackColor="Black" Width="300px" Height="300px">
<asp:UpdatePanel ID="UpdatePanel2" runat="server" ChildrenAsTriggers="false" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="BtnHide" runat="server" Text="Hide Button" OnClick="BtnHide_Click" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="BtnHide" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Btnshow" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
Server side code
protected void Btnshow_Click(object sender, EventArgs e)
{
ModalPopupExtender1.Show();
}
protected void BtnHide_Click(object sender, EventArgs e)
{
ModalPopupExtender1.Hide();
}
First attempt: Try to set your ButtonID into OkControlID Tag and try again
OR
Second attempt: Call your event over javascript there seems to be some problems with click events
<div>
<cc1:ModalPopupExtender PopupControlID="Panel1"
ID="ModalPopupExtender1"
runat="server" TargetControlID="LinkButton1" OkControlID="Ok"
OnOkScript="__doPostBack('Ok','')">
</cc1:ModalPopupExtender>
<asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
</div>
<asp:Panel ID="Panel1" runat="server">
<asp:Button ID="Ok" runat="server" Text="Ok" onclick="Ok_Click" />
</asp:Panel>
From this example you can easily control panel show up depends on conditions instead of just immediately show up panel once you click button.
<asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click"/>
<asp:HiddenField ID="hdnField" runat="server" />
<ajaxToolkit:ModalPopupExtender runat="server"
TargetControlID="hdnField"
ID="btnAdd_ModalPopupExtender"
PopupControlID="pnlPrintName">
</ajaxToolkit:ModalPopupExtender>
<asp:Panel ID="pnlPrintName" runat="server">
.
.
</asp:Panel>
//Server side code:
protected void btnAdd_Click(object sender, EventArgs e)
{
if( dt.Rows.Count == 0 )
{
btnAdd_ModalPopupExtender.Show();
}
else
{
add();
}
}
In code behind, you can do this:
if (true)
{
var script = #"Sys.Application.add_load(function() { $find('behavoirIDModal').show(); });";
ScriptManager.RegisterStartupScript(this, GetType(), "Show", script, true);
}
Change this 'behavoirIDModal'

Categories