c# - codebehind div style change not being rendered - c#

I have a div of the form
<div id="mydiv" runat="server" style="display:none"></div>
In my code behind, I attempt to change the display to block by
mydiv.Style["display"] = "block";
When I view the source code after the piece of code above is run, the page source reflects the change (i.e. the div's display is set to block) but the visible page does not. How can I get my page to show the div?

Where is your code mydiv.Style["display"] = "block";? I just test it in Page_Load,and in the view page,it displayed,and the code in page source is this
<div id="mydiv" style="display:block;">12345</div>
I set innerhtml to have a good view

Related

Modal popup not showing with runat=server on <div>

I hope this is a simple one for someone to answer.
If i have a button that allows a user to edit something on the page, they can click on
Edit this page
This then opens a modal on the page with the following DIV
<div id="editpage" class="modal" tabindex="-1" role="dialog">
This is the content in my modal
</div>
The issue I am having is that when I add a runat="server" to the modal's div tag, so that I can determine if the user can even see/inspect the page source if they are not a moderator,, that the modal no longer pops up when i click on my edit button.
Is there anything I can do so that I can still control if the modal is rendered from code-behind based on logic there, and still have the edit to modal click function still work without turning it into a server control.
Found the answer which was to wrap the modal div in another div or placeholder tag and then control that from the server side controls. This meant that the modal's div did not have it's ID modified due to the server side assignment and therefore I am now able to remove this markup entirely if the user is not a moderator.
<asp:Panel ID="editpage_panel" runat="server" Visible="false">
-- modal here
</asp:Panel>

access div tag inside form tag of master page

I have a div inside the form tag of master page.
I want to change it dynamically as and when a page loads.
I am building a e-commerce web site where in I have a cart icon along with which I display the total amount corresponding to a user.
This Cart tag and the amount tag is inside the form tag of master page and I cannot find it from the code behind using HtmlGenericControl.
C# code
HtmlGenericControl myObject = new HtmlGenericControl();
myObject = (HtmlGenericControl)Master.FindControl("simpleCart_total");
Html code
<div class="ca-r">
<div class="cart box_1">
<a href="../ShoppingCart.aspx">
<h3> <div class="total">
<span class="simpleCart_total" runat="server" style="display:none"></span> </div>
<img src="/images/cart.png" alt=""/></h3>
</a>
How do i access the simpleCart_total from code behind of master page itself to change it accordingly.
Please Help
Thank You

.NET webform Hide a HTML td column in backend server (Update Panel, Content Template)

I have an instance, it shows a listView.
the listview is in the update panel, it needs to response to each data source binding and show/hide a column in the listView table by checking a Session.
I cannot simply add
<% if((int)Session["v1"] ==1) { %> <td>Hi</td> <%}%>
as the exception throw saying that the update panel cannot update when <% %> exists.
(but it works for the control outside of the update panel)
I think I can do it by javascript but I just want to make sure if there is a smarter way to do in the backend.
class='<%# HiddenClass %>'
and changing this parameter during onload (change HiddenClass to empty string if the column should show.
It works for the item's column (in the ItemTemplate) but it doesn't work for the item's header column LayoutTemplate (i think it is because data source binding only re-rendered the fields in the ItemTemplate and not included LayoutTemplate.
currently, I was able to hide it to set the runat="server" Visible to false if for each data source binding of the listview. But it looks very complicated when I need to hide more columns (need to create more ID and asp.net cannot set visible of fields by class).
Wrap the <td></td> with a PlaceHolder and set it's Visibility property from code behind.
<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible="false">
<td>Hi</td>
</asp:PlaceHolder>
And then in code behind
if ((int)Session["v1"] == 1)
{
PlaceHolder1.Visible = true;
}

Can I change div id from c# code behind

I have a division something like :
<div id="divAdditionalInfo" runat="server">
//html code here
</div>
In code behind, I need to prompt out a small window (generate by this div) more than 1 time.
Because it is the same div, so I only can see the final time window pop up.
My main question is, Can I change the div id in code behind?
and then use the div id to pop up another window.
Something like :
divAdditionalInfo.Style["visibility"] = "visible";
divAdditionalInfo.Style["display"] = "table-cell";
After change :
divAdditionalInfo.ID = "div2";
But I cant write code like :
div2.Style["visibility"] = "visible";
I only can write back :
divAdditionalInfo.Style["visibility"] = "visible";
There is find control method for panel, text box, button and so on, but how about division?
Can find control division?
something like :
Division d = (Division).FindControl("div2"); //find control on the div id that just change
d.Style["visibility"] = "visible";
kindly comment my question is not clear : )
I have tried something like this.
aspx file:
<div id="div1" runat="server">
<asp:Label ID="lbl1" runat="server" Text="Div ID: "></asp:Label>
</div>
<asp:Button ID="btn_change" runat="server" Text="Change" OnClick="btn_change_Click" />
On button Click event:
div1.ID = "div2";
lbl1.Text = lbl1.Text + div1.ID.ToString();
It will change the ID of div for the temporary. Button when you refresh the page it will automatically get the ID which you have specified on ASPX page.
I think this will work when the ID is chenged by you at that time you can perform the operations you want to perform.
You will able to find Div Tag in FindControl() of panel or form but in Code behind You will not have the Division Control or something like that. Because it not the Asp.Net control it is typical HTML control.

c# - error using FindControl on div id

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?

Categories