1. 程式人生 > >Asp.Net母版頁元素ID不一致的體現

Asp.Net母版頁元素ID不一致的體現

Asp.Net母版頁元素ID不一致的體現
來自森大科技官方部落格 http://www.cnsendblog.com/index.php/?p=104
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" Theme="style" AutoEventWireup="true"
CodeFile="r_Balance.aspx.cs" Inherits="Report_r_Balance" %>
<asp:Content ID="content" ContentPlaceHolderID="MainContent" runat="server">

 <form id="form1" runat="server">
     <div>
         <table class="topTable" cellspacing="0" cellpadding="0" border="0">
             <tbody>
                 <tr class="title">
                     <td>
                         <asp:Label ID="lbPagename" runat="server" SkinID="title" Text="餘額統計"></asp:Label>
                     </td>
                 </tr>
                 <tr class="butt">
                     <td>
                         <asp:ImageButton ID="btnPrint" runat="server" SkinID="b_print" OnClick="btnPrint_Click" />
                         <asp:ImageButton ID="btnExport" runat="server" SkinID="b_export" OnClick="btnExport_Click" />
                     </td>
                 </tr>
             </tbody>
         </table>
         <!--結束功能條-->
         <table border="1" style="font: 宋體; font-size: 12px;">
             <tr>
                                 <td align="center" style="width: 256px; height: 15px;">
                     卡號*</td>
                 <td colspan="1" style="width: 233px; height: 15px">
                     <asp:TextBox ID="txtc_printno" runat="server"></asp:TextBox></td>
                 <td colspan="1" style="height: 24px; font-size: 14px; font-family: 宋體; width: 180px;"
                     align="center">
                     <asp:ImageButton ID="nSearch" runat="server" AlternateText="查詢" ImageAlign="Middle"
                         ImageUrl="~/images/go.gif" OnClick="nSearch_Click" />
                 </td>
             </tr>
         </table>

....
生成的HTML程式碼:
<form name="aspnetForm" method="post" action="r_Balance.aspx" id="aspnetForm">
<table class="topTable" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr class="title">
<td>
<span id="ctl00_MainContent_lbPagename" style="display:inline-block;color:#F2F3F9;border-style:None;font-family:宋體;font-size:13px;height:22px;">餘額統計</span>
</td>
</tr>
<tr class="butt">
<td>
<input type="image" name="ctl00$MainContent$btnPrint" id="ctl00_MainContent_btnPrint" src="../App_Themes/style/images/b_print.jpg" align="middle" onclick="return np();" style="border-style:Ridge;border-width:0px;" />
<input type="image" name="ctl00$MainContent$btnExport" id="ctl00_MainContent_btnExport" src="../App_Themes/style/images/b_export.jpg" align="middle" style="border-style:Ridge;border-width:0px;" />
</td>
</tr>
</tbody>
</table>

卡號*
注意: 1.原始檔控制元件和元素ID和生成HTML檔案的ID不一致。在生成的HTML中原ASP控制元件ID加了ctl00_MainContent_字首,其他元素加了ctl00$MainContent$字首。原變form1為aspnetForm這是因為aspx頁面的控制元件是母板頁的ContentPlaceHolder 控制元件下的子控制元件,所以控制元件ID會變 2. 在其中選擇 Transitional、Strict則產生自動字首。ctl00.選擇 Legacy|則產生自動字首_ctl0. 3.後臺Request.Form["txtc_name"]鍵值需要改變,必須變為Request.Form["ctl00$MainContent$txtc_name"]才能收到頁面輸入值 4.至於為什麼,只能說這是.NET機制問題。。。