Detailed explanation of Asp. Net master page element ID inconsistency

  • 2021-11-01 02:48:22
  • OfStack

This paper introduces the embodiment of Asp. Net master page element ID, and shares it with you, as follows;


<%@ 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 cellspacing="0" cellpadding="0" border="0">
         <tbody>
           <tr>
             <td>
               <asp:Label ID="lbPagename" runat="server" SkinID=" " Text=" Balance statistics "></asp:Label>
             </td>
           </tr>
           <tr>
             <td>
               <asp:ImageButton ID="btnPrint" runat="server" SkinID="b_print" ="btnPrint_Click" />
               <asp:ImageButton ID="btnExport" runat="server" SkinID="b_export" ="btnExport_Click" />
             </td>
           </tr>
         </tbody>
       </table>
       <!-- End function bar -->
       <table border="1" style="font:  Song Style ; font-size: 12px;">
         <tr>
                   <td style="width: 256px; height: 15px;">
              Card number *</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:  Song Style ; width: 180px;"
            >
             <asp:ImageButton ID="nSearch" runat="server" AlternateText=" Query " ImageAlign="Middle"
               ImageUrl="~/images/go.gif" ="nSearch_Click" />
           </td>
         </tr>
       </table>

....

Generated HTML code:


  <form name="aspnetForm" method="post" action="r_Balance.aspx" id="aspnetForm">

<table cellspacing="0" cellpadding="0" border="0">
         <tbody>
           <tr>
             <td>
               <span id="ctl00_MainContent_lbPagename" style="display:inline-block;color:#F2F3F9;border-style:None;font-family: Song Style ;font-size:13px;height:22px;"> Balance statistics </span>
             </td>
           </tr>
           <tr>
             <td>
               <input type="image" name="ctl00$MainContent$btnPrint" id="ctl00_MainContent_btnPrint" src="../App_Themes/style/images/b_print.jpg" ="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" style="border-style:Ridge;border-width:0px;" />
             </td>
           </tr>
         </tbody>
       </table>
       <!-- End function bar -->
       <table border="1" style="font:  Song Style ; font-size: 12px;">
         <tr>

           <td style="width: 256px; height: 15px;">
              Card number *</td>
           <td colspan="1" style="width: 233px; height: 15px">
             <input name="ctl00$MainContent$txtc_printno" type="text" id="ctl00_MainContent_txtc_printno" style="width:120px;height:16px;font-size:12px;font-family: Song Style ;color:DimGray;border-width:1px;border-style:Solid;border-color:#C4CAE6;background-color:White;" /></td>
           <td colspan="1" style="height: 24px; font-size: 14px; font-family:  Song Style ; width: 180px;"
            >
             <input type="image" name="ctl00$MainContent$nSearch" id="ctl00_MainContent_nSearch" src="../images/go.gif" alt=" Query " ="return nselect();" style="border-width:0px;" />
           </td>
         </tr>
       </table>

Note:

1. The source file control and element ID is different from the ID that generated the HTML file. In the generated HTML, the original ASP control ID is prefixed with ctl00_MainContent_, and other elements are prefixed with ctl00 $MainContent $. The original change of form1 to aspnetForm is because the control of aspx page is ContentPlaceHolder of motherboard page
Control, so the control ID will change

2. < system.Web > < xhtmlConformance mode="Transitional|Legacy|Strict" / > Where Transitional is selected, Strict generates an automatic prefix. ctl00. Selecting Legacy produces the auto prefix _ ctl0.

3. The background Request. Form ["txtc_name"] key value needs to be changed and must be changed to Request. Form ["ctl00 $MainContent $txtc_name"] to receive the page input value

4. As to why, it can only be said that this is a problem of. NET mechanism. . .


Related articles: