전체, 일반적으로 페이지의 코드 숨김 클래스 파일에 정의 된 페이지에서 변수를 출력하고 싶습니다. 변수를 public
또는 protect
표시로 설정할 수 있습니다. 첫 페이지에서 아래와 같이 사용할 수 있습니다. <%=VariableName%>
.ListView에서 <%=...%>을 사용할 수없는 이유
하지만 왜 ListView에서 사용할 수 있는지 모르겠습니다. 내 코드를 검토하는 데 도움을주십시오. 첫 페이지 나 오류 페이지를 가지고
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();
}
}
페이지의 코드 숨김에서
<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>
에서
는 말했다 :
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: }
그리고 나는 경우를 표현식을 사용하십시오.. 오류는 사라졌지만 목록 번호 대신 빈 값이 표시됩니다. 아래의 렌더링 된 결과를 검토하십시오. html :
<div id="divIssuedListItemNumber">
<span> </span>
</div>
내가 놓친 것인지 몰랐습니까? 내가 그랬다면. 내가 알려주도록 도와 줘. 고마워.
+1 매우 유용한 게시물. 그러나'<% # iListNumber.ToString() %>'는 작동하지 않습니다. –
* 작동하지 않습니다 * 어떻게? 아직도 당신에게 예외를 주는가? 당신에게 당신이 원하는 가치를주지 않습니까? –
죄송합니다, 예외없이 목록의 번호를 표시하지 않습니다. 감사. –