Asp.net custom ajax file uploader - asp.net

I develop custom file uploader.
<asp:FileUpload runat="server" ID="fuUpload" />
<asp:UpdatePanel runat="server" >
<ContentTemplate>
<asp:Button ID="btnUpload" runat="server" Text="Upload" CausesValidation="false"
OnClick="btnUpload_Click" />
<div>
<asp:Label ID="lblError" runat="server" Visible="false" ForeColor="Red" /></div>
<asp:Repeater ID="rptAttachments" runat="server" OnItemCommand="Uploader_ItemCommand">
<ItemTemplate>
<div>
<a href='<%#GetUrl(....) %>'><%#Eval("Filename") %></a> <b>
<asp:LinkButton ID="lnkDelete" runat="server" Text="Удалить" CommandName="DeleteAttachment" CommandArgument='<%#Eval("FileName") %>' /></b>
</div>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnUpload" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="rptAttachments" EventName="ItemCommand" />
</Triggers>
</asp:UpdatePanel>
protected void btnUpload_Click(object sender, EventArgs e)
{
//fuUpload.HasFile == false
if (fuUpload.HasFile && fuUpload.PostedFile != null
&& fuUpload.PostedFile.ContentLength > 0)
{
}
}
I wonder why fuUpload.HasFile always equls null.

Try these things.
Put <asp:FileUpload runat="server" ID="fuUpload" /> inside UpdatePanel
Set btnUpload trigger like this. <asp:PostBackTrigger ControlID="btnUpload" />

Related

Asp.net UpdateProgressBar Control Not Displaying data dynamically

I have a aspx is like shown below
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
<asp:UpdatePanel runat="server" ID="panel">
<ContentTemplate>
<asp:Button runat="server" id="bt" text="partial postback" OnClick="E" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="Button1" runat="server" text="full postback" OnClick="J" />
<asp:UpdateProgress runat="server" ID="progress" DynamicLayout="true" AssociatedUpdatePanelID="panel">
<ProgressTemplate>
<div class="divWaiting">
<asp:Label ID="lbl" runat="server" Text="OK" />
<asp:Image ID="imgWait" runat="server" ImageAlign="Middle" ImageUrl="images/indicator.gif" />
</div>
</ProgressTemplate>
</asp:UpdateProgress>
On Code Behind I have a function to update my label after making the thread sleep for 3 seconds
public void E(Object sender, EventArgs e)
{
System.Threading.Thread.Sleep(3000);
Label lbl = (Label)progress.FindControl("lbl");
if (lbl.ToString()!= null)
{
lbl.Text = "SomeThing";
}
update = true;
}

Resetting Textbox.Text in UserControl

<%# Control Language="C#" AutoEventWireup="true" CodeBehind="ChatUserControl.ascx.cs"
Inherits="MetamorphismApp.ChatUserControl" %>
<asp:Timer ID="timer1" runat="server" OnTick="timer1_Tick" Interval="5000">
</asp:Timer>
<div id="divChatWindow" class="clChatWindow">
<div>
<asp:Label runat="server" Text='<%# Eval("username") %>' class="divHeader" ID="lblChatFriend"></asp:Label>
<asp:Image ID="imgFriend" runat="server" CssClass="classFriendImage" />
<asp:LinkButton ID="lbClose" runat="server" CommandName="Close" CssClass="lbClose"
OnClick="lbClose_Click">Close</asp:LinkButton></div>
<div class="chatText" id="idChatText" runat="server">
<asp:UpdatePanel ID="UpdatePanel" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="timer1" EventName="Tick" />
<asp:AsyncPostBackTrigger ControlID="btnSendChat" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:Repeater runat="server" ID="rpChatMessages">
<ItemTemplate>
<asp:Image ID="imageForFriend" runat="server" CssClass="clFriendsImage" ImageUrl='<%# "HttpImageHandler.jpg?username=" + DataBinder.Eval(Container.DataItem,"fromusername").ToString() %>' />
<asp:Label ID="chatMessage" runat="server" Text='<%# Eval("Message") %>'></asp:Label>
<br>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<asp:TextBox ID="txtChatMessage" runat="server" Width="142px" CssClass="clChatMessage" EnableViewState="true"
TextMode="MultiLine"></asp:TextBox>
<asp:LinkButton ID="btnSendChat" runat="server" CommandName="Insert" CommandArgument='<%# Eval("username") %>'
OnClick="btnSendChat_Click">Send</asp:LinkButton>
</div>
protected void btnSendChat_Click(object sender, EventArgs e)
{
string username = lblChatFriend.Text;
ucc.InsertMessage(Session["username"].ToString(), username, txtChatMessage.Text);
txtChatMessage.Text = "";
}
txtChatMessage does not become blank when btnSendChat_Click event handler is called
Try this:
txtChatMessage.Text = string.Empty;
to make sure that the text is null or empty

Modalpopup with UpdatePanel

I have an autocompleteextender that displays the list of countries. In the same textbox, when I type something and click on "Search" button, a pop-up should be opened and should display the matching countries. I am using modalpopupextender for the popup.
aspx code:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<contenttemplate>
<asp:TextBox id="TextBox1" runat="server" Width="250px"></asp:TextBox>
<asp:ImageButton id="ImageButton1" onclick="imgBtnSearch_Click" runat="server" ImageUrl="~/Images/Lab/search.jpg"></asp:ImageButton>
<cc1:AutoCompleteExtender id="TextBox1_AutoCompleteExtender" runat="server" EnableCaching="true" CompletionSetCount="10" MinimumPrefixLength="1" ServicePath="AutoComplete.asmx" UseContextKey="True" TargetControlID="TextBox1" ServiceMethod="GetCountryInfo">
</cc1:AutoCompleteExtender>
<cc1:ModalPopupExtender id="ModalPopupExtender1" runat="server" TargetControlID="ImageButton1" BackgroundCssClass="ModalPopupBG" Drag="true" PopupDragHandleControlID="PopupHeader" PopupControlID="updatePanel2" CancelControlID="btnCancel" ></cc1:ModalPopupExtender>
</contenttemplate>
</asp:UpdatePanel>
<asp:UpdatePanel id="updatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:RadioButtonList id="RadioButtonList1" runat="server" Width="400" Height="400" RepeatColumns="5" RepeatLayout="Table" RepeatDirection="Vertical" AutoPostBack="True"></asp:RadioButtonList>
<DIV class="Controls">
<INPUT id="btnOk" type="button" value="OK" />
<INPUT id="btnCancel" type="button" value="Cancel" />
</DIV>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ImageButton1" EventName="Click"></asp:AsyncPostBackTrigger>
</Triggers>
</asp:UpdatePanel>
And in my codebehind:
protected void imgBtnSearch_Click(object sender, ImageClickEventArgs e)
{
LoadCountryPopUp();
ModalPopupExtender1.Show();
}
I am not getting any countries in my popup, although I get results via my autocompleteextender. On clicking the imagebutton, I get the popup without any content. Pls help!
Place your popup contents inside panel like this:
<asp:UpdatePanel id="updatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel id="pnlPopup" runat="server">
<asp:RadioButtonList id="RadioButtonList1" runat="server" Width="400" Height="400" RepeatColumns="5" RepeatLayout="Table" RepeatDirection="Vertical" AutoPostBack="True"></asp:RadioButtonList>
<DIV class="Controls">
<INPUT id="btnOk" type="button" value="OK" />
<INPUT id="btnCancel" type="button" value="Cancel" />
</DIV>
</Panel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ImageButton1" EventName="Click"></asp:AsyncPostBackTrigger>
</Triggers>
</asp:UpdatePanel>
and replace UpdatePanel's controls Id with this Panel's id:
<cc1:ModalPopupExtender id="ModalPopupExtender1" runat="server" TargetControlID="ImageButton1" BackgroundCssClass="ModalPopupBG" Drag="true" PopupDragHandleControlID="PopupHeader" PopupControlID="pnlPopup" CancelControlID="btnCancel" ></cc1:ModalPopupExtender>
and check if it works?
Try putting the extenders outside of the update panel, it should not be a child of what it is extending

How To Update TextBox using Update panel in asp.net

I Have a TextBox outside the UpdatePanel and a Button inside the Updatepanel When i click on the Button it show value in TextBox.
I Have Write Following Code.
<%# Register TagPrefix="AjaxToolKit" Assembly="AjaxControlToolkit"
Namespace="AjaxControlToolkit" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"
EnablePageMethods="true" />
<asp:TextBox ID="TextBox5" runat="server" />
<asp:UpdatePanel runat="server" ID="Up1">
<ContentTemplate>
<asp:Button ID="btn" runat="server" onclick="btn_Click" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
----------Code Behind---------
protected void btn_Click(object sender, EventArgs e)
{
TextBox5.Text = "20000";
}
Your textbox value can't be updated untill you put it in an update panel.
<ContentTemplate>
<asp:TextBox ID="TextBox5" runat="server" />
<asp:Button ID="btn" runat="server" onclick="btn_Click" />
</ContentTemplate>
OR, it would be better if you register a trigger of the button and pull your button out from your update, like...
<asp:UpdatePanel runat="server" ID="upnl" UpdateMode="Conditional" >
<ContentTemplate>
<asp:TextBox ID="TextBox5" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btn" EventName="Click" />
</Triggers>
</asp:UpdatePanel>

update panel not working

i am having two list box which perform add remove item functionality which are controlled by four buttons and o each button click there happen to be post back but i don't want it to be flicker for which i am using update panel like this but it still made post back wats wrong with this explain me this
<asp:UpdatePanel ID="button" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text=">" OnClick="ButtonAdd_Click" Width="50px"/><br />
<asp:Button ID="ButtonRemove" runat="server" Text="<" OnClick="ButtonRemove_Click" Width="50px"/><br />
<asp:Button ID="ButtonAddAll" runat="server" Text =">>>" OnClick="ButtonAddAll_Click" Width="50px"/><br />
<asp:Button ID="ButtonRemoveAll" runat="server" Text ="<<<" OnClick="ButtonRemoveAll_Click" Width="50px"/>
</ContentTemplate>
</asp:UpdatePanel>
I wrote a quick example that does work. You do not need your buttons in the UpdatePanel. You only need the ListBox since they are the only controls being refresh. Setup the Trigger for the UpdatePanel will cause the refreshes to occur without the 'flicker'.
aspx code:
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:Button ID="ButtonAdd" runat="server" Text=">" OnClick="ButtonAdd_Click" Width="50px"/><br />
<asp:Button ID="ButtonRemove" runat="server" Text="<" OnClick="ButtonRemove_Click" Width="50px"/><br />
<asp:Button ID="ButtonAddAll" runat="server" Text =">>>" OnClick="ButtonAddAll_Click" Width="50px"/><br />
<asp:Button ID="ButtonRemoveAll" runat="server" Text ="<<<" OnClick="ButtonRemoveAll_Click" Width="50px"/>
<asp:UpdatePanel ID="button" runat="server" UpdateMode="Always">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ButtonAdd" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="ButtonRemove" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="ButtonAddAll" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="ButtonRemoveAll" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
<asp:ListBox ID="ListBox2" runat="server"></asp:ListBox>
</ContentTemplate>
</asp:UpdatePanel>
</div>
cs (codebehind) code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ListBox1.Items.Add(new ListItem("Test1", "1"));
ListBox1.Items.Add(new ListItem("Test2", "2"));
ListBox1.Items.Add(new ListItem("Test3", "3"));
ListBox1.Items.Add(new ListItem("Test4", "4"));
ListBox1.Items.Add(new ListItem("Test5", "5"));
}
}
protected void ButtonRemove_Click(object sender, EventArgs e)
{
if (ListBox2.SelectedItem != null)
{
ListBox1.Items.Add(ListBox2.SelectedItem);
ListBox2.Items.RemoveAt(ListBox2.SelectedIndex);
ListBox2.ClearSelection();
ListBox1.ClearSelection();
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
if (ListBox1.SelectedItem != null)
{
ListBox2.Items.Add(ListBox1.SelectedItem);
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex);
ListBox1.ClearSelection();
ListBox2.ClearSelection();
}
}
I have tested this and it does work. I only implemented 2 of the Buttons to present a complete example.
Add the asp:ListBox to the ContentTemplate of your asp:UpdatePanel, or add a new asp:UpdatePanel and add it to its ContentTemplate. The control won't be updated using an asynchronous postback if it's not placed within an asp:UpdatePanel.
With the following code snippets the newly added listBox will be updated with new content using an asynchronous postback:
Page.aspx:
<asp:UpdatePanel ID="button" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text=">" OnClick="ButtonAdd_Click" Width="50px" /><br />
<asp:Button ID="ButtonRemove" runat="server" Text="<" OnClick="ButtonRemove_Click"
Width="50px" />
<br />
<asp:Button ID="ButtonAddAll" runat="server" Text=">>>" OnClick="ButtonAddAll_Click"
Width="50px" />
<br />
<asp:Button ID="ButtonRemoveAll" runat="server" Text="<<<" OnClick="ButtonRemoveAll_Click"
Width="50px" />
<asp:ListBox runat="server" ID="listBox"></asp:ListBox>
</ContentTemplate>
</asp:UpdatePanel>
Sample event handler in the code-behind file, Page.aspx.cs:
protected void ButtonAddAll_Click(object sender, EventArgs e)
{
listBox.DataSource = new List<string>() { "Test" };
listBox.DataBind();
}
<asp:UpdatePanel ID="button" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text=">" OnClick="ButtonAdd_Click" Width="50px" /><br />
<asp:Button ID="ButtonRemove" runat="server" Text="<" OnClick="ButtonRemove_Click"
Width="50px" />
<br />
<asp:Button ID="ButtonAddAll" runat="server" Text=">>>" OnClick="ButtonAddAll_Click"
Width="50px" />
<br />
<asp:Button ID="ButtonRemoveAll" runat="server" Text="<<<" OnClick="ButtonRemoveAll_Click"
Width="50px" />
<asp:ListBox runat="server" ID="listBox"></asp:ListBox>
</ContentTemplate>
</asp:UpdatePanel>

Resources