如何从Repeater中的控件获取数据

时间:2011-08-03 11:16:06

标签: asp.net repeater

我有项目模板的转发器:

<asp:Repeater ID="queryParametersRepeater" runat="server" 
  DataSourceID="queryParametersObjectDataSource">
  <ItemTemplate>
    <tr class="itemTemplate">
      <td class="labelTd" style="width: 300px;">
        <asp:HiddenField runat="server" Value='<%# Eval("ParameterType") %>' />
        Define <%# Eval("ParameterName") %> (type <%# Eval("ParameterType") %>)
      </td>                        
      <td class="valueTd">                            
        <asp:TextBox runat="server" Width="300px" Text='<%# Eval("ParameterName") %>' />
        <asp:CheckBox runat="server" Width="300px" />
      </td>
    </tr>
  </ItemTemplate>                
</asp:Repeater>

在jQuery中我改变了textBox和chekBox的显示属性。如何从这些控件中获取数据?

我无法使用FindControls()函数,因为我不知道我的控件的ID。

2 个答案:

答案 0 :(得分:11)

您可以使用ID和findcontrol来获取值。这是一个有效的例子:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Collections.Generic" %>

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //bind the data source
            RepeaterExample.DataSource = new List<KeyValuePair<int, string>>{
                new KeyValuePair<int,  string>(1, "Test1"), 
                new KeyValuePair<int,  string>(2, "Test2"), 
                new KeyValuePair<int,  string>(3, "Test3")
            };
            RepeaterExample.DataBind();
        }

    }

    protected void cmdSubmit_Click(object sender, EventArgs e)
    {
            //read the values and output them
            litResults.Text = "";
            foreach (RepeaterItem i in RepeaterExample.Items)
            {

                TextBox txtExample = (TextBox)i.FindControl("txtExample");
                if (txtExample != null)
                {
                    litResults.Text += txtExample.Text + "<br />";
                }
            }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Repeater ID="RepeaterExample" runat="server">
                    <HeaderTemplate>
                        test</HeaderTemplate>
                    <ItemTemplate>
                        <asp:TextBox ID="txtExample" runat="server" Text='<%#Eval("Value") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:Repeater><br />
                <asp:Button ID="cmdSubmit" runat="server" Text="Submit" OnClick="cmdSubmit_Click" />
                <br />
                <asp:Literal ID="litResults" runat="server"></asp:Literal>
            </div>
        </form>
    </body>
</html>

答案 1 :(得分:-1)

您可以在控件中添加一个类吗? 如果是这样,您可以尝试使用$(".givenClass").