繁体   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