【问题标题】:getting data from repeater controls从中继器控件获取数据
【发布时间】:2013-07-17 14:37:51
【问题描述】:

我有一个中继器,其中包含不同的控件,如文本框、下拉列表等... 这些控件的值填充在转发器的 itemdatabound 中。 当我单击一个按钮时,我想从这些控件中获取值。 此转发器位于具有母版页的页面中。

按钮代码如下

protected void butEdit_Click(object sender, EventArgs e)
        {
            Location loc = new Location();
            Dictionary<string, Dictionary<int, string>> ret = DBCommon.FillInterface(loc);
            foreach (RepeaterItem repeated in repEdit.Items)
            {
                DropDownList drp = (DropDownList)repeated.FindControl("drpdown");
                TextBox txt = (TextBox)repeated.FindControl("txt");
                CheckBox chk = (CheckBox)repeated.FindControl("chk");
                if (drp != null && !string.IsNullOrEmpty(drp.Attributes["ID"]))
                {
                    loc.GetType().GetProperty(drp.Attributes["ID"].Split('#')[0] + "ID").SetValue(loc, int.Parse(drp.SelectedValue), null);
                }
                if (txt != null && !string.IsNullOrEmpty(txt.Attributes["ID"]))
                {
                    if (txt.Attributes["ID"].Contains("#int"))
                    {
                        loc.GetType().GetProperty(txt.Attributes["ID"].Split('#')[0]).SetValue(loc, int.Parse(txt.Text), null);
                    }
                    else if (txt.Attributes["ID"].Contains("#decimal"))
                    {
                        loc.GetType().GetProperty(txt.Attributes["ID"].Split('#')[0]).SetValue(loc, decimal.Parse(txt.Text), null);
                    }
                    else
                    {
                        loc.GetType().GetProperty(txt.Attributes["ID"].Split('#')[0]).SetValue(loc, txt.Text, null);
                    }
                }
                if (chk != null && !string.IsNullOrEmpty(chk.Attributes["ID"]))
                {
                    loc.GetType().GetProperty(chk.Attributes["ID"].Split('#')[0]).SetValue(loc, chk.Checked, null);

                }
            }
        }

和aspx

<div class="searchResults">
        <asp:Repeater ID="repEdit" runat="server" OnItemDataBound="repEdit_ItemDataBound" ViewStateMode="Enabled" EnableViewState="true" OnItemCommand="repEdit_ItemCommand">
            <ItemTemplate>
                <asp:Label ID="lblName" runat="server" Visible="false" ViewStateMode="Enabled" EnableViewState="true"></asp:Label>
                <asp:TextBox ID="txt" runat="server" Visible="false" ViewStateMode="Enabled" EnableViewState="true"></asp:TextBox>
                <asp:CheckBox ID="chk" runat="server" Visible="false" ViewStateMode="Enabled" EnableViewState="true"/>
                <asp:DropDownList ID="drpdown" runat="server" Visible="false" EnableViewState="true"></asp:DropDownList>
            </ItemTemplate>
            <SeparatorTemplate>
                <br />
            </SeparatorTemplate>
            <FooterTemplate>
                <asp:Button runat="server" ID="butEdit" Visible="false" Text="Update" CommandArgument="editcomm" />
            </FooterTemplate>
        </asp:Repeater>

    </div>

【问题讨论】:

    标签: asp.net repeater


    【解决方案1】:

    我使用Item.FindControl() 方法来获取值。我需要在 ItemCommand 函数中获取值。

    public void repEdit_Itemcommand(object source, RepeaterCommandEventArgs e)
    {
        txtField = (TextBox)e.Item.FindControl("txt");
        txtField.Text;
    }
    

    这样,您也可以从中继器获取其他值。这对我有用,请试试这个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-30
      • 1970-01-01
      相关资源
      最近更新 更多