簡體   English   中英

在ASP.NET中回發訪問HTML輸入

[英]Access HTML Inputs on Postback in ASP.NET

我已經為此努力了幾天,試圖訪問html輸入,即基於數據庫結果在運行時生成的復選框或textboxex。 我的HTML作為文字控件添加到asp占位符中,如下所示:

<div class='cart-item'>
  <div class='product-name'> <h3>gold baseball figure</h3></div>
  <img src='Graphics/gold-trophy.png'></img>
  <div class='inventory-short-description'> <p>A finely crafted gold plated baseball figuring to top your choice of trophey base (sold seperatly).</p></div>        
  <div class='clear'></div>
  <div class='item-price'><p>$22.95</p></div>
  <div class='cart-item-checkbox'><label><input type='checkbox' id='1' name='1' runat='server'/> Select </label>
  <a href='products.aspx?viewItem=1'>view item </a></div></div

問題是,如何訪問頁面后面的.cs代碼中的此復選框?

我生成此html並將其添加到頁面的代碼在重寫的OnInit方法中。 查看回發中的占位符,表明該復選框位於文字控件中。

我試過了:

搜索動態控件時,Page.FindControl()返回null

ASP.NET page_init事件?

FindControl()返回null

http://forums.asp.net/t/1336244.aspx?finding+HTML+control

....還有無數其他人。

昨天我使用一種怪異的方式使用jquery將這些復選框的值設置為asp.net隱藏字段。 我的同事(所有Java開發人員)都說這似乎是訪問這些元素的錯誤方法。 有沒有更好的辦法? 我開始認為我已經將自己編碼成一個我無法擺脫的地方。

謝謝您的幫助。

在您的.aspx中

<asp:Repeater ID="rptItems" runat="server" EnableViewState="False">
        <ItemTemplate>
                <asp:TextBox ID="txtName" runat="server" /> 
        </ItemTemplate>
</asp:Repeater>

在您的.aspx.cs文件中,您可以在回發事件中訪問中繼器,如下所示

 foreach (RepeaterItem rptItem in rptItems.Items)
        {
            TextBox txtName = (TextBox)rptItem.FindControl("txtName");
            if (txtName != null) { System.Diagnostics.Debug.Write(txtName.Text); }          
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM