Using data input from pop-up page to current with partial refresh - asp.net

I'm building a product editor webpage using visual C#. I've got an image uploader popping up using fancybox, and I need to get the info from my fancybox once submitted to go back to the first page without clearing any info. I know I need to use ajax but how would I do it?
<%# Page Language="C#" AutoEventWireup="true" CodeFile="uploader.aspx.cs" Inherits="uploader" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body style="width:350px; height:70px;">
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<div style="width:312px; height:20px; background-color:Gray; color:White; padding-left:8px; margin-bottom:4px; text-transform:uppercase; font-weight:bold;">Uploader</div>
<asp:FileUpload id="fileUp" runat="server" />
<asp:Button runat="server" id="UploadButton" text="Upload" onclick="UploadButton_Click" />
<br /><asp:Label ID="txtFile" runat="server"></asp:Label>
<div style="width:312px; height:15px; background-color:#CCCCCC; color:#4d4d4d; padding-right:8px; margin-top:4px; text-align:right; font-size:x-small;">Click upload to insert your image into your product</div>
</div>
</form>
</body>
</html>
CS so far
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration; // Add to page
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data; // Add to the page
using System.Data.SqlClient; // Add to the page
using System.Text; // Add to Page
public partial class uploader : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void UploadButton_Click(object sender, EventArgs e)
{
if (fileUp.HasFile)
try
{
fileUp.SaveAs("\\\\london\\users\\DP006\\Websites\\images\\" +
fileUp.FileName);
string imagePath = fileUp.PostedFile.FileName;
}
catch (Exception ex)
{
txtFile.Text = "ERROR: " + ex.Message.ToString();
}
finally
{
}
else
{
txtFile.Text = "You have not specified a file.";
}
}
}

Related

How to solve the error Using the AjaxCallHelper write methods outside of an AjaxCall is not allowed

I've searched this error in all google results, but I can't find a solution.
I have this WebForm:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="LoTest.aspx.cs" Inherits="-.-.-.LoTest" %>
<%# Register TagPrefix="ajax" Namespace="MagicAjax.UI.Controls" Assembly="MagicAjax" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<ajax:AjaxPanel ID="ajaxLG" runat="server" AjaxCallConnection="Synchronous">
<asp:Button ID="btnTest" runat="server" CssClass="button" ToolTip="gin." Text="LogIn" OnClick="btnTest_Click"></asp:Button>
</ajax:AjaxPanel>
</div>
</form>
</body>
</html>
Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MagicAjax.UI;
using MagicAjax;
namespace -.-.-
{
public partial class LoTest : AjaxPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnTest_Click(object sender, EventArgs e)
{
AjaxCallHelper.WriteAlert("Test msg");
}
}
}
The web.config:
<httpModules>
<add name="MagicAjaxModule" type="MagicAjax.MagicAjaxModule, MagicAjax"/>
</httpModules>
In the Line “AjaxCallHelper.WriteAlert("Test msg");” throws the error:
Using the AjaxCallHelper write methods outside of an AjaxCall is not allowed.
In IE, just add the site to the compatibility view and that´s all.
Seems like if AjaxMagix.dll works with older elements of the explorer.
In Google Chrome doesn´t work.

ASP Data-binded dropdown list without autopostback

I have a simple form with a dropdown list binded to a string array (as a simple example). The form is submitted when the user clicks a button.
I want to query the selected item in the list. I read the SelectedValue member of the dropdownlist which always contains the default item no matter what I select in the form.
I can't use autopostback on the list as in my production environment the form is displayed in dynamic div using jquery.
If I remove the binding and add the list items in the asp file using ListItems tags, than it magically works.
My sample asp code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
And the code-behind file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string[] items = { "bindItem1", "bindItem2", "bindItem3" };
DropDownList1.DataSource = items;
DropDownList1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
string text = TextBox1.Text;
string item = DropDownList1.SelectedValue;
}
}
In the page_load do the databinding only if Page.IsPostBack==False.
if (!IsPostBack)
{
//do the data binding
}
Your code now on every page load, will bind the data again and again, so the selected value is "not" changing.

DropDownList1 does not exists in the current context error

I have an aspx application. I have defined a dropdownlist in the .aspx file and try to load some data into it from database in .aspx.cs file. But I am getting the error in .aspx.cs as "DropDownList1" does not exists in the current context.
Below is the code:
Wiki.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Wiki.aspx.cs" Inherits="FinalProj2._Wiki" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
body { margin: 4%; }
#space1 { height:1em; }
#space2 { height:1em; }
</style>
</head>
<body>
<h1>WIKI</h1>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" Width="100%"></asp:DropDownList>
</div>
<div id="space1"></div>
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack = "true" Height = "150px" Width = "100%"></asp:ListBox>
<div id="space2"></div>
<asp:TextBox ID="TextBox2" runat="server" Height="250px" Width="100%" TextMode="MultiLine"></asp:TextBox>
</form>
</body>
</html>
Wiki.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FinalProj2.Controllers;
namespace FinalProj2
{
public partial class _Wiki : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FinalProj2.Models.DataClasses1DataContext db = new FinalProj2.Models.DataClasses1DataContext();
Response.Write("<br/>Page.User.Identity.Name: " + Page.User.Identity.Name);
Response.Write("<br/>Page.User.Identity.IsAuthenticated: " + Page.User.Identity.IsAuthenticated);
Response.Write("<br/>Page.User.Identity.AuthenticationType: " + Page.User.Identity.AuthenticationType);
var query = from meet in db.Meets
select meet.Summary;
// where meet.Meeting_ID = (from meet_emp in db.Meet_Emps
//where meet_emp.Employee_Name == Page.User.Identity.Name
//select meet_emp.Meeting_ID)
DropDownList1.DataSource = query;
DropDownList1.DataBind();
}//end
}
}
What could be the possible reason. According to me I feel everything is fine as had used the same way for a list box into another application.
Make sure you have no files that accidentally try to inherit or define the same (partial) Mlass as other files.
Thus no other code behind file should have same signature in FinalProj2 Namespace.
public partial class _Wiki : System.Web.UI.Page

Why do I get the result zero when I try to get the width of a DropDownList control in asp.net?

After I click button1, it display 0, why? How can get correct width of a DropDownList control? Thanks!
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Button1.Text = DropDownList1.Width.Value.ToString();
}
}
It's because the value Width is not set. In html, when the width value is not set, you let the browser choose the size.
Moreover, the asp.net engine only host properties to be able to render the correct Html. If you look at the code of the textbox, you will see that if you put a witdh, it will produces a style="width:yourvalue". ASP.net runs on the server side, so it do not have any idea of the actual size of the control.
If you actually require the client size of the dropdownlist, either specify it explicitly, or use some custom javascript to calculate the actual size of the control, then to pass the calculted size to an hidden field. In the code behind you be able to get the value from the Page.Request.Form collection.
Please clarify your need if you need further explanation.

LinkButton's server-Side event not firing when disabled=true, and inside an UpdatePanel, in IE8

It is hitting the Page_Load event, but not the LinkButton's click :
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode=Conditional>
<ContentTemplate>
<asp:LinkButton ID="btnRenewAll" runat="server" onclick="LinkButton1_Click" OnClientClick="javascript:return ClientMe()">LinkButton</asp:LinkButton>
<br />
<asp:Label ID="lblMe" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
<script>
function ClientMe() {
var btnRenewall = document.getElementById('<%= btnRenewAll.ClientID %>');
btnRenewall.disabled = true;
alert("Hello");
return true;
}
</script>
</html>
CodeBehind :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
lblMe.Text = "Checked";
UpdatePanel1.Update();
}
}
If you replace your JS with:
function ClientMe() {
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(startRequest);
return true;
}
function startRequest(sender, e) {
var btnRenewall = document.getElementById('<%= btnRenewAll.ClientID %>');
btnRenewall.disabled = true;
alert("Hello");
}
it should work. citing:
link text
Change this line:
btnRenewall.disabled = 'disabled';
Here is a list of the minimized attributes in HTML and how they should be written in XHTML :
HTML XHTML
compact compact="compact"
checked checked="checked"
declare declare="declare"
readonly readonly="readonly"
disabled disabled="disabled"
selected selected="selected"
defer defer="defer"
ismap ismap="ismap"
nohref nohref="nohref"
noshade noshade="noshade"
nowrap nowrap="nowrap"
multiple multiple="multiple"
noresize noresize="noresize"

Resources