简体   繁体   English

如何通过ASP.NET代码隐藏获取放置在转发器中的html文本框的值?

[英]How to get the value of an html textbox placed in a repeater, through ASP.NET code-behind?

I have a repeater, which contains a text box (html) in the ItemTemplate. 我有一个转发器,它包含ItemTemplate中的文本框(html)。

I cannot create an asp textbox, as the text box in question needs to be accessed through jQuery for user manipulation of the text box. 我无法创建一个asp文本框,因为有问题的文本框需要通过jQuery访问,以便用户操作文本框。

Once the user is done, an asp button is pressed in the respective repeater row, and I would like to get the value which the user has entered into the text box, through the code behind (c#). 一旦用户完成,在相应的转发器行中按下asp按钮,我想通过后面的代码(c#)获得用户输入文本框的值。

Could anyone tell me if this is at all possible? 谁能告诉我这是否可能? If so, could some code be suggested? 如果是这样,可以建议一些代码吗?

Otherwise, any non-AJAX alternatives? 否则,任何非AJAX替代品?

There is no issue with referencing asp.net components in jQuery. 在jQuery中引用asp.net组件没有问题。 A couple of things to know about this: 有几点需要了解:

  1. The item will an have id that includes the ID value that you originally gave the control. 该项目的ID将包含您最初为控件提供的ID值。 Using the current encoding scheme (which I cannot imaging Microsoft changing significantly), <asp:TextBox ID="txtBox" runat="server" /> becomes <input id="...._txtBox" name="...$txtBox" /> in the HTML. 使用当前编码方案(我无法使Microsoft成像显着变化), <asp:TextBox ID="txtBox" runat="server" />变为<input id="...._txtBox" name="...$txtBox" /> HTML中的<input id="...._txtBox" name="...$txtBox" /> A jQuery selector like: $(input[id$=txtBox]) will find it. 一个jQuery选择器,如: $(input[id$=txtBox])会找到它。
  2. Because the asp controls eventually resolve to good-old HTML form elements, you treat them exactly like an HTML element you created directly. 因为asp控件最终解析为古老的HTML表单元素,所以您将它们视为您直接创建的HTML元素。 In this case, you enable/disable it using the pseudo-class disabled. 在这种情况下,您可以使用禁用的伪类启用/禁用它。
  3. I learned much of this by viewing the generated source for my web pages and seeing what was happening. 通过查看我的网页生成的源并查看发生的情况,我了解了很多这方面的知识。 I like FireFox with the Web Developer's Toolbar and its View Generated Source command. 我喜欢FireFox和Web Developer的工具栏及其View Generated Source命令。

This should do it. 这应该做到这一点。 Let me know if you want/need any further explanation. 如果您需要/需要任何进一步的解释,请告诉我。

put this in your aspx page: 把它放在你的aspx页面中:

<form id="form1" runat="server">
<div>
<asp:Repeater ID="rptHtmlTag" runat="server">
        <ItemTemplate>
            <input id="htmlTextBox" runat="server" />
        </ItemTemplate>
</asp:Repeater>
</div>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
</form>

put this in your code-behind: 把它放在你的代码隐藏中:

protected override void OnInit(EventArgs e)
{
    this.rptHtmlTag.ItemDataBound += new RepeaterItemEventHandler(rptHtmlTag_ItemDataBound);
    this.btnSubmit.Click += new EventHandler(btnSubmit_Click);
    base.OnInit(e);
}

void btnSubmit_Click(object sender, EventArgs e)
{
    foreach (RepeaterItem item in this.rptHtmlTag.Items)
    {
        HtmlInputText htmlTextBox = (HtmlInputText)item.FindControl("htmlTextBox");
        string THIS_IS_YOUR_VALUE = htmlTextBox.Value;
    }
}

void rptHtmlTag_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        HtmlInputText htmlTextBox = (HtmlInputText )e.Item.FindControl("htmlTextBox");
        htmlTextBox.Value = String.Concat("Some Value - Index", e.Item.ItemIndex);
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        List<int> repeaterPopulator = new List<int> { 1, 2, 3 };
        this.rptHtmlTag.DataSource = repeaterPopulator;
        this.rptHtmlTag.DataBind();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM