I am using this div inside a repeater.
<div class="view" id="View">
I want this div control in asp.net c# code. This is my code now:
HtmlGenericControl h3name = null;
h3name = (HtmlGenericControl)RepeaterView2.Items[count].FindControl("View");
h3name.Style.Add("width", CHeight.ToString());
But I did not get the control. How can I solve this problem?
HtmlGenericControl defines the methods, properties, and events for all HTML server control elements not represented by a specific .NET Framework class.
So if its a server control you can simply use:
HtmlGenericControl sample= e.Item.FindControl("YOURDIVNAME") as HtmlGenericControl;
and also add runet="server" to your div.
First add a runat server in your div
<div class="view" id="View" runat="server">
now in item bound event of your repeater find it like
HtmlGenericControl sample= e.Item.FindControl("View") as HtmlGenericControl
Your div must be with runat = "server" attribute.
Related
I have created some div, label and checkbox using htmlgenericcontrol with runat="server". these dynamically created div is added inside the main div which is created at the design time.
Now, i want to access the checkbox which is there inside dynamically created div using c# on page load.
my dynamic div created using c# looks like this -
<div id='firstdiv'>
<div id='seconddiv'>
<label id='lbl' >
<input type="checkbox" id="chk" value="check" runat="server">
<div id='thirddiv'></div>
</label>
</div></div>
I tried below options but none of them worked -
HtmlGenericControl chkfirstdiv =
HtmlGenericControl)maindiv.FindControl(strchkfirstdiv) as
HtmlGenericControl;
HtmlGenericControl chkfirstdiv1 =
HtmlGenericControl)FindControl(strchkfirstdiv) as HtmlGenericControl;
LiteralControl literalControl =
LiteralControl)maindiv.FindControl(strchkfirstdiv);
Can anyone help in accessing the dynamic checkbox inside dynamic div?
For WebForms, you should create the control as <asp:Checkbox Id="MyCheckbox" runat="server" Text="Check" /> and the designer should generate Checkbox MyCheckbox for you and it's Checked property will be populated on postback without the need to call FindControl(..).
asp buttons will be accessed based on their names not IDs so place a name attribute inside checkbox definition like <asp:Checkbox Id="MyCheckbox" name="myCheckBox" runat="server" Text="Check" /> and access myCheckBox in backend on click event
I have an ASP.NET site that I am trying to access div elements by their ID from the C# code behind file. Essentially I want to see if a div element exists, and if so, alter its properties.
I've found many resources out there that point to a dozen different solutions and none of them seem to work.
HTML on ASP.Net Page:
<div class="contentArea">
<div class="block" id="button1" runat="server">
Some Content Here
</div>
<div class="block" id="button2" runat="server">
Some Content Here
</div>
<div class="block" id="button3" runat="server">
Some Content Here
</div>
</div>
C# Code Behind (examples I've tried):
System.Web.UI.HtmlControls.HtmlGenericControl div1 = (System.Web.UI.HtmlControls.HtmlGenericControl)this.FindControl("button1");
div1.Attributes["class"] = "classNameHere";
or
Control div1 = this.FindControl("button1");
div1.GetType();
When the code gets to the second line of each of the above examples, I get an error:
Object reference not set to an instance of an object.
If I try the following:
if (div1 != null)
{
// Do Something;
}
Nothing ever happens because div1 is always set to null. Ironically, if I look at the Locals window and examine this, I can see the button# ids in the listing, so I know they are there, but the system is acting like it isn't finding the control.
My ultimate goal is to find the max id # of the button divs (looking at my html example, the max id would be 3 (button3). Maybe there is a better way to go about it, but either way, once I have my max id, I want to be able to touch each div and alter some css properties.
Although I could easily do all of this via jQuery, in this instance I need to do this in C#.
Any help is much appreciated. If you need more info, let me know.
UPDATE
I created a new C# web project from scratch. After adding a masterpage (and not altering it) and adding a webform using masterpage, I only added one line to the webform under Content ID="Content2":
<div id="button1"></div>
From c# code behind I still run into the same exact issue as before.
FINAL UPDATE AND ANSWER
I'm shocked no one (including myself) caught my mistake from the above update. I never put runat="server" when I created a new project from scratch under the div. Here is how I fixed my problem under my new project from scratch:
Add runat="server" to div:
<div id="button1" runat="server"></div>
Then I did a FindControl on the ContentPlaceHolder under the MasterPage:
ContentPlaceHolder myPlaceHolder = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
Note: This is what the ContentPlaceHolder code looks like on the Site.Master page created by default:
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
After finding this ContentPlaceHolder in code behind, I then searched within this placeholder for button1:
using System.Web.UI.HtmlControls;
HtmlControl myControl = (HtmlControl)myPlaceHolder.FindControl("button1");
Finally I check to see if myControl is null:
if (myControl != null)
{
\\ Do Something
}
When I ran this code, it found the div I was looking for. Here is the complete code behind all put together:
using System.Web.UI.HtmlControls;
ContentPlaceHolder myPlaceHolder = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
HtmlControl myControl = (HtmlControl)myPlaceHolder.FindControl("button1");
if (myControl != null)
{
// Do Something
}
If your page is using a MasterPage, the div control will not be in the main collection of controls. That collection only contains the Content controls pointing to the ContentPlaceholder of your MasterPage.
There are three options:
Use FindControl on the Content control: contentControl.FindControl("button1");
Do a recursive FindControl until you find the control you need
Normally, a declaration of your div control is added to your designer.cs codebehind, so you can directly access the control by its name: button1.Attributes["class"] = "classNameHere";
Update
I have created a MasterPage, added a Content Page to it, and added <div id="button1" runat="server">Some text</div> to the Content Page.
In the codebehind of my Content Page, I added this code:
protected void Page_Load(object sender, EventArgs e)
{
var control = FindHtmlControlByIdInControl(this, "button1");
if (control != null)
{
control.Attributes["class"] = "someCssClass";
}
}
private HtmlControl FindHtmlControlByIdInControl(Control control, string id)
{
foreach (Control childControl in control.Controls)
{
if (childControl.ID != null && childControl.ID.Equals(id, StringComparison.OrdinalIgnoreCase) && childControl is HtmlControl)
{
return (HtmlControl)childControl;
}
if (childControl.HasControls())
{
HtmlControl result = FindHtmlControlByIdInControl(childControl, id);
if (result != null) return result;
}
}
return null;
}
This works for me.
If you add runat=server please add it at last of the div:
This will work
<div id="divBtn" class="mybuttonCass" runat="server">
However, this will not
<div runat="server" id="divBtn" class="mybuttonCass">
By doing that, you can modify any property from codebehind as:
divBtn.Style["display"] = "none";
This works even with masterpages. The div is not included in a masterpage.
You need to add a runat=server attribute to any control that you want to access in code behind. See this article for more help.
Accessing <div> from both javascript and code-behind
I cannot seem to be able to reproduce your problem, any chance your DIV's are within a template control such as UpdatePanel, Repeater, or Grid?
I have a div element which I would like to change it'e dir attribute from the code.
The problem is that I can't set the runat=server attribute on the div, so it's not accessible from the code.
Using asp.net, c#, framework 4.0.
Any idea?
Thanks.
If you don't want to set the div to runat="server" so you can do the following:
<div dir='<%= GetDir() %>'>
Your text
</div>
and in the code behind you can set the direction using the following code:
if(You Condition)
{
return "ltr";
}
else
{
return "rtl";
}
Yes, you can't access to the client elements on the page, only to the server-ones (asp.net is a server-oriented framework). The client controls are compiled to the Literal controls with html in them.
If you don't want to set div for the runat="server" attribute, you can register client script to edit your divs content.
If you set the runat="server" attribute, and set the ID="YOUR_DIV_ID_HERE" for it, it will be accessible from code under YOUR_DIV_ID_HERE name.
If you want you can do this also
<div><%= your server variable %> </div>
Use Jquery and call back function to call server side function.
You can set the runat="server" attribute on a HTML element. Just did a local demo, works just fine. It will be represented as an HtmlGenericControl (http://msdn.microsoft.com/en-us/library/7512d0d0(v=VS.100).aspx).
EDIT: You say you cannot use runat="server" to access the element from code. I am not sure why that would be a problem, as the html rendering should be no different. Maybe it has to do with the elements client side id beeing changed? If you depend on this, you could try setting ClientIdMode=Static for your div.
I want to enclose listbox inside a DIV tag in the code behind file using C#.I am adding all the required attributes and adding the list box as below:
Controls.Add(listBox);
I want the generated listbox to be enclosed with in a div. Please suggest how this can be done.
Thanks in advance.
If the div isn't specified in the form then it can be created in the code behind using the HtmlGeneric class:
HtmlGenericControl myDiv = new HtmlGenericControl { TagName = "div" };
myDiv.Controls.Add(listBox);
this.form1.Controls.Add(myDiv);
Otherwise if the div is already in the HTML just add an id and the runat="server" attibute and add it direclty
HTML:
<div id="myDiv" runat="server"></div>
code behind:
myDiv.Controls.Add(listBox);
I have a div which I set the styling from overflow:auto to display:none depending on which buttons are pressed. Is there a way to retrieve which current styling it has and return that value as a string or something in C#?
You can give the div element the runat="server" tag, and access it in codebehind (it is a HtmlGenericControl.
You can get/set the Style property of the div control via ".Style.Value".
<div id="Foo" runat="server"></div>
CodeBehind example:
string myStyle = Foo.Style.Value;
Foo.Style.Value = "padding: 0px;";