I have the following test code:
protected void Page_Load(object sender, EventArgs e)
{
MyDiv.InnerHtml = "1=" + Request.Form.Count.ToString(); // WORKING
if (Request.Form.Count > 0)
{
MyDiv.InnerHtml = "2=" + Request.Form.Count.ToString(); // NOT WORKING
}
}
Why is this happening? Do I have to use the Page.IsPostback feature? I try to avoid that because it works poorly with my javascript code. I use jquery ajax when posting data.
It is not working because the whole ASP page is not reloaded when you do your AJAX call, and therefore MyDiv is not being updated in your browser.
You need to do something more like this:
protected void Page_Load(object sender, EventArgs e)
{
MyDiv.InnerHtml = "1=" + Request.Form.Count.ToString(); // WORKING
if (Request.Form.Count > 0)
{
Response.Write(Request.Form.Count.ToString());
Response.End();
}
}
From a code-maintenance point of view, it would probably be better to write a special page or handler for receiving the ajax postbacks, but I'm just sticking with the one page for simplicity.
And then add something like this to the settings parameter of your ajax call:
success: function(data){
$("<%=MyDiv.ClientId%>").innerHTML = "2=" + data.responseText;
}
Related
Because of the environment I work in I need to add controls to a page via code-behind. I have done it dozens of times. For some reason the FileUpload control is giving me grief. here is my code:
FileUpload fileUpload = new FileUpload();
fileUpload.ID = "FileUploadControl";
this.Controls.Add(fileUpload);
The page looks as though it is timing out and display this error, "Internet Explorer cannot display the webpage".
When I remove the last line (the Add), then the page renders just fine.
Any ideas?
Thanks!!
You didn't mentioned which event handler you have used. Please try this,
FileUpload file;
protected void Page_Load(object sender, EventArgs e)
{
file= new FileUpload();
PlaceHolder1.Controls.Add(file);
}
protected void Button1_Click(object sender, EventArgs e)
{
if(file.HasFile)
{
file.SaveAs(MapPath("~/" + file.FileName));
}
}
I hope you can help me. First, I'd like to tell you I am a desktop app guy, which means I mostly develop my apps in desktop. Now I am trying to build some web app but it leads me to am not sure if confusion or just am doing it wrong.
I have a code here that it populates a menu at runtime. Runtime because they the menu items are populated at code behind and the items are fetched in database.
here's the code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Menus menu = new Menus();
imgMainLogo.ImageUrl = VARIABLES.MainLogoImage;
menu.PopulateMenuControl(ref mainmenu, 2);
menu.PopulateMenuControl(ref footermenu, 9);
}
else
{
System.Diagnostics.Debug.WriteLine("link: " + footermenu.SelectedValue);
if (footermenu.SelectedValue != null)
{
Response.Redirect(footermenu.SelectedValue, true);
}
}
}
and the code in PopulateMenuControl
public void PopulateMenuControl(ref Menu menucontrol, int menuparentid)
{
//menucontrol.Items.Clear();
foreach (MenuFields mf in GetMenusByParentID(menuparentid))
{
MenuItem menuitem = new MenuItem(mf.MenuName, ReplaceSystemNameLink(mf.Link));
menucontrol.Items.Add(menuitem);
foreach (MenuFields cmf in GetMenusByParentID(mf.MenuID))
{
MenuItem childmenuitem = new MenuItem(cmf.MenuName, ReplaceSystemNameLink(cmf.Link));
menuitem.ChildItems.Add(childmenuitem);
}
}
}
So Page.IsPostBack is the very basic thing I should learn when doing something in a page. But the problem here is, one of my menu item in "footermenu" has an offsite link, and it should redirect the page into my blog.. but what's happening was, footermenu.SelectedValue is empty once I clicked on the "Blog" link.
What's going on?
UPDATE
I have updated the code still stuck, the SelectedValue is still empty
protected void Page_Load(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("Page_Load IsPostBack: " + Page.IsPostBack.ToString());
if (Page.IsPostBack)
{
if(footermenu.SelectedValue != null)
{
System.Diagnostics.Debug.WriteLine("link: " + footermenu.SelectedValue);
}
}
}
protected void Page_Init(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine("Page_Init IsPostBack: " + Page.IsPostBack.ToString());
if (!Page.IsPostBack)
{
Menus menu = new Menus();
imgMainLogo.ImageUrl = VARIABLES.MainLogoImage;
menu.PopulateMenuControl(ref mainmenu, 2);
menu.PopulateMenuControl(ref footermenu, 9);
}
}
You need to learn about the page lifecycle.
With dynamic controls (created and added in code), you need to re-create them on every page load - this is best done in the init event handler.
I need to open a browser tab from a link that is given to me by an asp.net code behind.
Normally I would have a link and target="_blank", but the link that I need is dynamic, so I must have the behavior of a _blank link from code behind.
Any Ideas?
If you have the data needed to create the link when generating the initial HTML, you can do something like this in the Page_Load event:
protected void Page_Load(object sender, EventArgs e)
{
Button1.OnClientClick="javascript:window.open('MyPage.aspx?Param=" + Param1.ToString() + "');"; }
}
If you're waiting for the PostBack to get the required data to build the link, you can send javascript down to the browser via the ScriptManager:
protected void Button1_Click(object sender, EventArgs e)
{
//process whatever you need to to get Param1
ScriptManager.RegisterStartupScript(Page, typeof(Page), "OpenWindow", "window.open('MyPage.aspx?Param=" + Param1.ToString() + "');",true);
}
You're looking for the Target property.
I have a .aspx web page, with a html form within it, this also has two input boxes.
Whats the best way to take the input box data and pass it to a new .aspx page where it is dealt with by the request method.
Assuming that the data is not sensitive then the best method to pass it to your new page using Response.Redirect and the querystring using:
protected void MyFormSubmitButton_Click(Object sender, EventArgs e)
{
string value1 = txtValue1.Text;
string value2 = txtValue2.Text;
// create a querystring
string queryString = "x=" + value1 + "&y=" + value2;
// redirect to the encoded querystring
Response.Redirect("NewPage.aspx?" + Server.URLEncode(queryString));
}
This web page has a lot of information which you can use for passing the values from page to page.
http://msdn.microsoft.com/en-us/library/6c3yckfw.aspx#Y1100
Try Server.Transfer:
Terminates execution of the current
page and starts execution of a new
page by using the specified URL path
of the page. Specifies whether to
clear the QueryString and Form
collections.
If you set the preserveForm parameter
to true, the target page will be able
to access the view state of the
previous page by using the
PreviousPage property.
Your main page:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
// ThreadAbortException occurs here.
// See http://support.microsoft.com/kb/312629 for more details.
Server.Transfer("AnotherPage.aspx", true);
}
}
"AnotherPage.aspx":
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
// Accessing previous page's controls
}
}
I need to insert a bit of Javascript into the process when a Web Form is submitted, but after the client side validation takes place.
RegisterOnSubmitStatement seems to place the javascript before the validation.
Anyone know how to get it to render after?
Solution found:
In a web control, I put something like this:
protected override OnInit(EventArgs e) {
Page.SaveStateComplete += new EventHandler(RegisterSaveStuff);
base.OnInit(e);
}
void RegisterSaveStuff(object sender, EventArgs e) {
Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "name", "JS code here");
}
that´s right, the RegisterOnSubmitStatement DO NOT WORK in the init function.
It should be called after in the page lige cycle.
I thing the right place therefor is:
"PreRenderComplete"
protected override OnInit(EventArgs e)
{
Page.PreRenderComplete+= new EventHandler(Page_PreRenderComplete);
base.OnInit(e);
}
void Page_PreRenderComplete(object sender, EventArgs e)
{
Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "name", "JS code here");
}
After some research online and playing around with it, I figured out that you can do it by hooking into the Page's SaveStateComplete event. I guess the validation submit statement is registered during the PreRender event, so if you register it after that (in the SaveStateComplete event) you can get it afterward.
You do have to re-register it, but that's not a big deal because I'm not relying on ViewState for my JS.