How can I call a csharp variable in .aspx page? - c#

In code behind page I create a variable like this (it belongs to one of the class)
string login_status = "you are not logged in";
I want to show this variable value in my Default.aspx page. What do I do?

You can create a variable of type string with protected access modifier and call it on your web page using:
<%= login_status %>

Drop a label onto your aspx page:
<asp:Label ID="Label1" runat="server"></asp:Label>
then in the codebehind, say:
Label1.Text = login_status;

Add a label control onto the default.aspx page. It will automatically be named Label1.
From your code behind you can place your string into the label.
Label1.Text = login_status

http://haacked.com/archive/2007/02/15/asp.net_tip_-_use_the_label_control_correctly.aspx
you can use asp.Literal control instead of Label.

You should add a control to the page (label, literal) that will display this nicely and set it's value in the code behind.
You can also user runat="server" on any html tag to set it's inner html (etc) in the code behind.
Finally if you want to do it the 'quick' way you can put <%=login_status%> in the markup.

There are multiple ways you can do
First Variables you trying to access should be protected or public
Then you can access these variables like
<% Response.Write(login_status); %>
or
<%= login_status%>
or you can use control like asp control like labels and change its Text property on code behind

Related

how to send selected text of an HTML page which is inside an aspx panel to the same aspx page but another panel?

I have an aspx page with 2 panels (C# code-behind), (Panel1 & Panel2). Inside Panel1 I load an HTML page. Inside Panel2 there is an aspx textbox. I want to select some text of the HTML page in Panel1 and send it to the textbox of Panel2. Would you please say how to do it?
If you know what part you want to select, assign it an ID and a runat="server" value, then you will be able to access it from the code.
For example:
If you have something like this in your HTML:
<div id="divForCode" runat="server"></div>
Then you can access the contents of the div in code-behind using
divForCode.InnerHTML;
and
divForCode.InnerText;
So all you would need to do is send it to the textbox (myTextBox.Text = divForCode.InnerHTML; or myTextBox.Text = divForCode.InnerHtml;)
Sorry I had to post this as an answer, I don't have enough reputation to comment.

ASP.NET set hiddenfield a value in Javascript

I don't know how to set the value of a hiddenField in Javascript. Can somebody show me how to do this?
Javascript:
document.getElementById('hdntxtbxTaksit').value = "";
HTML:
<asp:HiddenField ID="hdntxtbxTaksit" runat="server" Value="" Visible="false"> </asp:HiddenField>
error : "Unable to get value of the property \'value\': object is null or undefined"
Prior to ASP.Net 4.0
ClientID
Get the client id generated in the page that uses Master page. As Master page is UserControl type, It will have its own Id and it treats the page as Child control and generates a different id with prefix like ctrl_.
This can be resolved by using <%= ControlName.ClientID %> in a page and can be assigned to any string or a javascript variables that can be referred later.
var myHidden=document.getElementById('<%= hdntxtbxTaksit.ClientID %>');
Asp.net server control id will be vary if you use Master page.
ASP.Net 4.0 +
ClientIDMode Property
Use this property to control how you want to generate the ID for you. For your case setting ClientIDMode="static" in page level will resolve the problem. The same thing can be applied at control level as well.
asp:HiddenField as:
<asp:HiddenField runat="server" ID="hfProduct" ClientIDMode="Static" />
js code:
$("#hfProduct").val("test")
and the code behind:
hfProduct.Value.ToString();
First you need to create the Hidden Field properly
<asp:HiddenField ID="hdntxtbxTaksit" runat="server"></asp:HiddenField>
Then you need to set value to the hidden field
If you aren't using Jquery you should use it:
document.getElementById("<%= hdntxtbxTaksit.ClientID %>").value = "test";
If you are using Jquery, this is how it should be:
$("#<%= hdntxtbxTaksit.ClientID %>").val("test");
document.getElementById('<%=hdntxtbxTaksit.ClientID%>').value
The id you set in server is the server id which is different from client id.
try this code:
$('hdntxtbxTaksit').val('test');
I suspect you need to use ClientID rather than the literal ID string in your JavaScript code, since you've marked the field as runat="server".
E.g., if your JavaScript code is in an aspx file (not a separate JavaScript file):
var val = document.getElementById('<%=hdntxtbxTaksit.ClientID%>').value;
If it's in a separate JavaScript file that isn't rendered by the ASP.Net stuff, you'll have to find it another way, such as by class.
My understanding is if you set controls.Visible = false during initial page load, it doesn't get rendered in the client response. My suggestion to solve your problem is
Don't use placeholder, judging from the scenario, you don't really need a placeholder, unless you need to dynamically add controls on the server side. Use div, without runat=server. You can always controls the visiblity of that div using css.
If you need to add controls dynamically later, use placeholder, but don't set visible = false. Placeholder won't have any display anyway, Set the visibility of that placeholder using css. Here's how to do it programmactically :
placeholderId.Attributes["style"] = "display:none";
Anyway, as other have stated, your problems occurs because once you set control.visible = false, it doesn't get rendered in the client response.
I will suggest you to use ClientID of HiddenField. first Register its client Id in any Javascript Variable from codebehind, then use it in clientside script. as:
.cs file code:
ClientScript.RegisterStartupScript(this.GetType(), "clientids", "var hdntxtbxTaksit=" + hdntxtbxTaksit.ClientID, true);
and then use following code in JS:
document.getElementById(hdntxtbxTaksit).value= "";
Try setting Javascript value as in document.getElementByName('hdntxtbxTaksit').value = '0';

ASP.NET Expression: Accessing the Property of a Class

Is there any way to directly access a field of class in the aspx page?
I have tried this:
In the aspx page I have added:
<form id="LoginForm" runat="server">
<asp:Label Text='<%# Eval("Test") %>' runat="server" ID="jym" />
</form>
and in the backend class of this page I have declared a property as:
private string test;
public string Test {
get {
return test;
}
set {
test = value;
}
}
This property is initialized in Page_Load() as: Test = "JYM";
But the problem is I am unable to see this value in the browser. The tag is rendered into <span/> but without any content.
What I am doing wrong?
Are you calling Page.DataBind() in Page_Load()?
You are using the # data binding operator in your embedded code block. The values that you bind to the control will only show after you have called DataBind() from either the control or the page (which will in turn call it for each control on the page).
If you only want to bind a value to that single label control you could just call jym.DataBind().
I would however suggest using a more descriptive name (id) for the Label control so it is clearer in the code about what is being databound.
See this page for more details.
UPDATE (extra info requested by OP in comment)
So you have 3 options:
1.
Call DataBind() as suggested above.
2.
Don't use a server side control for the label. Just use plain HTML and then you can use the following syntax:
<p><%= Test %></p>
3.
Set the value of the label in the code behind. For example in your page load you might have the following:
protected void Page_Load(object sender, EventArgs e)
{
jym.Text = Test;
}
You may want to use <%= this.Test%>. You can also do this.jym.Text=Test; in the Page Load evt.

Get codebehind to create a complete JS section

I want my codebehind to dump a very large set of JS into the ASPX page. This is required, as I can't use external JS code for this component, and the code is also unique to each customer. Is it possible to do this from codebehind? I know how to set the value of text boxes etc. (.Text/.Value = xxx) but I can't see how I can just 'dump' code straight onto the page.
have a look at Page.ClientScripts.RegisterStartupScriptBlock or RegisterScriptBlock, these method do exactly what you need.
Try using RegisterClientScript:
http://www.codeproject.com/KB/aspnet/Register_Client_Script.aspx
set a string variable into code behind and then add to the page just
<%= variableName %>
where you want your js code to be dumped to
Or if you like you can create a method and do the same thing
<%= methodName() %>

Setting a textbox text in usercontrol from page

I have a usercontrol which contains a textbox. I now want to add variable to that user control, lets call it Text, which will populate the textbox with the value passed in. I thought this could be done in the "set" of the variable in the code behind of the user control.
public string Text
{
get {}
set
{
txtBox.Text = value;
}
}
txtBox is the ID of the textbox within the usercontrol. Does anyone know how this can be done?
Thanks
Edit
The problem I suspect is that I am setting the text value in the Page_Load of the page hosting the user control. Where should I be setting this value?
If your problem is that txtBox is null then I can suggest you the following:
If you're creating a user control dynamically then don't forget to add it to the page's control tree BEFORE (implicitly or explicitly) accessing its child controls. Otherwise all these child controls will remain uninitialized:
MyUserControl ctrl = (MyUserControl )Page.LoadControl("~/MyUserControl.ascx");
base.Controls.Add(ctrl);
ctrl.Text = "some value";
If your user control is declared in the page's markup then don't forget to register using the Register directive with the "Src" property set to location of your user control:
<%# Register TagPrefix="controls" TagName="MyUserControl"
Src="~/MyUserControl.ascx" %>
<controls:MyUserControl id="ctrl1" Text="some value" runat="server" />
Registering the user control using the following technique WILL NOT work (not the case if all child controls are created dynamically. But then you don't need a user control - you just need a class derived from the Control class):
<%-- Will not work for user controls --%>
<%# Register Assembly="MyControlsAssembly.Shell" Namespace="MyControls"
TagPrefix="controls" %>
Hope this will help you.

Categories

Resources