Why I can't add TemplateField to div html element? - c#

I use asp.net 4.5 in my project.
I try to add control in code behind bu I get error.
Here is my HTML code from view:
<div class="row" id="_FooterPanel" runat="server"></div>
Here is my code behind:
public void FooterPanelInit()
{
var myFooterTemplate = new TemplateField();
var viewFooter = new ViewFooterTemplate();
var editFooter = new EditFooterTemplate();
myFooterTemplate.ItemTemplate = viewFooter;
myFooterTemplate.EditItemTemplate = editFooter;
_FooterPanel.Controls.Add(myFooterTemplate);
}
In code above I try to add to div with id="_FooterPanel" the TemplateField.
But in this row:
_FooterPanel.Controls.Add(myFooterTemplate);
I get this error:
cannot convert from 'System.Web.UI.WebControls.TemplateField' to 'System.Web.UI.Control'
My question is how can I add TemplateField to div with id="_FooterPanel"?

TemplateField is databound control of type 'System.Web.UI.WebControls.TemplateField' but div control can have collection of controls of type System.Web.UI.Control
so you may use repeater control instead of div with runat='server'
or use your custom user control

Related

Dynamically add div in .aspx page

I'm looking for a JavaScript-free way to dynamically add div elements inside a certain element of an aspx web page according to data passed to i.
I'm looking for something similar to razor c# where you can even create loops and write c# code that directly effects the content of the page.
It appears that razor c# doesn't work unless the page is a .cshtml page, so I'm kinda lost here since I don't want to use JavaScript.
Is there a better approach to it?
Say, below is the placeholder div
<div id = "maindiv">
<asp:PlaceHolder ID ="maindivs" runat="server">
</asp:PlaceHolder>
</div>
Then you may add divs to your placeholder div like below
protected void addmainDiv(int m)
{
for(int i = 0; i< m; i++)
{
System.Web.UI.HtmlControls.HtmlGenericControl newdivs = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
newdivs.Attributes.Add("class", "maindivs");
newdivs.ID = "createDiv";
newdivs.InnerHtml = " I'm a div, from code behind ";
maindivs.Controls.Add(newdivs);
}
}
If you want to create HTML dynamically in .aspx then you can create your html code in your .cs (backend file)
string htmlAsStringToShow = "<div>" + variable + "</div>";
Then show its content in your .aspx page like this :
<%= htmlAsStringToShow %>

User control not added to panel via code?

I have a panel inside an update panel on my page which is pre-loaded with a user control, and I want to remove this control and add a new one instead (after a user does some action)
I registered the control:
<%# Register src="~/UserControls/FilesControl.ascx" tagname="FilesControl" tagprefix="files" %>
<asp:Panel ID="pnlFiles" CssClass="selected_tab" runat="server" ClientIDMode="Static">
<files:FilesControl runat="server" ID="filesControl" ShowSearchParams="false" ShowExportControl="false" />
</asp:Panel>
And for adding the new control I wrote this code:
pnlFiles.Controls.Clear();
FilesControl filesHistory = (FilesControl)LoadControl("~/UserControls/FilesControl.ascx");
filesHistory.ShowExportControl =
filesHistory.ShowSearchParams = false;
InitHistoryControl<FilesControl>(filesHistory, daysBack, true); //Sets a datasource to a grid view in the control
pnlFiles.Controls.Add(filesHistory);
But the control is not added to the panel, I don't get any errors even in debug, it is just not there. I can't even see it in the html on view source.
Perhaps because you have these lines:
filesHistory.ShowExportControl =
filesHistory.ShowSearchParams = false;
Try something like:
filesHistory.ShowExportControl = true;
filesHistory.ShowSearchParams = false;

c# - how to set specific control indentification in asp.net?

i desire to create ASP.NET control with specific identification with C#:
MyControl myControl = new MyControl { ID = "my-control" };
but after render, the identification become:
<div id="ctl13_my-control"> ... </div>
i desire for style sheet of cascade:
<div id="my-control"> ... </div>
how to fix?
In ASP.NET 4 and above, you can set the ClientIDMode property to determine how the control's ID is rendered. For example using Static means whatever ID you give the control is what you will see in the HTML source.
You could try setting the ClientID property instead:
MyControl myControl = new MyControl { ClientID = "my-control" };
You can do that with the property "clientidmode" set to "static"
<div id="my-control" clientidmode="Static"> ... </div>
Been there, done that.

Is there a way I can make a div runat server? So i can turn it into a control?

Is there a way I can make a div runat server? So i can turn it into a control? In asp.net?
EDIt:
IF so how can I tell my code below to make div ID=test runat server?
while (reader.Read())
{
System.Web.UI.HtmlControls.HtmlGenericControl div = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
div.Attributes["class"] = "test";
//div.Style["float"] = "left";
div.ID = "test";
Image img = new Image();
img.ImageUrl = String.Format("{0}", reader.GetString(1));
// this line needs to be represented in sql syntax
//img.ImageUrl = "~/userdata/2/uploadedimage/batman-for-facebook.jpg";
img.AlternateText = "Test image";
div.Controls.Add(img);
div.Controls.Add(ParseControl(String.Format("&nbsp&nbsp "+"{0}", reader.GetString(0))));
div.Style["clear"] = "both";
test1.Controls.Add(div);
}
Yes
<div runat="server"></div>
will make it a server side control. You can generate it in the code behind using
var myDiv = new HtmlGenericControl("div");
Edit: What you're generating is a server side control, there is no need to add runat="server".
Edit per comment:
<div onclick="alert('hello')" runat="server"></div>
or
var myDiv = new HtmlGenericControl("div");
myDiv.Attributes.Add("onclick", "alert('hello');");
You can make a div runat="server", give it an id and the reference it from C# if that's what you're after. However, why not just use an asp:panel, they do the same job essentially and the panel renders a div out in most scenarios
asp:Panel is a DIV when it gets rendered
it is definitely not be the most elegant solution, but adding a literal control and build up your div from code will do the trick.
ASPX code
<div id="alert" runat="server" style="float: left">
</div>
C# code
alert.InnerHtml= ANY HTML CODE ;

html from code behind

I want to insert html with a few controls +style from the code behind ( asp.net c#) how can I do it ?
You could use a <asp:PlaceHolder> then add controls to this.
e.g.
Image img = new Image();
img.ImageUrl = "/someurl.jpg";
img.CssClass = "someclass";
img.ID = "someid";
img.AlternateText = "alttext"
PlageHolderId.Controls.Add(img);
This would produce the html
<img src="/someurl.jpg" class="someclass" id="someid" alt="alttext" />
You can then do this will any control, literal, hyperlink, button, table, etc...
You can add <asp:Literal> controls in the markup, then set their Texts in code-behind.
Make sure to set Mode="PassThrough" to prevent them from escaping the HTML.
You can add server-side controls by adding them to the Controls collection of any existing control (such as an <asp:Panel>)
Add a few <asp:PlaceHolder>'s to your template file in the <head> and <body>
Then use PlaceHolder1.Controls.Add();
I put a <asp:Panel ID="myPanel" runat="server"/> , and in the codebehind i add controls with:
myPanel.Controls.Add(...)
If you want to insert HTML code direct to the panel, use
myPanel.Controls.Add(new LiteralControl("Your HTML goes here!"))
Instead of Literal control you can use either HtmlGenericControl
HtmlGenericControl div = new HtmlGenericControl();
div.ID = "div";
div.TagName = "div";
div.Attributes["class"] = "container";
form1.Controls.Add(div);

Categories