I have used ajax control in asp.net c# application
<div class="uploadfiles">
<asp:ScriptManager ID="ScriptManager1" runat="server"/>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:FileUpload ID="file_upload" class="multi" runat="server" />
<br />
<asp:Button ID="btnUpload" runat="server" Text="Upload" onclick="btnUpload_Click"/>
<asp:Label ID="lblMessage" runat="server" />
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnUpload" />
</Triggers>
</asp:UpdatePanel>
in .cs file
protected void btnUpload_Click(object sender, EventArgs e)
{
HttpFileCollection fileCollection = Request.Files;
for (int i = 0; i < fileCollection.Count; i++)
{
HttpPostedFile uploadfile = fileCollection[i];
string fileName = Path.GetFileName(uploadfile.FileName);
if (uploadfile.ContentLength > 0)
{
uploadfile.SaveAs(Server.MapPath("~/UploadFiles/") + fileName);
lblMessage.Text += fileName + "Saved Successfully<br>";
HyperLink dynamHyperLink = new HyperLink();
dynamHyperLink.Text = fileName.ToString() + "Saved Successfully<br>";
// hyperlnk.Attributes.Add("href", Server.MapPath("/UploadFiles/") + fileName);
dynamHyperLink.NavigateUrl = "~/UploadFiles/" + fileName;
Panel1.Controls.Add(dynamHyperLink);
//lblMessage.Text= ""+fileName+"";
}
}
}
When I Click on upload button it's not executing the btnupload_Click() event and doesn't display uploaded files in Panel1 control.
Please help me!!!
use <asp:PostBackTrigger ControlID="btnUpload" /> instead of AsyncPostBackTrigger on your code.
Take FileUpload
<asp:FileUpload ID="file_upload" class="multi" runat="server" />
outside the update panel and add EventName parameter to this
<asp:AsyncPostBackTrigger ControlID="btnUpload" />
like this
<asp:AsyncPostBackTrigger ControlID="btnUpload" EventName="Click" />
Related
I have a form where users upload their picture to the registration form.. everything worked fine until i added an updatepanel and an Image control.
I placed an update panel, inside it i place a table with Fileupload control and an image control. once the button is clicked the image is uploaded to the images folder and the preview is displayed in image control.
Before making the modification it worked fine.. but now im unable to read the FileUpload1.Filename .. below is the code
Reg.aspx
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table> <tr>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload1" runat="server" onclick="btnUpload1_Click"
Text="Upload" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</td>
</tr>
<tr>
<Td>
<asp:Image ID="Image1" runat="server" Width="147px" /></td></tr>
</table>
Reg.aspx.cs
protected void btnUpload1_Click(object sender, EventArgs e)
{
Label1.Text = FileUpload1.FileName;
try
{
var folder = Server.MapPath("~/UploadImages/");
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
Label1.Text = Label1.Text + " Directory Created <br>";
}
var picUrl = Server.MapPath("~/UploadImages/"+ FileUpload1.FileName.ToString());
if (CheckFileName(picUrl) == true)
{
FileUpload1.SaveAs(picUrl);
Image1.ImageUrl = picUrl.ToString();
Label1.Text = Label1.Text + "Image Uploaded";
}
else
Label1.Text = Label1.Text +"File already exists";
}
catch (IOException ioex)
{
Console.WriteLine(ioex.Message);
}
}
private bool CheckFileName(string picUrl) {
if (File.Exists(picUrl))
return false;
return (true);
}
Now im not able to read the filename from fileupload1 control..
Please help
Add Postback Trigger in your update panel. Have a look at below code sample
ASPX:
<asp:UpdatePanel runat="server" ID="updatePanel1">
<ContentTemplate>
<asp:FileUpload runat="server" ID="fileUploader"/>
<asp:Button runat="server" ID="buttonSubmit" OnClick="buttonSubmit_OnClick" Text="Submit"/>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="buttonSubmit"/>
</Triggers>
</asp:UpdatePanel>
ASPX.cs
protected void buttonSubmit_OnClick(object sender, EventArgs e)
{
var fileName = this.fileUploader.FileName;
}
I need partial render with ajax; I don't know what is wrong. ¿What is the problem?
My code:
ascx
<div id="temasatratar" onclick="__doPostBack('UpdatePanel1', '');"><h1>Temas a tratar</h1></div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" OnLoad="UpdatePanel1_Load">
<ContentTemplate>
<asp:Label runat="server" ID="Label1" />
</ContentTemplate>
</asp:UpdatePanel>
ascx.cs
protected void UpdatePanel1_Load(object sender, EventArgs e)
{
Random rnd = new Random();
int number = rnd.Next(0, 99999);
Label1.Text = "Best "+number;
}
Any suggest?
My application: Sharepoint - Visual web part / C# / Asp.Net / Visual Studio
I would use a fake-button that is invisible as trigger for the UpdatePanel:
<div id="temasatratar" onclick="__doPostBack('fakeButton', '');"><h1>Temas a tratar</h1></div>
<asp:LinkButton ID="fakeButton" style="display:none" runat="server" Text="foo" OnClick="fakeButton_Click" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" OnLoad="UpdatePanel1_Load">
<ContentTemplate>
<asp:Label runat="server" ID="Label1" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="fakeButton" />
</Triggers>
</asp:UpdatePanel>
Now this click-event is handled in an async postback when the user clicks on the div.
protected void fakeButton_Click(Object sender, EventArgs e)
{
// you are in an asynchronous postback now
}
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>
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
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'