Dispay html textbox with value from code behind using c# - c#

How can I display an html textbox with value obtained from code behinh?

As long as your string value is not private, you should be able to do something like this
<input type="text" value="<%= YourStringValue %>" />

You could use an actual <asp:Textbox /> and set it's "text" value directly from the code behind. If you want to directly inject text into a "normal" html textbox (or anywhere else for that matter), you can use <%= SomeValue %>. Yet another way is to include the "runat=server" attribute on standard html elements, allowing you to manipulate them from the codebehind.
Normally I'd just go for the built-in ASP textbox control so I don't have to worry about persisting values/wiring up viewstate/etc. Injecting dynamic content into plain html elements tends to be an edge case requirement...

<%# Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
//assign text value here
txt1.Value = "hello world!";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="txt1" runat="server" type="text" />
</div>
</form>
</body>
</html>

Related

asp:Button how to capture Response.WriteLine

I am using the OnClick of the asp:Button tag to compute a report which I would like to display as an innerHTML of a div tag. I would like to use the Response.WriteLine method to return this html file. Does ASP.NET webforms provide a way of capturing this response?
Am I forced to use JavaScript instead? I am using C# on the Server side.
The Web Forms way of doing this is to use a control and set its InnerHTML from the code behind. Here's a self contained example:
<%#Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
protected void GenerateReport(object sender, EventArgs e)
{
var reportHTML = "<p>This is a <span style='font-weight: bold;'>report!</span></p>";
ReportDiv.InnerHtml = reportHTML;
}
</script>
<!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>
<title>Test</title>
</head>
<body>
<form runat="server">
<asp:Button runat="server" OnClick="GenerateReport" Text="Generate Report" />
<div runat="server" id="ReportDiv"></div>
</form>
</body>
</html>

having an asp back button in popup window for displaying pdf

Am having a popup window for displaying pdf.. but i need to put an asp button in pop up window have tried the following code but could not find a solution.. can any one resolve this?
enter code here
<asp Page attributes>
...............
<!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>
<br />
<form id="form1" runat="server">
<div> <asp:Button ID="dd" runat="server" OnClick="Backonclick" Text="back"/>
</div>
<div></div>
</form>
</body>
</html>
on page load event
dd.Visible = true;
string path = Request.QueryString["val"].ToString();
string extention = Path.GetExtension(path);
int len = extention.Length - 1;
string extwithoutdot = extention.Substring(1, len);
if (extwithoutdot.Equals("JPG") || extwithoutdot.Equals("jpg") ||
extwithoutdot.Equals("jpeg") || extwithoutdot.Equals("JPEG"))
{
extwithoutdot = "jpeg";
...........
The issue here is that you're mixing two separate types of documents; a PDF is one type of download, whilst an HTML is another. A single download can't unfortunately be both, and you can't switch document types part-way through and expect the browser to handle it.
The simplest solution for what you're after is to combine the two on the client side. Load your pop-up as an HTML page (generated by ASP.NET if you wish) and include within that an IFRAME linking to your PDF generation script; something of the form:
<html>
<body>
<a class='button' href="javascript:window.close()">Close</a>
<iframe src="generatePdf.aspx" height="300" width="300" />
</body>
</html>
i have got solution
<form id="form1" runat="server">
<iframe src="Copy (2) of DisplayPdf.aspx" width="1000" height="400">
</iframe>
<iframe src="Copy of DisplayPdf.aspx" width="100" height="200">
</iframe>
</form>

Load .ascx from string (SQL)

I like to to ask that is there a way to load an ascx control from string? To be more precise: I like to make/store a new .ascx in sql then load it to the website.
Yes, you should be able to use the ParseControl function to do what you want:
http://msdn.microsoft.com/en-us/library/kz3ffe28.aspx
Example from MSDN:
<%# Page language="c#" Debug="true" %>
<!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>
<title>ASP.NET Example</title>
<script runat="server">
// System.Web.UI.TemplateControl.ParserControl;
// The following example demonstrates the method 'ParseControl' of class TemplateControl.
// Since TemplateControl is abstract, this sample has been written using 'Page' class which derives from
// 'TemplateControl' class.
// A button object is created by passing a string to contstruct a button using ASP syntax, to the
// 'ParseControl' method. This button is added as one of the child controls of the page and displayed.
void Page_Load(object sender, System.EventArgs e)
{
Control c = ParseControl("<asp:button text='Click here!' runat='server' />");
myPlaceholder.Controls.Add(c);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:placeholder id ="myPlaceholder" runat="server" />
</form>
</body>
</html>

Using UrlEncode in HyperLink Button in .NET and in Anchor Tag of HTML

I am just trying to use UrlEncode in a Hyperlink which is in GridView and found it was not working. Then I tried to take the HyperLink as a separate control and tried with that. It is not giving Hyperlink to me, I mean it is not even clickable.
While when I tried with simple Anchor tag, it is working. This is what I am using
<asp:HyperLink ID="HyperLink2" runat="server"
NavigateUrl ='<%= "~/Default.aspx?customer=" + "&CompanyName=" + Server.UrlEncode("abc#")%>' > wc
</asp:HyperLink>
// While following is working
<a title="asxd" href='<%= "~/Default.aspx?customer=" + "&CompanyName=" + Server.UrlEncode("abc#")%>'>wc
</a>
Still looking for the answer
You don't need to use a Hyperlink control or make an anchor tag runat="server" unless you're doing something to it in your code behind.
wc
You are missing the text property of the hyperlink control.
<asp:HyperLink ID="HyperLink2" runat="server"
NavigateUrl ='<%= "~/Default.aspx?customer=" + "&CompanyName=" + Server.UrlEncode("abc#")%>' Text="wc" />
You also can set NavigateUrl property in code-behind file, in Page_Load event handler, for instance. It will work.
In the code-behind class:
protected void Page_Load(object sender, EventArgs e)
{
HyperLink2.NavigateUrl = "~/Default.aspx?customer=&CompanyName=" + Server.UrlEncode("abc#");
}
And in markup:
<%# 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:HyperLink ID="HyperLink2" runat="server" Target="_new">wc
</asp:HyperLink>
</div>
</form>
</body>
</html>
<%= ... %>
doesn't work inside ASP.NET controls.
Alternatives:
<%# ... > works in some cases in data-bound controls
code-behind

Encountering Error when doing post-back using __doPostBack()

I am getting an error on javascript when doing post back. The code is as follows:
<%# 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 id="Head1" runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function DoPostBack()
{
__doPostBack('Button2','My Argument');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<input type="button" id="Button2" value="Press me" onclick="DoPostBack()" />
</form>
</body>
</html>
I am getting the following error:
Line: 13
Error: Object expected
I can't understand why this error is coming. Kindly help...
you can use a hidden button to do this task
Button1.Attributes.CssAttributes.Add("Display","None");
after hiding the button
you can call its click function from javascript
document.getElementById('<%=Button1.ClientID%>').click();
this will call Button1_Click on server
** remember to set UseSubmitBehaviour=false to make this work on non-IE browsers
hope that helps :)
__doPostBack is not created by default. If the page does not have a control that causes a postback then ASP.NET does not create/generate this method.
In your case you can force ASP.NET to generate __doPostBack by adding the following line in you Page_Load event:
ClientScript.GetPostBackEventReference(this, string.Empty);
This line will force the creation of this method.
_doPostBack isn't created by default. It appears when you are adding control with autoPostBack=true or adding some grid with buttons in it.
So there is no _doPostBack javascript generated in your code.
If you add
<asp:DropDownList ID="list" runat="server" AutoPostBack="true">
<asp:ListItem Text="first"></asp:ListItem>
<asp:ListItem Text="second"></asp:ListItem>
</asp:DropDownList>
for instance your code will work.
Don't know if it is really useful code :) however.

Categories

Resources