0

我在 ItemDataBound 事件中从数据库填充的转发器中有一个 TextBox。当我尝试在 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;
                    }
                }
4

2 回答 2

2

这是你的中继器。它有一个 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);
        }
}

Get Value On Button Click 现在您需要为按钮创建事件处理程序并获取结果值

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;
            }
        }

}
于 2014-07-02T06:14:54.643 回答
0

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

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

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

于 2013-07-17T11:28:34.390 回答