【问题标题】:Getting values from textbox in the repeater in button click event在按钮单击事件中从转发器中的文本框中获取值
【发布时间】:2013-07-17 10:58:07
【问题描述】:

我在转发器中有一个 TextBox,它是从 ItemDataBound 事件中的数据库填充的。当我尝试在 Button_Click 事件中获取 TextBox 内的文本时,我发现 TextBox.Text 为空。如何获取文本?

    foreach (RepeaterItem repeated in repEdit.Items)
    {
        DropDownList drp = (DropDownList)FindControlRecursive(repeated, "drpdown");
        TextBox txt = (TextBox)FindControlRecursive(repeated, "txt");
        CheckBox chk = (CheckBox)FindControlRecursive(repeated, "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);
        }
    }

HTML

<asp:Repeater ID="repEdit" runat="server" OnItemDataBound="repEdit_ItemDataBound">
        <ItemTemplate>
            <asp:Label ID="lblName" runat="server" Visible="false"></asp:Label>
            <asp:TextBox ID="txt" runat="server" Visible="false"></asp:TextBox>
            <asp:CheckBox ID="chk" runat="server" Visible="false" />
            <asp:DropDownList ID="drpdown" runat="server" Visible="false">
            </asp:DropDownList>
            <br />
        </ItemTemplate>
</asp:Repeater>

这里是itemdatabound事件

protected void repEdit_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Label name = e.Item.FindControl("lblName") as Label;
            TextBox text = e.Item.FindControl("txt") as TextBox;
            CheckBox chk = e.Item.FindControl("chk") as CheckBox;
            DropDownList drp = e.Item.FindControl("drpdown") as DropDownList;
            Button but = e.Item.FindControl("butEdit") as Button;
            //butEdit.Visible = true;
            if (but != null)
            {
                but.Visible = true;
            }
            if (e.Item.ItemType == ListItemType.Item)
            {
                KeyValuePair<string, Dictionary<int, string>> kvp = (KeyValuePair<string, Dictionary<int, string>>)e.Item.DataItem;
                if (name != null)
                {
                    if (kvp.Key.Contains("#datetime"))
                    {
                        return;
                    }
                    name.Visible = true;
                    name.Text = kvp.Key.Split('#')[0].ToString();
                }
                if (kvp.Key.Contains("#int") || kvp.Key.Contains("#decimal"))
                {
                    text.Visible = true;
                    text.ID = kvp.Key;
                    text.EnableViewState = true;
                    text.Attributes["ID"] = kvp.Key;
                    text.Text = kvp.Value[0].ToString();
                    if (kvp.Key.Split('#')[0] == "ID")
                    {
                        text.Enabled = false;
                    }
                }

【问题讨论】:

  • 在这里贴一些代码sn-ps
  • 这里的代码是你的Button_Click吗?
  • 是的,它在 button_click 中
  • ​​

标签: asp.net asprepeater


【解决方案1】:

这是您的中继器。它有一个 PlaceHolder 和 Button。

<asp:Repeater ID="rpNotifications" runat="server" 
              OnItemDataBound="rpNotifications_ItemDataBound">
    <ItemTemplate>

         <asp:PlaceHolder ID="commentHolder" runat="server"></asp:PlaceHolder>

         <asp:Button runat="server" ID="btnComment" 
              Text="Comment" CssClass="btn blue" 
              OnClick="btnComment_Click" CommandArgument='<%# Eval("id") %>' />

    </ItemTemplate>
</asp:Repeater>

按钮的命令参数设置为我绑定到它的数据的 ID。

接下来是您需要绑定中继器的地方。如果您不在 Page_Init 中绑定它,则动态创建的控件中的结果值将丢失。

protected void Page_Init(object sender, EventArgs e)
{

        rpNotifications.DataSource = {datasource-here}
        rpNotifications.DataBind();
}

这就是原因(图片来自:http://msdn.microsoft.com/en-us/library/ms972976.aspx

在 ItemDataBound 上动态创建控件 您需要将文本框添加到占位符,因为这是为控件动态分配 ID 的唯一方法。

protected void rpNotifications_ItemDataBound(object sender, 
                                             RepeaterItemEventArgs e)
{

        if (e.Item.ItemType == ListItemType.Item 
            || e.Item.ItemType == ListItemType.AlternatingItem)
        {

            Common.Models.Item item = (Common.Models.Item)e.Item.DataItem;

            // Create new Control
            TextBox txtComment = new TextBox();
            txtComment.ID = String.Format("txtComment{0}", item.id.ToString());

            // Ensure static so you can reference it by Id
            txtComment.ClientIDMode = System.Web.UI.ClientIDMode.Static;

            // Add control to placeholder
            e.Item.FindControl("commentHolder").Controls.Add(txtComment);
        }
}

获取按钮点击价值 现在您需要为按钮创建事件处理程序并获取结果值

protected void btnComment_Click(object sender, EventArgs e)
{
        // This is the ID I put in the CommandArgument
        Guid id = new Guid(((Button)sender).CommandArgument);

        String comment = String.Empty;

        // Loop through the repeaterItems to find your control
        foreach (RepeaterItem item in rpNotifications.Controls)
        {
            Control ctl = item.FindControl(
                               String.Format("txtComment{0}", id.ToString()));

            if (ctl != null)
            {
                comment = ((TextBox)ctl).Text;
                break;
            }
        }

}

【讨论】:

    【解决方案2】:

    尝试使用以下代码获取 ASP.Net 控件

     DropDownList drp = (DropDownList)repeated.FindControl("drpdown");
     TextBox txt = (TextBox)repeated.FindControl("txt");
     CheckBox chk = (CheckBox)repeated.FindControl("chk");
    

    然后你可以从那里获得相应的属性值。

    【讨论】:

    • @user1928473 那是因为你没有在其中插入任何值。你的 HTML 说这被设置为 visible = false。
    • 不,我填充了 itemdatabound 事件中的值并且它们被正确填充
    猜你喜欢
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多