I've been reading about user controls on msdn, and I was wondering how does one deal with element IDs that may be variable?
For example, say I have usercontrol1.ascx on my page that contains this:
<div id="myPageDiv1">
<div id="container1">
Contents
</div>
</div>
What if I need the IDs to be different across different pages?
Maybe I need another page that needs to look like this:
<div id="myPageDiv2">
<div id="container3">
Contents
</div>
</div>
Thanks
You could add a property to your usercontrol so you can change it on a page-by-page basic:
UserControl
Public Property SomeDivID as String
ASPX
<div id="<%= Me.SomeDivID %>">
</div>
Page
<control:yourusercontrol SomeDivID="myPageDiv1" runat="server" />
<control:yourusercontrol SomeDivID="myPageDiv2" runat="server" />
You can define a property in your user control.
Public Property DivName As String
You can set it in your codebehind to your desired name. You can even pass this from your controlling page.
DivName = "MyDesiredDivName"
Then you can use crocodile syntax in your markup to render the value of the property.
<div id="<%= Me.DivName %>">
<div id="container1">
Contents
</div>
</div>
Related
I use masterpage in asp.net web form, I use span to display message to users
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
<div class="col-md-10 col-sm-10">
<span id="spnMessage" class="btnText" runat="server"></span>
</div>
</asp:Content>
and code is:
spnMessage.InnerText = "Add record successfully ";
but in runtime id of span change to "ContentPlaceHolder2_spnMessage" and my code does not work. What should I do?
Change the span into a Literal. Then there will be no more issues with ID conflicts.
<asp:Literal ID="spnMessage" runat="server"></asp:Literal>
Then accessing the Literal from code behind will always work.
spnMessage.Text = "Add record successfully ";
Or if you need to use the <span> element for CSS purposes, you can still wrap the Literal with it.
<span><asp:Literal ID="spnMessage" runat="server"></asp:Literal></span>
You could also use a aspnet Label.
A label will generate it's own <div> tag in HTML.
<asp:Label ID="spnMessage" runat="server" Text="Add record successfully"></asp:Label>
Will become
<div>Add record successfully</div>
I am working on setting up a Asp.Net Master page that uses Bootstrap. We have a three column layout with third column as optional
<div class="row " >
<div id="leftCol" class="col-sm-3 no-left-gutter leftmenu" />
<div class="col-sm-7" id="centercol' />
<div class="col-sm-2" id = "rightcol"> <asp:ContentPlaceHolder .../> </div><div>
Now If any of the pages inheriting this master page doesn't use the rightCol div to place any content then the right column should be hidden and the center column should take size of both center and right column.
Is there any way to achieve this easily using bootstrap (or with out it)? Thank you.
The class for the centercol and rightcol divs can be set dynamically based on right column's visibility
visible: class set to col-sm-7(center) and col-sm-2 (right)
hidden : class set to col-sma-9(center) and display:none (right)
You can check the ContentPlaceHolder for any content and set the CssClass in code behind:
ASPX:
<div class="row " >
<div id="leftCol" class="col-sm-3 no-left-gutter leftmenu" />
<asp:Panel ID="pCenter" runat="server">
<asp:Panel ID="pRight" runat="server">
<asp:ContentPlaceHolder ID="cphRight" runat="server" />
</div>
<div>
CS:
if(this.cphRight.Controls.Count > 0)
{
this.pCenter.CssClass = "col-sm-7";
this.pRight.CssClass = "col-sm-2";
}
else
{
this.pCenter.CssClass = "col-sm-9";
this.pRight.Visible = false;
}
I used asp:Panel instead of div, because they come handy and generate a div, too. Alternatively, you can still use a div but you should give it the attribute and value runat="server" to be able to access it in code behind.
All, Generally .If we want to output a variable in a page which defined in the code-behind class file of page . We could set the variable with public or protect visibility. and in the front page we can use it like below.
<%=VariableName%>.
But I don't know why can't use it in the ListView. Please help to review my code.
In the front page
<div id="divIssuedListContainer" class="myIssuedList">
<asp:ListView id="listShower" runat="server">
<LayoutTemplate>
<div id="divIssuedListHeadTitle">
<div id="divIssuedListTitleIco">
<img alt="" src="ico/IssuedCap.png" />
</div>
<div id="divIssuedListTitleName">
<span>ISSUED</span><span>TRADE NAMES</span><span>/LICENSES</span>
</div>
<div id="divIssuedListItemNumber">
<span>
<%=iListNumber%></span>
</div>
</div>
<div style="clear:both"></div>
<div id="divIssuedListBody">
<ul>
<asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
</ul>
</div>
</LayoutTemplate>
<ItemTemplate>
<li>
<table>
<tr>
<td>
<img alt="" src="" />
</td>
<td>
<div>
<span>
<%# Eval("altID") %>
-
<%# Eval("englishTradeName") %></span></div>
<div>
<span>Expired Date:<%# Eval("expDate") %></span>
</div>
</td>
</tr>
</table>
</li>
</ItemTemplate>
</asp:ListView>
In the code-behind of page
public partial class _Default : System.Web.UI.Page
{
protected int iListNumber = 0;
protected void Page_Load(object sender, EventArgs e)
{
List<SimpleCapModel> DataSourceList = TestDataHelper.GetMyIssuedList();
listShower.DataSource = DataSourceList;
iListNumber = DataSourceList.Count();
listShower.DataBind();
}
}
I got an error page said :
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
Source Error:
Line 19: listShower.DataSource = DataSourceList;
Line 20: iListNumber = DataSourceList.Count();
Line 21: listShower.DataBind();//this line cause error.
Line 22: }
Line 23: }
And If I use the expression <%#iListNumber%>. The error is gone, but I got a empty value instead of the number of the list. please review below rendered result html :
<div id="divIssuedListItemNumber">
<span> </span>
</div>
I didn't know whether I missed something ? If I did .Please help to let me know it.thanks.
You need to use <%#iListNumber %> because you are using data-binding the ListView, otherwise you'll get the exception (see: http://support.microsoft.com/kb/976112 for more on inline expressions).
Try using <%#iListNumber.ToString %> and see if that makes a difference.
UPDATE:
The problem appears to be that your inline-expression is in the LayoutTemplate, where data-binding [apparently] won't work without either handling the OnLayoutCreated event, or extending the ListView.
See either of the following for examples of how to extend the ListView:
http://www.codeproject.com/Articles/42962/Databind-Expression-in-ListView-LayoutTemplate
http://forums.asp.net/p/1201992/3319344.aspx#3319344
Given the error you're receiving, it basically means this: you can't just randomly throw .NET tags in databound controls.
The solution is to, instead, throw that random text in a .NET control, like this: <asp:Literal Runat="server" Text="<%# iListNumber %>" />
When you put a .NET codeblock in code, it dynamically has to build a control placeholder for it. It's a little tricky. But if you wrap all your custom tags up in a control, then just that one property gets databound and the databound block remains "clean" if that makes sense.
I have following code in aspx page:
<div id="a" runat="server" style="display:block;">
abc
</div>
I am trying to show the div in code like this:
a.Visible = True
But this is not working. Can anyone suggest how to do this without using any scripting language?
A div with runat=server becomes a HtmlGenericControl on serverside. This has the Visible-property as every server-control. So you can hide it on serverside. But that means on clientside it won't get rendered at all.
If you instead want it to be rendered invisibly, add the Styledisplay:none:
a.Style.Add("display","none")
Then you can also switch the visibility on clientside.
Apart from that your tag is malformed, change
runat="server
to
runat="server"
You are missing doube quotes after runat Server. It should be like this..
<div id="a" runat="server" style="display:block;">
abc
</div>
Or in code Behind to hide the div
a.Style.Add("display","none")
in code Behind to show the div
a.Style.Add("display","block")
Try this:
a.Attributes.Add("style", "display:block;");
Mode details:
http://msdn.microsoft.com/en-us/library/7512d0d0(v=vs.71).aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.control.visible.aspx
<div id="a" runat="server" style="visibility:hidden" >
</div>
you can try : hidden in code vb.net
a.Attributes("style") = "visibility:hidden"
you can try : visible
a.Attributes("style") = "visibility:visible"
thanx
a.Style.Add("display", "block")
for visible
a.Style.Add("display", "none")
for hide
I have a set of websites that basically do the exact same thing in terms of functionality, the only thing that varies is the css and how the html is laid out.
Is there a way to generate the html from a database (ie retreiving the html from a table) and then catching the controls (like buttons, textboxes etc) from the code behind?
UPDATE:
Here is an example of what I want to acheive:
<html>
<div id="generatedContent" runat="server">
<!-- the following content has been generated from the database on the page load event -->
<div>
This is a textbox <input id="tb1" runat="server" type="text" />
This is a button <input type="submit" id="btn1" runat="server" value="My Button" />
</div>
</div>
</html>
This is the code behind
var tb1 = new HtmlControls.HtmlInputText();
tb1 = FindControl("tb1");
var btn1 = new New HtmlControls.HtmlInputSubmit();
btn1 = FindControl("btn1");
Now obviously since the html has been generated from the database (ie after the page has already been initialised) the FindControl method will return null.
For static HTML content, you can easily add your content to your form by using LiteralControl like that :
Page.Controls.Add(new LiteralControl("<b>content from db</b>"));
EDIT : Your code-behind code (FindControl) should work if you put them in a form tag with runat=server attribute. But beware, your content should be added in every postback. Also you can get the values of your form elements by Request["FormElementId"].