Assign database value to hyperlink but not a runat=server - asp.net

Its simple if I have an ASP.net page with an ASP.net linkbutton / hyperlink
and I obtain a value from say a SQL Database and I store it in the label...
For example:
this.myLabel.Text = someValueReturnedFromADatabase
This is simple because it goes right to the code behind page and set the text value
to the value returned from my database (aside from going into more details with data access layer, etc).
What I was wondering is what if I dont want to use an ASP.net linkbutton and I simply want to use an HTML link button (as I need to call the jquery fade function). How would I set the value someValueReturnedFromADatabase to a control that is not runat=server?

Have a variable in your codebehind:
protected string TextForLabel
Set it in Page_Load, or wherever:
TextForLabel = someValueReturnedFromADatabase;
Reference it with pointy-bracket percent notation:
<% =TextForLabel %>

You can set runat="server" on standard html controls. I do it all the time. Then you will be able to access their properties in your code behind just like you do for asp controls.
If I am not mistaken, for labels you can use .InnerText or .InnerHTML to change the text.

From what I know of, you need to make some sort of relation between your HTML document and the code behind to interact with data from a SQL database. Either that or you'll have to make the entire database connection etc. in the HTML header using script type="text/javascript" and script type="text/C#" or whatever language you use to develop.

Related

how to display variable value in asp.net which is set in Page_Load function

I have a variable which I get from database I want to output this variable in the aspx page in between the html.
I tried to make it public but it shows some error, how can I use a variable set in page load function in its aspx page?
First way
You place a Literal control in the point you want to display the variable and then you set on PageLoad this value to the Literal Control
<asp:Literal runat="server" id="txtValueA" EnableViewState="false" />
and on code behind.
txtValueA.Text = "one of the basic";
This way you have also more control over what you try to render out.
Second way
The other way is to make it public as you say and print it when the page render. This is a different way, but not a better one.
public string cMyValue = "some string here";
and on aspx page
<%=cMyValue%>
This way you get the parameter when the page renders at run time and send this in run time on the Client.
If you try this way inside an updatepanel, then the update is fail because the update panel can not read and render again the full page but only the code behind.
Try to avoid this way, and use it only when you really needed it because this make a call to the code the moment its try to render the page and change the page...
One idea to use this way is when you have some extra calculation that you like to make, you flush the content and then you call a function with extra time cost. For example.
<%
Response.Flush();
Response.Write(CallATimeConsumeFunctionThatReturnString());
%>

Asp.net yellow code <% vs Explicit asp control

I have a literal in my aspx called xxx.
Now lets go into the JS world.
I always used :
alert('audit.aspx?claim_id=<%= xxx.Text%>');
But Ive seen a code like this :
alert('audit.aspx?claim_id=<asp:Literal id="xxx" runat="server" />');
This is also working.
Can I conclude that the <asp:Literal is equal to <%= syntax ?
I know that he is a RUNAT server Item...
but again - I want to see the differences.
The asp:Literal control simply outputs the value of its Text property when the page is rendered. That's why the resulting JavaScript looks the same when viewed by the client. But the two are not the same, no.
<%= xxx.Text %> explicitly reads the value of this text property and writes it out. The Literal control will be elsewhere on the page, where its Text property will also be written out.
Placing the asp:Literal control within your JavaScript relies on the rendering of this control to place the value there, and because this is its location within your page, there's no need to have the same content rendered elsewhere.
However, neither taken in isolation seems an appropriate use of this control to my mind. If you have simple text you want written out, then expose it as a property of your Page-derived code-behind class.
The Literal class is basically a placeholder for text, but it also exposes events which you can hook into for greater control.
Have a look at the API here http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.literal.aspx

What control to render dynamic HTML text on an aspx page

Page_Load generates a string of HTML for a dashboard. (html)
What control on an aspx page to bind that "text" to so when the page renders you see the tables, and buttons within?
Tried
With dhtml.Text = html but I don't see the buttons. I do see the tables as well as the borders of cells that I expect.
Any ideas?
TIA
You can inject any text/html into your ASPX page using: <% =GetMyText() %> where "GetMyText()" is a public or protected method in your code behind that returns a string.
You can also drop a Literal control onto a form and set the text via its "Text" property.
But if you want to do things the ASP.NET way, you might use a Gridview or Repeater to display tabular/repeating data, and Databind to it with some data.
If you are starting out with ASP.NET, you would probably be better off learning ASP.NET MVC as it is easier to get your head around if you are used to writing HTML. ASP.NET Web Forms, which you are using, generally tries to insulate you from HTML, CSS, and Javascript by giving you controls that you drop onto the page and bind data to. The controls do a lot of work for you, but take away almost all control of your HTML, CSS and Javascript.
I use javascript to dynamically create html elements. Your page_load function could register a javascript function which creates the elements you need.
Not sure why you were downvoted, but a very simple one to use is the HtmlGenericControl.
Basically, just add a span or div to your .aspx file and give it an ID and the runat="server" attribute.
Then, in your code behind just set the InnerHtml property of that control to your generated html.

How to change html view of web user control

I am creating a web user control for a simple poll. I am currently registering it on the page and then referencing it via tagprefix.
The form for the poll is in basic html (no server controls) and is in the front-end of the web control. How can I change the look of the user control depending on the settings passed into it? Is this possible without using server controls?
Update
Can I change the html layout of a user control? If so could someone post some examples. Please note I do not use asp.net form controls, so none of that please :)
You might be able to also use jQuery to replace existing css setting in your code. Create properties on for your user control, and then pass settings in the classes. Then use jQuery to replace them. This however requires jQuery to be linked to your page (or within your control) and you'd have to write the CSS classes out to the jQuery code (using server controls, but you could use the literal control so there's no excess code).
Personally I'd go with the option of using server controls instead of straight up HTML, you'd get alot more flexibility, and then passing through the settings would be pretty straightforward, put something like this in your controls backend code:
Private _TextBoxCssClass As String
Public Property TextBoxCssClass() As String
Get
Return _TextBoxCssClass
End Get
Set(ByVal value As String)
_TextBoxCssClass = value
txtBox1.CssClass = value
txtBox2.CssClass = value
End Set
End Property
You most likely want to have a property or event in the control that changes the css. It may end up best to add some server controls or javascript / jquery to make it easier.
If its only the styles you want to change, then you can expose a property to set the style attribuites of the respective control inside your User Control. If you want to control the whole HTML layout of the control then Custom Control is the viable option.

Access html control from server side

How can i access html control from code behind in asp.net. I do not want to use "runat=server" as it is causing problems.
I have made an empty html table and from my code behind, I want to add <td> to it.
Please help.
Thanks,
Prachi
This is not possible without runat="server".
You say this was causing problems. Please say what problems it was causing.
You say you were accessing the table using getElementById? The ID of a server control changes based on what controls it's inside of. You need to get the changed ID to use:
var tab = getElementById("<%= myTable.ClientId %>");
or something to that effect.
You should use runat=server attribute if you want to access it at code behind.
But maybe you can generate some javascript code that adds the td/tr to your table at codebehind. and you can register it yo your page to run at startup.
runat="server"
to the table and the tr elements, there is no way the code behind can access them (as this tells the frame work to expose the elements to the server).
What are you actually trying to do? Have you looked at either DataGrid, GridView or simple Repeater control? These allow you to define a table structure and dynamically add controls to it (usually through data binding, although there are ways to add items to thier ItemCollections).
one way that you could do this is to add some inline scripting and a public string variable. in your code behind, make a public class-level variable:
public String myColumns
then in your Page_Load event, create your HTML as a string and save it in your myColumns variable:
protected void Page_Load() {
//do stuff
myColumns = someStringWithTDTagsInIt
}
Then in your .aspx page, do the following:
<table id=maintable"><tr><%=myColumns %></tr></table>
It should render your HTML as you wanted it to in your Page_Load event (or whatever event you want, if you want it to load on a button click or something) without using a "runat=server" tag.
Please note that this is not the recommended way to achieve this (controls are typically more efficient) but this should be a valid solution to the question you posed.
simple example.
just you place the runat="server" then you can access .
html controls are cannot access at server side. if you place runat server then it access in the server.

Resources