I am using FileUpload Control to upload images, I can select the image using that browse button, but when i try to preview that selected one, i am not getting the file name, its showing empty..
protected void btnImgUpload_Click(object sender, ImageClickEventArgs e)
{
try
{
string strimage;
string strfilename, strextn;
if (fupImage.HasFile)
{
In the above code,fupImage.FileName property should have to selected Image name, but it remains as empty string "" , so fubImage.HasFile condition is going false. I am not getting why the condition is going false, while the file is selected,? what is the problem here?
Thanks in advance
what i did to test it is
I create an Asp page
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>
<!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:FileUpload ID="fupImage" runat="server" />
<asp:Button ID="btnImageUpload" runat="server" onclick="btnImageUpload_Click"
Text="Upload" />
</div>
</form>
</body>
</html>
and the code behind class is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication2
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnImageUpload_Click(object sender, EventArgs e)
{
try
{
string strimage;
string strfilename, strextn;
if (fupImage.HasFile)
{
//do something
}
}
catch
{
}
}
}
}
and nothing goes wrong with that
I want to say to test it just create a very simple instance and test it in complex environment may be there are some extra rules may prevents normal jobs! and sometimes it looks so unnoraml
Check for the View-state property of that file uploader Control, If View-state is false, then on post back you'll get empty value
Related
First I have index.aspx and below that there is a c# class called index.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Index : Page
{
public string test = "hello";
protected void Page_Load(object sender, EventArgs e)
{
test = "Hello lesley";
}
}
But how can I show the value of the 'test' string in the html page?
In your markup use asp:Label as
<asp:Label runat="server" ID="MyLabel"></asp:Label>
And in your backend code use Text property accordingly
public string test = "hello";
if(!IsPostBack)
{
MyLabel.Text = test;
}
Try this
protected void Page_Load(object sender, EventArgs e)
{
test = "Hello lesley";
Label label = new Label();
label.Text = test;
Page.Controls.Add(label);
}
Alternatively, if you already have that property in your index.aspx.cs, you can do this in index.aspx html page.
<!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">
</head>
<body>
<form id="form1" runat="server">
Hi <b><%=test%></b>
</form>
</body>
I have an ASP.net WebForm. The markup is like:
<div>
<input type="text" id="input" runat="server" value=" " />
<asp:Button Text="send" OnClick="btnsend_Click" ID="btnsend" runat="server" />
</div>
This HTML is generated at runtime . The events are defined in the code behind file.
I need to add these controls at runtime. I tried to use the Literal-Control but the controls are working just like HTML Controls and not like ASP.net Controls.
EDIT:
Note: The Project type should be Website, not a web Application. Web application won't support on demand compilation where website yes, it is.
If I understood currectly, you want to take the Markup from User which contains even asp.net controls and scriplets too.
if this is the case, Follow below steps:
Create a dummy .ascx control file, like DynamicMarkup.ascx with empty content
Add this user control to the page (xxxx.aspx) where you want to show this control statically so it registered to the page
<%# Register src="~/DynamicMarkup.ascx"
tagname="DynamicMarkup" tagprefix="MyASP" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:PlaceHolder runat="server"
ID="DynamicMarkupContainer" ></asp:PlaceHolder>
</div>
</form>
</body>
</html>
Write user input markup (may be get from database based on your criteria) to the DynamicMarkup.ascx file in page OnInit of the page (xxxx.aspx) And the create object of this DynamicMarkup
DynamicMarkup dynamicMarkup = LoadControl("~/DynamicMarkup.ascx") as
DynamicMarkup;
DynamicMarkupContainer.Controls.Add(ucSimpleControl);
I have not tested this approach, Just give a thought, With this you may get some session overwriting issue which you need handle.
Hope this will help!!
OLD:
is this the one that you are expecting? TextBox, and Button controls are available in System.Web.UI.WebControls namespace.
void Page_Load(Object sender, EventArgs e)
{
TextBox input = new TextBox();
input.Id ="input";
this.PlaceHolder.Controls.Add(input);
Button btnSend=new Button();
btnSend.Id ="btnSend";
btnSend.Text="Send";
btnSend.Click += new EventHandler(btnSend_Click);
this.PlaceHolder.Controls.Add(btnSend);
}
void btnSend_Click(object sender, EventArgs e)
{
// throw new NotImplementedException();
}
<%# 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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:PlaceHolder ID="phHolder" runat="server"></asp:PlaceHolder>
</form>
</body>
</html>
code behind :
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Init()
{
GenerateContorls();
}
protected void Page_Load(object sender, EventArgs e)
{
}
private void GenerateContorls()
{
TextBox newTxt = new TextBox() { ID = "txtsend" };
Button newBtn = new Button() { Text = "Send", ID = "btnsend" };
newBtn.Click += btnsend_Click;
phHolder.Controls.Add(newTxt);
phHolder.Controls.Add(newBtn);
}
protected void btnsend_Click(object sender, EventArgs e)
{
TextBox txt = (TextBox)this.FindControl("txtsend");
//your code
}
}
hope it helps
I have created a TextBox dynamically, and i am getting the value of the textbox when i click the button. But the value entered in the dynamic textbox gets empty when i click the button.
Below is my ASPX Code:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Reports.WebForm1" %>
<!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>
</div>
<asp:DropDownList ID="DropDownList1" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack = "true">
<asp:ListItem>Text</asp:ListItem>
<asp:ListItem>Check</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="GetTextBoxValue" />
</form>
</body>
</html>
CodeBehind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Reports
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox TB = new TextBox();
TB.ID = "abc";
form1.Controls.Add(TB);
Response.Write(Request.Form["abc"]);
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
createcontrol();
}
protected void createcontrol()
{
if (DropDownList1.SelectedValue.ToLower().Trim() == "text")
{
TextBox TB = new TextBox();
TB.ID = "abc";
form1.Controls.Add(TB);
}
}
}
}
When you use dynamic controls in .NET and want their values to be accessible after postback you need to create them within the Page_Init method of the page. In essence its not working because ViewState has already been set by the time you created the controls. See this guide for detailed info on this topic https://web.archive.org/web/20211020131055/https://www.4guysfromrolla.com/articles/081402-1.aspx . To fix the problem elevate your Textbox instantiation code to the Page_Init method and all should be well.
I need a value from javascript before the main page load and then want to use that value in code. The code which I am using for that purpose is:
I have make a test.aspx page. The code for which is as following:-
<%# Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
<!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>Untitled Page</title> <script type="text/javascript" language="javascript">
function GetScreenHeightAndWidth()
{
var width = screen.width;
var height = screen.height;
var object = 'Label1';
document.getElementById(object ).innerHTML=height ;
//alert(height);
'<%Session["Screensize"] = "' + height +'"; %>' ;
}
</script>
</head>
<body onload="GetScreenHeightAndWidth();" >
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="test"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
The code for test.aspx.cs page is as following:-
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Windows.Forms;
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Session["Screensize"] = Label1.Text;
TextBox1.Text=Session["Screensize"].ToString();
}
}
The result is as following:-
768 test
while result what I need is 768 and 768.
Is there any way to solve the problem?
What you are asking is impossible to do. Why do you need to know the screen size on the server -side in the first place? Whatever you need to accommodate on the html produced by the server can be either adjusted via proper CSS rules or Javascript (JQuery if you prefer that framework)
If you set the width/height values to a hidden field from script, you can than access them from codebehind after a postback. Labels are not posted with the form so you cant use that.
First of all thanks to all of you for responding to my question. After much search I have found what I was exactly wanting. For this I have to use one more page and add this code in my original code:
if (!IsPostBack)
{
if (Session["ScreenResolution"] == null)
{
Response.Redirect("detectscreen.aspx");
}
else
{
Session["Screensize"] = Session["ScreenResolution"].ToString();
}
TextBox1.Text = Session["Screensize"].ToString();
}
And the code for new page is:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["action"] != null) {
Session["ScreenResolution"] = Request.QueryString["res"].ToString();
Response.Redirect("Catalogue.aspx");
}
}
and the script in the new page will be like this:
<script language="javascript" type="text/javascript">
res = "&res="+screen.height
top.location.href="detectscreen.aspx?action=set"+res
</script>
I have a web page with an empty div, id="PreAcquisitionDiv" runat="server"
In the C# code-behind, on Page_Load, I build up a table and set it to PreAcquisitionDiv.InnerHtml
My page displays OK. I cause a PostBack, intending to change the data in the table. In the code, the PreAcquisitionDiv.InnerHtml does change, but the table I put there the first time is still displayed on the page instead.
I have seen some warnings about using InnerHtml at all, but the examples of alternatives that I have seen are JavaScript.
I am using IE 8, but I observe the same behavior in Chrome.
Thank you for any suggestions!
For example, this code displays "SomeStuff" when it first displays the page; then still "SomeStuff" after the PostBack even though the InnerHtml was changed.
protected void Page_Load( object sender, EventArgs e )
{
StringBuilder html = new StringBuilder( #"<TABLE BORDER='1' WIDTH='100%'>" );
if( !IsPostBack )
{
html.Append(#"<TR><TD>SomeStuff</TD></TR>" );
}
else
{
html.Append( #"<TR><TD>Some New Stuff</TD></TR>" );
}
html.Append( #"</TABLE>" );
this.PreAcquisitionDiv.InnerHtml = html.ToString( );
}
If you are changing the HTML on post back are you running your function to set it on post back as well?
UPDATE: I copied and pasted your code and it works fine for me. Here is my entire code behind:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
namespace Sandbox
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
StringBuilder html = new StringBuilder(#"<TABLE BORDER='1' WIDTH='100%'>");
if (!IsPostBack)
{
html.Append(#"<TR><TD>SomeStuff</TD></TR>");
}
else
{
html.Append(#"<TR><TD>Some New Stuff</TD></TR>");
}
html.Append(#"</TABLE>");
this.PreAcquisitionDiv.InnerHtml = html.ToString();
}
protected void btnTest_OnClick(object sender, EventArgs e)
{
}
}
}
And here is my entire ASPX page
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Sandbox._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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div id="PreAcquisitionDiv" runat="server">
</div>
<asp:Button ID="btnTest" runat="server" OnClick="btnTest_OnClick" />
</form>
</body>
</html>