i am trying to create a hyperlink from code-behind, but it is not creating it where i want it to be.
if i look at my souce code is creating somewhere else and from .aspx page it seems like everything is in place where it needs to be.
.aspx
<div class="AZ">
<ol class="AZlist">
<asp:PlaceHolder runat="server" ID="AToZ"></asp:PlaceHolder>
</ol>
</div>
.codebehind
HyperLink links = new HyperLink();
links.Text = "<li>" + CheckMe.ToString() + "</li>";
links.NavigateUrl = "<a href='#'" + CheckMe.ToString() + ">";
ph.Controls.Add(links);
source code:
....
....
...
<div class="AZ">
<ol class="AZlist">
// I WANT HYPERLINK HERE....!!!!!!!!!!!
</ol>
<br />
</div>
</li>
but its creating outside the div area here
<li>A</li>
I dont think you should put those tags in the .text and .navigateurl properties. just put the link and the text in them. Put the <li> tags around the placeholder.
Ditch the way you're doing it here. Try making your ol tag runat="server" and give it an id. Then create a new ListItem control, add the hyperlink control, and add the ListItem to the ol.
Something like this in the codebehind:
dim anLi = new ListItem([can't remember exact parameters])
dim aHyperlink = new Hyperlink([whatever to initialize])
anLi.addControl(aHyperlink)
theOl.controls.add(anLi)
Why not use an asp:repeater instead.
You could then put all your Mark up in the aspx template making use of header / detail / footer templating. Next just get the text and URL values into a bindable format in the code behind and data bind.
Saves doing markup in code behind and the need to dynamically insert controls which can cause headaches on post backs.
Related
I have a dynamically named DIV in a GridView which contains a user control with a dynamically assigned Parent_ID. The javascript is used to show or hide the DIV. I'll show you two examples of different rows without the ASP code.
Row 1 showing for Order # 123456:
<a href="<%#"javascript:collapseExpand('Order_Notes_Panel123456');" %>" >+</a>
<div id='Order_Notes_Panel123456' style="display:none;">
<uc:Comments_Control id="Comments_Control_ID" runat="server" Parent_ID='123456'/>
</div>
Row 2 showing for Order # 678901:
<a href="<%#"javascript:collapseExpand('Order_Notes_Panel678901');" %>" >+</a>
<div id='Order_Notes_Panel678901' style="display:none;">
<uc:Comments_Control id="Comments_Control_ID" runat="server" Parent_ID='678901'/>
</div>
The good news is that the user control binds and works perfectly. The javascript shows (sets the style to "display:block;") and hides (style set to "display:none;") the appropriate DIV each time the '+' is clicked.
Here is my problem: there is a 'Reply' link in the user control that, when clicked, does a post-back and puts the control into Edit mode. When I employ this user control on another page without a containing DIV, you won't notice a thing. However, when the 'Reply' does its post-back, the containing DIV reverts back to style="display:none;".
Can you provide a recommendation how to set the parent DIV's style to "display:block;" while a user is obviously working with it? I would imagine the appropriate code would go in the code behind of the user control when it goes into Edit mode.
Thanks,
Rob
Update: I recognize that there is no runat=server in my DIV. Since I'm trying to establish a dynamic ID for each, I get an error if I try to use the runat. That is probably the reason why I can't reach it from code behind...
I am very happy of myself... (see the YouTube video for this phrase, you'll be glad you did.)
In summary, this is what I added:
1. New Javascript function to add the name of the target DIV to a hidden field (The "collapseExpand" function is in the Site.Master. I couldn't put "load_div_to_hidden" in the Site.Master since "myhiddenField" isn't set up on every page
2. New hidden field to capture the name of the target DIV
3. New Javascript function to run on window.onload, check if we've got a post-back, and then display the value from the hidden field
4. Adding second Javascript call from the href in the link
Below are the new snippets of code:
<script type="text/javascript">
function load_div_to_hidden(obj) {
var hidden = document.getElementById('<%= myhiddenField.ClientID %>');
hidden.value = obj;
}
function windowOnLoad() {
var isPostBack = (('<%= IsPostBack %>').toLowerCase() == 'true') ? true : false;
if (isPostBack == true) {
var hid_field_value = document.getElementById('<%= myhiddenField.ClientID %>').value;
var right_div = document.getElementById(hid_field_value);
right_div.style.display = "block";
}
}
window.onload = windowOnLoad;
</script>
<input type="hidden" id="myhiddenField" runat="server" value="" />
<a href="<%#"javascript:collapseExpand('Order_Notes_Panel123456'); javascript:load_div_to_hidden('Order_Notes_Panel123456');" %>" >+</a>
<div id='Order_Notes_Panel123456' style="display:none;">
<uc:Comments_Control id="Comments_Control_ID" runat="server" Parent_ID='123456'/>
</div>
Works like a charm!
I have loaded my html template into my master page and then to bind category to database.
I have used this coding.
<ul class="categories">
<li id="categoryItem">
<h4>Categories</h4>
<ul class="categories" id="categorylist">
<asp:Repeater ID="repCategories" runat="server">
<HeaderTemplate><ul></HeaderTemplate>
<ItemTemplate>
<li>
<asp:HyperLink ID="hyperCategories" runat="server"><%#Eval("CategoryName")%></asp:HyperLink>
</li>
</ItemTemplate>
<FooterTemplate></ul></FooterTemplate>
</asp:Repeater>
</ul>
</li>
and try to bind this repeater to my database by doing coding on master.cs page.
if (!IsPostBack)
{
DataSet ds = new ViewAction().GetAllProductCategoryData();
repCategories.DataSource = ds;
repCategories.DataBind();
}
But it is showing the error that
"The name repCategories does not exist in the current context"
Why it is showing this error help me solve this. Please
The reason your code (as-written) isn't working, is because that Repeater is nested within 2 other server controls:
<li runat="server"> and
<ul class="categories" runat="server" id="categorylist">.
This means the Repeater is in a different "naming container" than your top-level elements, and isn't directly accessible from you masterpage's codebehind file.
To fix this, you need to either
Remove runat="server" from those controls (if you don't actually need to access them from the server side code). This will allow your code to work the way it is now. Or,
Add an ID to the <li> element, and then use the FindControl method to access your nested Repeater.
Option two would look something like this (we'll assume you gave the <li> an ID of "categoryItem"):
if (!IsPostBack)
{
// Get the Repeater from nested controls first
Repeater repCategories = (Repeater)categoryItem.FindControl("categorylist").FindControl("repCategories");
// Do the rest of your work
DataSet ds = new ViewAction().GetAllProductCategoryData();
repCategories.DataSource = ds;
repCategories.DataBind();
}
You need to use that code to "get" the Repeater in any place where you need to access it in codebehind.
I have a user control defined like this
<%# Control .....
<Test:MyCustomControl id="xxx" runat="server>
</Test:MyCustomControl>
I would like to use this control on a page like
<Tag:MyControl runat="server">
<div>
my html
</div>
</Tag...
In my custom control codebehind I would like to read the inner html and set it to a property of Test:MyCustomcontrol
Currently I am getting an error saying that "...does not have property div"
How can I do this?
Note: For clarification the inner html can be an arbitrary html, so I need a way to read anything that user has typed in the page.
you can expose the div(running on the server) as a property from your UserControl
on the usercontrol html:
<div id="dvSomething" runat="server"></div>
on ur usercontrol codebehind ".cs file":
public System.Web.UI.HtmlControls.HtmlGenericControl TheDiv
{
get
{
return dvSomething;
}
set {
dvSomething = value;
}
}
on the page that contains the usercontrol:
WebUserControl11.TheDiv.InnerHtml = "addin something to div from page";
good luck
I will keep the other question open in case someone need a different solution:
I'm not sure about your requeriments like but here are two solutions, I hope this is what you want:
one adding control(you can add any by the way, not only textbox) and other pure html as per you described
TextBox txtAdd = new TextBox();
txtAdd.Style.Add("width", "200px");
WebUserControl11.Controls.Add(txtAdd);
TextBox txtRead = (TextBox)WebUserControl11.Controls[1];
((System.Web.UI.HtmlControls.HtmlGenericControl)WebUserControl11.Controls[0]).InnerHtml = "<b>something</b>";
string currentHtml = ((System.Web.UI.HtmlControls.HtmlGenericControl)WebUserControl11.Controls[0]).InnerHtml;
of course, the index will change depending how many elements you have on your user control
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)
I have several <li> elements with different id's on ASP.NET page:
<li id="li1" class="class1">
<li id="li2" class="class1">
<li id="li3" class="class1">
and can change their class using JavaScript like this:
li1.className="class2"
But is there a way to change <li> element class using ASP.NET? It could be something like:
WebControl control = (WebControl)FindControl("li1");
control.CssClass="class2";
But FindControl() doesn't work as I expected. Any suggestions?
Thanks in advance!
Add
runat="server" in your HTML page
then use the attribute property in your asp.Net page like this
li1.Attributes["Class"] = "class1";
li2.Attributes["Class"] = "class2";
FindControl will work if you include runat="server" in the <li>
<li id="li1" runat="server">stuff</li>
Otherwise you server side code can't 'see' it.
The FindControl method searches for server controls. That is, it looks for controls with the attribute "runat" set to "server", as in:
<li runat="server ... ></li>
Because your <li> tags are not server controls, FindControl cannot find them. You can add the "runat" attribute to these controls or use ClientScript.RegisterStartupScript to include some client side script to manipulate the class, e.g.
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script language=\"javascript\">");
sb.Append("document.getElementById(\"li1\").className=\"newClass\";")
sb.Append("</script>");
ClientScript.RegisterStartupScript(this.GetType(), "MyScript", sb.ToString());
This will find the li element and set a CSS class on it.
using System.Web.UI.HtmlControls;
HtmlGenericControl liItem = (HtmlGenericControl) ctl.FindControl("liItemID");
liItem.Attributes.Add("class", "someCssClass");
Remember to add your runat="server" attribute as mentioned by others.
you must set runat="server" like:
<li id="li1" runat="server">stuff</li>
Please try this if you want to apply style:
li1.Style.Add("background-color", "black");
For CSS, you can try below syntax :
li1.Attributes.Add("class", "clsItem");
Leaf Dev provided the solution for this, but in the place of "ctl" you need to insert "Master".
It's working for me anyway:
using System.Web.UI.HtmlControls;
HtmlGenericControl liItem = (HtmlGenericControl) ctl.FindControl("liItemID");
liItem.Attributes.Add("class", "someCssClass");
You also can try this too if u want to add some few styles:
li1.Style.add("color","Blue");
li2.Style.add("text-decoration","line-through");