I programatically create li elements, however I need some of them disabled, so far I have:
HtmlGenericControl htmlLi = new HtmlGenericControl("li");
htmlLi.InnerText = row["name"].ToString();
tab_content.FindControl("tab_content_" + row["stars"].ToString()).Controls.Add(htmlLi);
Is it possible to disable them? If not, is their any alternatives?
An <li> element can't be disabled, as there's no input to prevent.
You can however hide the elements you don't want to be displayed.
You can either hide them from the code-behind:
htmlLi.Visible = false;
or you can add some styling to it so that it is put into the DOM, but made invisible by the CSS (so that you can make it visible again with javascript, should you want to):
htmlLi.Attributes.Add("style", "display: none");
or
htmlLi.Attributes.Add("class", "SomeInvisibleCssClass");
Related
I have a problem with creating dynamic <li> elements from codebehind. I need to assign runat server to li, but I didnt find a way to assign runat server, so I can't find that li control when I need to change the attributes from code behind. Is there any answer to my problem? I am new to asp.net c#.
Here is my code:
<ul class="nav nav-tabs" runat="server" id="tabList">
//First i got ul control that i assign runat=server in aspx page
</ul>
//then i create li from code behind in Page_Init()
System.Web.UI.HtmlControls.HtmlGenericControl tab = new System.Web.UI.HtmlControls.HtmlGenericControl("li");
tab.ID = "tab" + (i + 1);
tab.Attributes.Add("runat", "server");//this is not working
tab.Controls.Add(new LiteralControl("Penumpang " + (i + 1) + ""));
//then i add the li to my ul controler called tablist
this.tabList.Controls.Add(tab);
My problem is, when the page loads, I can see the li on the page, but I cannot call li from the code behind when i need to do something with it. Is there any way to call the li in code behind? Or change the li attributes when it is assigned dynamically? Sorry for bad English.
Thanks in advance.
The reason why you cannot access the <li> in your code-behind is because it is dynamically generated.
Dynamically generated controls lose their state when they are rendered on the view and for you to access them again in your code-behind when a postback occurs, you need to recreate them before playing with them. So basically, you need to recreate them everytime on your postback to access their properties and values and to manipulate with them.
Also, runat='server' wont work from code-behind. Instead of using <li>, you could also try using some ASP.NET controls such as Listview or other data-binding controls.
Hope this helps.
I am wondering Is it Possible to disable all controls including validators inside a div using code behind in C#, like we do in jQuery.
The code I tried is
var controls = articlePickupDateTime.Controls.GetEnumerator();
while (controls.MoveNext())
{
var item = controls.Current;
}
This was returning everything inside div including span, etc.
RequiredFieldValidator1.Enabled = false;
'RequiredFieldValidator1' the id of validator control.
Put all your controls in Panel Control and set
YourPanel.Enabled = false;
I try to simply add a div wrapper around every control of type <asp:DropDownList> at a global level (if possible). At this point I have solved it with a asp skin adding "default-select" class to all <asp:DropDownList> and then jquery just adding a div for each of them.
$j(".default-select").wrap("<div class='myClass'></div>");
Now, my question. Is it possible to add this div wrapper from the code-behind instead of relying on a javascript.
Control Adapter:
I know this should be possible by writing a control adapter that override <asp:DropDownList> render method (as described here: Dropdownlist control with <optgroup>s for asp.net (webforms)?). However, I just want to add a wrapping div, not rewrite the entire rendering of the <asp:DropDownList> control (which I have to do if i override the method?). Any suggestions? Maybe there is a way to just add something to the existing adapter??
Custom User Control: Another solution would be to build a custom <mycustom:DropDownList> with the wrapping, but, this would force me to replace every instance of <asp:DropDownList> trough the whole project (large project). I rather just change the original control some how so that my styling applies everywhere.
So summary: Is there an easy way to just make all <asp:DropDownList> render as:
<div class="myClass">
<select><option...></select>
</div>
instead of just:
<select><option...></select>
My first attempt (on Page load): I tried to add this code in the Page_load method but I don't find any way to render that div out?
var j = 0;
foreach (DropDownList control in Page.Controls.OfType<DropDownList>())
{
HtmlGenericControl div = new HtmlGenericControl();
div.ID = "div" +j;
div.TagName = "div";
div.Attributes["class"] = "myClass";
div.Controls.Add(control); // or control.Controls.Add(div); but this wouldn't wrap it.
j++;
}
Your solution just about works. Server control can only exist within the scope of a server form, you will need to perform a recursive search on the page or look directly in the form controls collection. Once you have the DropDownList and wrapped it around a div container it will need to be added to the controls collection.
Also, I think it better to perform this in OnPreInit.
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
var j = 0;
foreach (DropDownList control in form1.Controls.OfType<DropDownList>().ToList())
{
var div = new HtmlGenericControl();
div.ID = "div" + j;
div.TagName = "div";
div.Attributes["class"] = "myClass";
div.Controls.Add(control); // or control.Controls.Add(div); but this wouldn't wrap it.
j++;
form1.Controls.Add(div);
}
}
I hope this is helpful. Let me know how you get on.
Either way what you're wanting is custom and you have to code for it. So IMO, the best and simplest option is the custom control. You may have to spend some time refactoring references to replace your <asp:DropDownList>, but in all the time you've spent trying another way, you could've been done by now. :)
I've learned the hard way that keeping it simple is usually the best way to go.
How to make div visible & not visible after clicking button in MVC3.
You could also check the status of the div, whether is hidden or visible, and use the same button to show or hide it. In addition, you can change the caption of the button accordingly:
$('#button1').click(function() {
if ($('#id1').is(':hidden')) {
$('#id1').show();
$('#button1').val('hide');
} else {
$('#id1').hide();
$('#button1').val('show');
}
});
Visible/Invisible and at the same time removing the space that the element occupies in the page
$('#someid').toggle(); // to toggle between visible/invisible
or $('#someid').show(); and $('#someid').hide();
If you want to make visible/invisible but maintain the space the element occupies then use $('#someid').css({visibility:'hidden'}); and $('#someid').css({visibility:'visible'});
But the most correct way in both cases is to create a css class and add that class or remove it from the element
CSS rule
.hidden{ display:none; }
and use $('#someid').addClass('hidden') and $('#someid').removeClass('hidden')
Asp.net-MVC uses jQuery be default, so this is the jQuery version:
$('#buttonId').click(function(){
$('#divId').toggle();
});
Better do it in client side with Javasctipt
//to hide element
document.getElementById("MyElement").style.display = "none";
//to show element
document.getElementById("MyElement").style.display = "inline";
// where MyElement is id of your div
I want to create a DIV dynamically and place it into another DIV when a button is clicked.
So far I've managed to create a new DIV on button_click, however I don't know how to place the new DIV object into a specific DIV tag as a child.
Here's my code which is within the button's click event.
System.Web.UI.HtmlControls.HtmlGenericControl dynDiv = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
dynDiv.ID = "dynDivCode" + i.ToString();
dynDiv.Style.Add(HtmlTextWriterStyle.BackgroundColor, "Gray");
dynDiv.Style.Add(HtmlTextWriterStyle.Height, "20px");
dynDiv.Style.Add(HtmlTextWriterStyle.Width, "300px");
dynDiv.InnerHtml = "I was created using Code Behind";
this.Controls.Add(dynDiv);
Any suggestions please?
thanks
Your specific existing div should be server control. For example: Panel. Then you can add as a child to the Controls collection of the panel.