I have a div with id="myDiv" and runat="server" inside a listview with id="lvItem".I need to access the div in code behind to add width of the div at runtime.How can I access the div from codebehid using C#?Can anyone help me?
Just add an Asp:Panel instead. Asp:Panel in ASP.NET = DIV on the front end.
So basically this would be your code
ASP.NET
<asp:Panel id="myDiv" runat="server">
</asp:Panel>
Code Behind
Panel myDiv = (Panel)myListView.FindControl("myDiv");
myDiv.Attributes.Add("style", "width: 100px");
<div id="coolmenu" runat="server">
</div>
HtmlControl htmlDivControl = (HtmlControl)Page.FindControl("coolmenu");
(htmlDivControl.Style["width"]) ="30px";
Related
I am using the AjaxControlToolKit.
I have an Accordion with an Image inside the Header-Template.
Inside the Content-Template there is a nested Accordion.
These two Accordions are each inside an UserControl. So the parent Accordion has an UserControl inside the Content.
If I am changing a specific control inside the nested Accordion, the nested UserControl calles a public Method of the parent UserControl which changes the source of the Image I have mentioned.
I'm doing this this way because I want to avoid a DataBind because it's very heavy loaded.
While debugging it seems to work because the new value is set to the source and will be stored to the next Postback. But the responsed site does not show the new values.
Is there a way to change the values and show it on response?
Edit - Here some striped code...
Structure:
#Site
#DocumentationPanel-UserControl (OuterAccordion)
#InnerDocumentAccordion-UserControl (InnerAccordion)
#/DocumentationPanel
#/Site
Striped Code:
<table><tr><td>
<ajaxtoolkit:accordion id="accordionGrid" runat="server" style="overflow: visible;">
<%-- Header --%>
<headertemplate>
<img id="imgUserExamState1" runat="server" title="" alt="" src="" />
</headertemplate>
<%-- Contenter --%>
<contenttemplate>
<ajaxtoolkit:accordion id="accordionGridExaminations" runat="server">
<%-- Header --%>
<headertemplate>
<img id="imgUserExamState2" runat="server" title="" alt="" src="" />
</headertemplate>
<%-- Contenter --%>
<contenttemplate>
...
</contenttemplate>
</ajaxtoolkit:accordion>
</contenttemplate>
</ajaxtoolkit:accordion>
</td></tr></table>
Now I am calling from inside the InnerDocumentAccordion the Parent, casting it to DocumentationPanel and using the public Property that links to the outer Accordion.
The InnerDocumentAccordion-Control knows the PaneIndex of the outer Accordion it is in, so I can call the wanted Pane directly and find the image (imgUserExamState1) in the header.
That looks like this:
AccordionContentPanel acp = ((DocumentationPanel)this.ParentControl).AccordionGrid.Panes[this.ParentAccordionIndex].HeaderContainer;
HtmlImage image = ((HtmlImage)(acp.FindControl("imgUserExamState")));
image.Src = "funny.png";
I can change it but there are no changes about the image in the responsed site. I tried to call a public function of DocumentationPanel, so the changes where made inside the same class the outer Accordion is in, but as expected - no success
I have tried to Rebind the outer Accordion but it says that no modification can be done while init, load, prerender etc. But it was only a try from the start.
Currently this is working:
<div id="DashboardDiv" runat="server">
<asp:Panel ID="pnlA" runat="server" CssClass="Widget" Width ="900">
<asp:Literal ID="ltrChart" runat="server"></asp:Literal>
</asp:Panel>
<asp:Panel ID="pnlB" runat="server" CssClass="Widget" Width ="900">
<asp:Literal ID="ltrChart2" runat="server"></asp:Literal>
</asp:Panel>
</div>
I need to use a literal, in order to use dotNet Highcharts in codebehind.
The problem however, is that I'm creating a Dashboard which will display multiple widgets. Right now my testcase consists of 1 widget, but I want to eventually expand that to an indetermined number of widgets.
I feel that I need to create panels (and literals added to these), based on the amount of widgets a user wants, on run-time. How do I do this?
I tried something like this:
protected static List<Panel> panelList = new List<Panel>();
Panel myChildPanel = new Panel();
myChildPanel.Width = 900;
panelList.Add(myChildPanel);
DashboardDiv.Controls.Add(myChildPanel);
And then adding the literal to this List of Panels, to a specified panel:
Literal ltrChart = new Literal();
ltrChart.Text = chart.ToHtmlString();
panelList[counter].Controls.Add(ltrChart);
This feels like an ugly way of doing things.
Basically I'm looking for this:
How can I create panels/areas/zones that are capable of containing a Literal, in code-behind, so that I can create a dashboard filled with widgets-look?
Have you tried using asp placeholders? I believe they will help you acheive what you want to do. They allow you to place and kind of controls in them and you can hide and show a placeholder based on a specific condition. For example
<asp:Placeholder runat="server" visible="<%=CMSContext.CurrentUserIsAuthenticated()%> >
<div class="faq-listing itmList faq odd">
<h2 class="heading"> Home</h2>
<asp:Repeater ID="rptSubFaq" runat="server">
</asp:Repeater>
</div>
</asp:Placeholder>
I have a problem you can see at the title.
<form id="form1" runat="server">
<div>
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:TextBox ID="mac_id" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Temp Ekle" />
</div>
</form>
inside aspx.cs i create a table and i insert some values from database to this text box called "mac_id".
TableCell cel = new TableCell();
mac_id.Text = Convert.ToString(myDataTable.Rows[0][2]);
cel.Controls.Add(mac_id);
rw.Cells.Add(cel);
it is inside the form tag and it is runat="server".This is the code that using mac_id textbox.there is no code that manipulate that textbox.I only want to show the user the id.I am not using any render control or something that textbox has.I also check the other solutins but they are all using render or writer i am not thats because i open a new question.
I hope i can explain my problem enough.Thanks for helping.
You can avoid this execption by overriding VerifyRenderingInServerForm
public override void VerifyRenderingInServerForm(Control control)
{
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET
server control at run time.
Hence, no exception. */
}
I am designing a website for avoid the code complexity i use controls for footer section and i call the control in master page itself. but now my client asks they not need footer for a particular page named contactus.
so, here any programmatic way to disable the control for a single page or any other solutions available?
my master page structure:
<form id="form1" runat="server">
<div id="bodywrapper">
<ucdart:Header ID="Header1" runat="server" />
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
<ucdart:Footer ID="Footer1" runat="server" />
</from>
you can try this on the page you want to hide the div
Footer foot = (Footer)Master.FindControl("Footer1");
foot.Visible = false;
and this on a page you want to show the div
Footer foot = (Footer)Master.FindControl("Footer1");
foot.Visible = true;
Control c = Master.FindControl("Footer1");
c.Visible = false;
TextWriter tr = new StringWriter();
HtmlTextWriter writer = new HtmlTextWriter(tr);
HtmlForm form = new HtmlForm();
form.Controls.Add(divQueryDescription);
divQueryDescription.RenderControl(writer);
<div runat="server" id="divQueryDescription">
<asp:GridView runat="server" id="grv"></asp:GridView>
</div>
My GridView is inside a DIV and I add that DIV to an HtmlForm, so that I can render it to a string. But I get the following error:-
Gridview 'grv' must be placed inside a form tag with runat=server, although my Gridview is already inside a form because I added my DIV to the form.
How can I render this DIV to a string ?
Just add the following code in your page
public override void VerifyRenderingInServerForm(Control control)
{
return;
}
To learn more about it, read the MSDN documentation.
Make sure you have runat="server" in your form element/control
From what you show us here, the GridView isn't already in a form element with runat="server", you need to do something like this:
<form runat="server">
<div runat="server" id="divQueryDescription">
<asp:GridView runat="server" id="grv"></asp:GridView>
</div>
</form>
Of particular note is the <form runat="server"> part, and its ending tag.
Update:
It looks like the form is causing problems because it hasn't been written out, you're writing out from the div directly: try using form.RenderControl(writer);.