DropDown values lost when FileUpload.IsInFileUploadPostBack - c#

i'm working on a web project. I have a dropdown and i need the selectedvalue for uploading a file using asp:AjaxFileUpload.
Issue: when uploading a file, dropdown values are lost and the selectedvalue will be blank. I don't want to use a Session variable because if I open the same page in two tabs with different dropdown.selectedvalue, the other page might get the wrong session variable. Please help me find another way of getting the dropdown.selectedvalue when uploading a file,
your help will be really appreciated

Try this.
Enclose your DropDownList in an asp UpdatePanel as below:
<asp:UpdatePanel id="someid" runat="server>
<ContentTemplate>
<asp:DropDownList ID="yourdropdownlist" runat="server"></asp:DropDownList>
</ContentTemplate>
</UpdatePanel>

This is because your page is posting back each time.
Set AutoPostBack='false' and use Page.IsPostback property while populating and dealing with drop down box.
MSDN For IsPostBack:
http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx
Hope Its Helpful.

You can use update panel and inside that put your dropdown, file upload control etc..
in your code behind on Page_Load check below condition and bind data.
if (!IsPostBack && !IsCallback && !AjaxFileUpload1.IsInFileUploadPostBack) // Need to prevent normal stuff on ajaxFileUpload PostBack
{
// bind drop down , gridview etc..
}

Related

How can i check if a checkbox is checked on a different asp page?

Basically i've got a settings page, and when you check the checkbox, it should hide certain topics on another page...
How would i go about checking whether the checkbox is checked?
Just need ideas on how to approach it.
(ASP and C#)
Thanks,
Becki
asp.net c# is checkbox checked?
Specify event for Button Click
<asp:Button ID="PublishButton" runat="server" Text="Save" onclick="PublishButton_Click" />
Just you can store the return value ie true or false in a session variable. So that every page can know that whethere the particular check box is checked or not. With this you can automate your code. I haven't tried it but it may work i guess. ie.
`if(checkbox.checked)
session["variablename"]=true;// u have to decide
else
{
// false statement
}
`
Hence this variable session["variablename"] can be accessible in that particular user session about 20mins.
You can check your setting by ajax call in time periods.
To do this :
Settings page changes value from 0 to 1 in your database.
Your pages should have jQuery ajax calls to get the latest value of your setting and apply it.
I hope it help.

Ajax AsyncFileUpload fires server code but does not update client web page

I have an asyncfileupload control inside an update panel. The file succesfully upload and fires the correct server side code. The code on the server is exected as expected however, one line in the server code changes the text on a label. I step through the code in debug mode and the line is executed but no change is made to the page.
Here's some of the code:
<asp:UpdatePanel runat="server" ID="updater" >
<ContentTemplate>
<asp:AsyncFileUpload ID="fileUpload" runat="server" OnUploadedComplete="FileUploadComplete" />
<asp:Label ID="AsyncText" runat="server" Text="File Type not checked" />
</ContentTemplate>
</asp:UpdatePanel>
public void FileUploadComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
System.Threading.Thread.Sleep(500);
if(fileUpload.HasFile) { AsyncText.Text = "file of correct format: "; }
}
Can anyone help me with solving this problem or offering annother solution??
Thanks
I think you are going to have to move toward a different solution. From your label message, it looks like you are trying to check the file type, correct? Basically, the AsyncUplaod control, although posting back to get the uploaded file to the browser, is not actually updating the page's viewstate, thus the label never gets updated. Boo! I was able to visualize this using this code in the page load event.
if (Page.IsPostBack)
{
if (Request.Files.Count > 0)
{
AsyncText.Text = "file of correct format";
ListItem item = new ListItem("item to add");
lb.Items.Add(item);
}
}
This was allowing me to set the label text but still nothing changed until I clicked on a random button that I added to the page. This button didnt even have an event in the code behind, but it was enough to cause a normal postback, and the label text and list item were successfully updated/added to the list. With that said, I would wait to update any labels until the user clicks upload by using a seperate upload button. (ie use the AsyFileUplaod to get it to the browser, but another button to save the file to the server). You can always do file evaluations in the button click event by referencing the posted files to the webpage as I did in the code above.
Some other examples I found online were using javascript to change the label text which works well also. Somthing like this:
string message = "";
if (e.StatusMessage == "Success")
{
message = "File upload successful;";
}
else
{
message = "File did not upload successfully;";
}
ClientScript.RegisterStartupScript(this.GetType(), "akey", "document.getElementByID('label').value =" + message, true);
Another example: here
I think in this case it's just the nature of the control and the only way to achieve what you want is though some creative thinking. If you have any other questions about anything I listed here feel free to ask.
Good luck!
Where is the label positioned, inside or outside the update panel? Seems like the partial page update may not be including the update to the label text. I would say move the label around as the simplest suggestion, but you could also try something like RegisterStartupScript which will change the lable text via javascript. This should still give you server side control over what text to display based on what happens during the upload.
If you could post some code that would be great.

Details grid view refreshes the whole page unexpectedly

I have a grid view and details view. My details view is in the update panel and it should be displayed for edits of the gridview. I have a file Upload control inside detailsview, Now when I click the upload button the whole page gets refreshed.
What do I do and where do I do? I think it should be some problem with the postback triggers.
use this..
http://geekswithblogs.net/ranganh/archive/2008/04/01/file-upload-in-updatepanel-asp.net-ajax.aspx
set
<Triggers><asp:PostBackTrigger ControlID="your fileupload control id" />
</Triggers>
in update panel
I think , you must post back when uploading a file, but try to do the following:
in the file uploader event : call the following method:
updatepanelName.Update();
If, it should not get refreshed then use the AutoPostBack= false in Upload control.

can't get value in code-behind after jQuery appends to asp:Listbox

I've created 2 asp:listbox... asp:ListboxFrom and asp:ListboxTo. When I double click on the item on ListboxFrom it moves (appends) the item to ListboxTo... So everything works fine till here. But when I click on the submit button to process everything I don't get the selected items in my code-behind for the listboxes. I debugged on Page_Init and Page_load but it restores the default controlstate values.... So what am I missing now?
1) How can I get the values in code-behind ? so the state of asp:listbox as it is added client-side code.
2) another question: when it appends to the ListboxTo the items are selected per default (they are blue). How can I avoid this?
thanks for helping,
below is the simple JQuery code I use in aspx,
<asp:ListBox ID="listboxFrom" SelectionMode="Multiple" />
<asp:ListBox ID="listboxTo" SelectionMode="Multiple" />
$(function() {
$('select[id$=dnn_ctr658_ViewAgenda_Invoer_listboxFrom]').bind("dblclick", function() {
$("select[id$=dnn_ctr658_ViewAgenda_Invoer_listboxFrom] option:selected").appendTo("#dnn_ctr658_ViewAgenda_Invoer_listboxTo");
});
$('select[id$=dnn_ctr658_ViewAgenda_Invoer_listboxTo]').bind("dblclick", function() {
$("select[id$=dnn_ctr658_ViewAgenda_Invoer_listboxTo] option:selected").appendTo("#dnn_ctr658_ViewAgenda_Invoer_listboxFrom");
});
});
This is a security thing. I wonder why aspnet doesn´t throw a warning exception for an unauthorized element manipulation...
You will have to create either a custom element or u use a hiddenfield. But please be very carefull with data, lists that everybody can change on client side. Everybody could insert items in your dropdown and make it selected...

Selected Item not being updated?

I've got the following DropDownList control:
<asp:DropDownList ID="SubjectFilter" runat="server" AutoPostBack="True" onselectedindexchanged="SubjectFilter_SelectedIndexChanged"></asp:DropDownList>
SubjectFilter data:
BookStore b = new BookStore();
b.LoadFromXML(Server.MapPath("list.xml"));
SubjectFilter.DataSource = b.BooksList.Select(x => x.Subject).Distinct().ToArray();
SubjectFilter.DataBind();
SubjectFilter.Items.Insert(0, new ListItem("הכל", "Default"));
Everything loads just fine. However in the SubjectFilter_SelectedIndexChanged method, SubjectFilter.SelectedValue is always Default, even though I'm selecting different options.
What is the problem?
Thank you very much.
I'm guessing the above code is from the PageLoad event. You may want to wrap that in a if(!isPostBack) block.
Make sure that in your Page_Load that you only populate the dropdown when IsPostBack is false.
For example
public void Page_Load(...)
{
if (!IsPostback())
UpdateDisplay();
}
When are you binding the drop down? You may any to wrap in an
If(page.ispostback ==false)
It looks like you may be binding on page load, before you check its value..
ViewState is assigned between the Init and Load for an ASP.NET page. Your event handlers occur after load. If you are programmatically setting content in the controls that your user will be using, you want to handle that before ViewState is applied. In other words, move it to Page_Init. Afterwards, ViewState kicks in and you'll see what the user actually selected when your handler executes.

Categories

Resources