Dynamic controls in updatepanel not posting in Opera Mobile - c#

I have a page to which I am adding a dynamic UpdatePanel with more dynamic controls in it's ContentTemplateContainer. ViewState is disabled on the ContentTemplateContainer because the entire state can be recreated on postbacks from a single ID in a HiddenField like so:
if (request.HttpMethod == "POST")
{
string ctrlName = request.Params.Get("__EVENTTARGET");
int yearID = int.Parse(request.Form["hfPrevYear"]);
if (!string.IsNullOrEmpty(ctrlName) && ctrlName == "btnYearUp")
{
yearID = getNextDataYear(di.getTestData(), yearID);
}
else if (!string.IsNullOrEmpty(ctrlName) && ctrlName == "btnYearDown")
{
yearID = getPrevDataYear(di.getTestData(), yearID);
}
}
The problem is that Opera Mobile is not posting the hidden field, causing my page to throw an exception when it tries to parse that value. It only posts controls outside the update panel like the hidden fields that ASP.NET adds for viewstate and event target. When I was stepping through the postback code in Dragonfly, I notice that the document.forms[0].elements[] collection doesn't include my controls, which is what the updatepanel code loops through to build the post request.
Other browsers post the field just fine, including Opera Mini and Desktop Opera.
Any idea what's going on here?
Thanks for the help,
--Nick

Since you haven't posted an example, I can only give you some general tips. My best guess is that the core version of Opera mobile may have a bug that's already fixed in Opera desktop/Mini.
Make sure your markup inside the form is correct - if tags are in the wrong order somewhere, for example something like
<p><b></p></b>
it can confuse the parser, and an example of such confusion might be that a form is closed too early and controls end up "outside" it. The validator.w3.org service can help you find such problems.
Also, check that tags that need closing do have a closing tag, for the same reason.
How are the elements added to the form - are they in the markup from the beginning, or added with JavaScript? If the latter, try adding them to the form itself explicitly (document.forms[x].appendChild().. rather than appending them to an element you think is inside the form).

Related

TabContainer goes Invisible when I make child tab visible = false

I've been struggling with this for a day. Not finding any others with my exact situation, so I figured I'd post and answer with what worked for me.
Environment: Asp.net 4.0 - AjaxControlToolkit v.7.1213.0
Problem: I have a TabContainer with 3 tabs, and based on a database value, I will make the 2nd tab invisible or not. My problem is that when I make that second tab invisible it makes the entire tabContainer invisible. When I inspect source, I can see the control is rendered on the page (tested in Firefox And Chrome), but there's now a style tag (visibility:hidden) that is coming from somewhere NOT in my code (master page, child page, style.css, c# codebehind files, etc), as far as I can tell. I have yet to find an explanation for this errant style tag. I'm not an ASP.net master, so it could be some idiosyncracy with my code, but it's also possible that this is a bug with AjaxControltoolkit.
I'll answer this with the workaround that is currently working for me.
Workaround:
Since I only need to remove / hide this from my users, I am able to use the Tabcontainer.Remove method. When using this method, my tabContainer no longer disappears after postback when it contains an invisible tab.
I replaced:
if(x.value == true)
tabpanel1.Visible == false;
with:
if(x.value == true)
tabContainer1.Tabs.Remove(tabpanel1);

Find what Elements were added by Javascript

I have got an ASP-Site, which enables the user to Add Label-Elements. I don’t know how many Labels where added or which ID they have. I know only, they will be within the Panel pnl_Added. After the user has added all his labels, he pushes a Send-Button for Update.
So, now I am at my Server, awaiting this postback, but I don’t know where, when and how to find out, which Elements were Added to pnl_Added. Can somebody help me?
I have tried something like that:
protected void Page_Load(object sender, EventArgs e)
{
[...]
for (int i = 0; i < pnl_Added.Controls.Count; i++)
{
[...]
}
[...]
}
But I think it is too late because of the loaded ViewState? Is that possible?
I am working with VS 2013, ASP c#, with the .Net Framework 4.
On server, controls tree doesn't created from actual client HTML. Actually, server doesn't know anything about client HTML besides input tags values in scope of submitted form. In general, all controls available in Page_Load method, created on server side from aspx file markup.
To implement your scenario, you need to add hidden field for each label, added from client and save label's inner text into hidden field's value. Then you'll can get these labels texts as below:
var labels = Request.Form["hiddenField's name"] as string[];
You should go one lever deeper and take the added elements from Request variable, because the control pnl_Added doesn't know about them as there was no postback.
Something like this:
Request.Form["field_id"]
I suggest to run the page in debug mode, review Request.Form collection and find what you need. You should see your label elements there.

Making a treeview mimic required field validator functionality

I am in need of validating a TreeView to make sure that users have selected something. What I need is something that mimics a RequiredFieldValidator and stops a page from posting back with any data until something is selected.
I am using C# and ASP.NET 3.5
I have tried this and it did not work for me:
if (TreeView.SelectedNode.Value == null)
{
lblError.Text = "Required";
lblError.ForeColor = Color.Red;
}
I am at a loss, and any help would be very appreciated.
As far as I can say, TreeView is basically used for navigation and in navigation you cannot force a user to choose node. But if its requirement in your app. If your are navigating to other page, then you can do one thing, add some flag or value to your querystring so that the app will know whether its coming from treeview or directly by copy pasting the url. Catch it all in your Page_Load event.
or Create a Session variable in the treeview selected event Session["TreeviewChecked"]= true;
In your code check if Session is null or not.
I hope this will help.

Iterating Dynamic FileUpload Control Collection in Panel Control using ASP.NET C#

I'm trying to get the values of dynamically generated FileUpload controls that I add to a Panel:
<asp:Panel ID="pFileControls" runat="server">
</asp:Panel>
I create the controls during a loop through a record set:
foreach(DataRow dr in ds.Tables[0].Rows)
{
FileUpload fu = new FileUpload();
fu.ID = dr["SomeID"].ToString();
pFileControls.Controls.Add(fu);
}
Everything works fine up to the point where I submit the form with this button:
<asp:Button ID="btnImportFile" runat="server" Text="Save" OnClick="btnImportFile_Click" />
Which I register like this (Page_Load):
ScriptManager.GetCurrent(this).RegisterPostBackControl(btnImportFile);
I do this because I'm using a MasterPage/ContentPage setting in my website and mostly everything happens inside an UpdatePanel for AJAXification purposes. Bear in mind that if I explicity specify a FileUpload Control in the HTML view, it works 100%.
When the form is submitted I try to iterate the Panel like this:
foreach (Control ctrl in pFileControls.Controls)
{
if (ctrl.GetType() != typeof(FileUpload))
{
continue;
}
//Do the saving of the file here
}
Except, the Panel seems to only return one control: The Content Place Holder for the page and nothing else. Does anyone have some ideas about this?
What part of the life cycle are you adding the dynamic controls?
if you are putting them in the page_load it may be too late, try putting the generation of the dynamic controls into the page_init and see if that fixes the problem.
page lifecycle
http://msdn.microsoft.com/en-us/library/ms178472.aspx
dynamic controls
http://geekswithblogs.net/shahed/archive/2008/06/26/123391.aspx
Note:
"Its recommended to load the dynamic
controls during the Page_Init instead,
because we may want to hook up our
events with proper handler at an early
stage. ... Do not assigning
properties of a dynamic control
(viewstate enabled), during Page_Init,
it will not be reflected. "
I would expect that even with the update panel, you will need to be mindful of the page_load limitations with dynamic controls.
let me know if this helps or if I missed the mark!
Let's try a different course of action (I've gotten dynamic file upload to work, but it was a bear and I wish I had simply used this)
http://www.asp.net/ajaxlibrary/act_AsyncFileUpload.ashx
or
http://en.fileuploadajax.subgurim.net/
these may not create a 'loop' of elements, but you can simply keep loading docs on a as-needed basis.
I have specifically used
http://www.asp.net/ajaxlibrary/act_AsyncFileUpload.ashx
to great effect.
There also appear to be some limitations to the update:panel and the file upload, check out these sites.
(this one says it does not work in partial update status but does work in full postback)
http://forums.asp.net/p/1105208/1689084.aspx
do you know if the submit is triggering the full page or just the update:panel? (check out this: http://geekswithblogs.net/mmintoff/archive/2009/04/01/fileupload-within-updatepanel.aspx

javascript viewstate problem

I have a situation that I am stuck with, and hoping someone can help. I am building a .NET/C# web application in which I have a tabbed panel layout, and when the user clicks on each of the tabs the display panel is updated using javascript to hide and show some divs. None of these clicks cause postback, it is all client-side, so I can't use viewstate or session.
What I want to do is somehow remember which panel was last visible when the page is refreshed, yet without posting back to the server I am unsure how to do this. I have tried a hidden field but obviously its value is reset every time because the form is never submitted. I do know that I can achieve this using cookies but its a little annoying to implement for such a (seemingly) trivial operation ... but maybe this is the only way?
Does anyone have any more elegant solution to this problem?
Using a function like this to show and hide tabs):
function makeCurrent(tab) {
if (tab.title == 'Manage orders') {
document.getElementById('panelOrders').style.display = "block";
// Hide others
document.getElementById('panelAccounts').style.display = "none";
document.getElementById('panelProducts').style.display = "none";
document.getElementById('panelSettings').style.display = "none";
// Remember last viewed panel
document.getElementById('hdnCurrentlyViewing').value = "orders";
}
The panels are just divs with style.display controlling their visibility. Not sure if its useful to post HTML code because its fairly self explanatory ...?
You can make this happen without a postback is to make an AJAX call from Javascript where you tell your server what the current panel is as you switch it.
I prefer using a framework like JQuery or Prototype to help make these AJAX calls myself.
I think Hidden field will be the best option. have you tried ASP:HiddenField? It can be accessed across postbacks.
But if you still have some reservations with postbacks and hiddenfield you can also use cookies from JS http://techpatterns.com/downloads/javascript_cookies.php this is helper lib for cookies manipulation within JS.
Regards.

Categories

Resources