What is the server side control used to add an H4 to the markup from the code behind?
var h4 = new HtmlGenericControl("h4");
h4.InnerHtml = "Heading Text";
parentControl.Controls.Add(h4);
I recommend creating an HtmlGenericControl in your code-behind. The benefit of these over Literals is that they are proper HtmlControls, with the ability for you to programmatically set and modify properties such as InnerHtml, CssClass, Style etc.
HtmlGenericControl myH4 = new HtmlGenericControl("h4")
{
ID = "myH4",
InnerHtml = "Your Heading Here"
});
yourContainerControl.Controls.Add(myH4);
You could use an asp:Literal control - this just writes out the exact text that you set it to.
E.g:
Dim myLiteral as Literal = new Literal()
myLiteral.Text = "<h4>My Heading</h4>"
Then add your Literal to the page.
There is nothing like a <asp:H4 /> control. However, you can add any HTML element to a page via HtmlGenericControl type in your code behind.
For example, to create it:
HtmlGenericControl headerControl = new HtmlGenericControl(HtmlTextWriterTag.H4.ToString());
headerControl.ID = "myHeader";
headerControl.InnerHtml = "Hello World";
placeHolder.Controls.Add(headerControl);
To access it from code behind later:
HtmlGenericControl headerControl = FindControl("myHeader") as HtmlGenericControl;
Related
I've got the next situation: within an ITemplate object I'm rendering a Repeater with <li> elements inside, normally it's done like this:
Dim literalControl as new LiteralControl
literalControl = "<li>blah blah</li>"
container.Controls.Add(literalControl)
But at one point I need to add a PlaceHolder returned by another function which serves other purposes, not only this repeater and I get something like this:
Dim literalControl as new LiteralControl
Dim plTable As PlaceHolder
literalControl = "<li>blah blah</li>"
MyObject.Render(AComplexObect, plTable)
container.Controls.Add(literalControl)
container.Controls.Add(plTable)
This works just fine but elements in placehoder stay outside <li> tag, I want it inside. plTable has many elements of different types inside, so I loop converting it's content to Literal or LiteralControl throws error as not all elements can be converted to these types.
What alternatives do I have?
Thank you
Does this work for you:
Dim literalControl As New LiteralControl
Dim plTable As PlaceHolder
Dim swWriter As New StringWriter
Dim htwWriter As New HtmlTextWriter(swWriter)
MyObject.Render(AComplexObect, plTable)
MyObject.RenderControl(htwWriter)
literalControl.Text = "<li>" & swWriter.ToString() & "</li>"
container.Controls.Add(literalControl)
In my asp.net application , I have created HTMLAnchor class and HTMLImage class dynamically.
So that they bind the data from sqlserver at runtime. The code as below,
for (int i = 0; i < _dataset.Tables[0].Rows.Count; i++)
{
//Allows programmatic access to the HTML <a> element on the server.
HtmlAnchor URL = new HtmlAnchor();
URL.HRef = ConfigurationManager.AppSettings["MakerURL"].ToString();
URL.Attributes.Add("col-md-2 select_make", "url");
URL.Target = "_self";
URL.InnerText = "";
//Allows programmatic access to the HTML <img> element on the server.
HtmlImage htmlImage = new HtmlImage();
htmlImage.Src = _dataset.Tables[0].Rows[i][0].ToString();
//add htmlimage control to anchor control
URL.Controls.Add(htmlImage);
//add anchor control to div control
makeList.Controls.Add(URL);
}
So far so good!. Everything works fine. Now my requirement is when i click on image it redirect to another page where i need to show the image which i selected from home page.
How do i proceed with my above code.
Regards
Sangeetha
You may append the Id or name of image as query string in href of anchor.
Add the following to the PageLoad of CodeBehind of target page:
HtmlImage htmlImage = new HtmlImage();
htmlImage.Src = _dataset.Tables[0].Rows[i][0].ToString();
//add htmlimage control to anchor control
Page.Controls.Add(htmlImage);
or
AnyOtherControlInYourPage.Controls.Add(htmlImage);
I have written a code in which a textbox is dynamically added to a gridview cell. There is some default texts in the textbox. I want that when users will click on the textbox the default text will disappear and the user can then write anything on it only in number, i.e. the user will not be able to use letters or special characters.
Kindly let me know how to achieve this.
Code example
Gridview gv=new Gridview();
gv.DataSource=dt;
gv.DataBind();
Textbox t1 = new Textbox();
t1.Text="Outages if any(in mins)";
gv.Rows[0].Cells[0].Controls.Add(t1);
Need help after this, something like when user puts his cursor in the textbox , the default text will disappear , and if the user removes the cursor without writing anything , the default text will reappear. Also the default text should be a bit blurred
Thanks.
Try something like this
Textbox t1 = new Textbox();
t1.Attributes.Add("onclick", "if(this.value == 'default text') this.value = '';"
t1.Attributes.Add("onblur", "if(this.value == '') this.value = 'default text';" />
You could also use onfocus in case users use tab key
With this, using this.value, approach you don't need to know the client ID of the control.
Here is a post describing exactly what you are looking to do:
HowTo: including default text in a Textbox while enforcing server-side validation
The pertinent points are:
Adding javascript attribute to onfocus & and onblur:
txtName.Attributes.Add("onfocus","clearText()");
txtName.Attributes.Add("onblur","resetText()");
Adding the javascript to clear and repopulate the textbox:
function clearText() {
document.form1.txtName.value = ""
}
function resetText() {
if(document.form1.txtName.value == "")
document.form1.txtName.value = "(enter something here)"
You can do this. The easiest way is to use the ajaxControlToolkit. You can create controls dynamically. For example:
Dim mt As new TextBox
Dim newTest As New AjaxControlToolkit.TextBoxWatermarkExtender
With mt
.ID = "textBox1"
.TextMode = TextBoxMode.SingleLine
End With
With newTest
.ID = "TextBoxWatermarkExtender1"
.TargetControlID = mt.ClientID
.WatermarkText = "test"
End With
Then just add both controls to the gridview as you are doing with the textbox. If you are not using Ajax, you can add javascript to the control through codebehind but this is more difficult. Let me know if that is what you want to do and Ill add some code to show you how.
This should create a textbox control with an associated ajax TextBoxWaterMarkExtender.
How can I add a custom attribute to a HiddenValueField in ASP.NET? Specifically, I need a class="gmapPoint" attribute for a dynamically generated HiddenValue control. This is necessary for a JavaScript on that page
var hiddenField = new HiddenField();
hfield.Value = "myValue";
hfield.... Attributes["class"]
-- how can I do a similar thing?
Use HtmlInputHidden control instead of HiddenField. It allows programmatic access to the HTML <input type=hidden> element on the server and has .Attributes property:
var hiddenField = new HtmlInputHidden();
hiddenField.Value = "myValue";
hiddenField.Attributes["class"] = "a-class-for-a-hidden-field";
I have a label and I want to add to it a link.
I want to use javascript like :
MyLabel.Attributes.Add("`onclick`", "javascript:`SOME_CODE`")
What must I add in (SOME_CODE) to redirect the user to another link.
Thanks.
Have you tried: window.location = 'http://google.com' ?
Are the any particular reason you want to use Javascript for this, and not just the HyperLink Control?
Update:
You can either use a normal a-tag link or use the ASP.Net HyperLink control:
This is the markup:
<asp:HyperLink ID="MyHyperLinkControl" NavigateUrl="http://google.com" runat="server" />
This is if you want to add it from the code-behind:
HyperLink link = new HyperLink();
link.NavigateUrl = "http://google.com";
parentControl.Controls.Add(link);
Where parentControl, is the container you want to add it to, for instance a cell in a table or a panel.
See here for more information on how to add a control to a panel
Just use a plain anchor tag (<a >), but put the label inside the anchor (the reverse is not strictly valid html). If you don't want it to show up as a link every time, you can accomplish that by omitting the href attribute. This is easy to do with a normal <asp:HyperLink> server control like so:
<asp:HyperLink id="..." runat="server"><asp:Label ... ></asp:Label></asp:HyperLink>
Now, the href attribute will only render if you actually set the NavigateUrl property in your code. You might also find that using an <asp:HyperLink> completely replaces the need for the label.
<a href="http://google.com" >Go to Google</a>
If this has anything to do with your previous question, use a Hyperlink control instead of a Label:
Dim Hyperlink1 As New Hyperlink
Hyperlink1.Text = "XYZ"
Hyperlink1.NavigateUrl = "http://www.google.com"
Dim Literal1 As New Literal
Literal1.Text = "<br />"
' Add the control to the placeholder
PlaceHolder1.Controls.Add(Hyperlink1)
PlaceHolder1.Controls.Add(Literal1)