0

我正在使用包含文本框、下拉列表等控件的转发器...我将它们填充到转发器的项目数据绑定事件中。它们已正确填充,问题是当我尝试在 button_click 事件中获取它们中的值时,我发现它们为空。这是 button_click 事件中的代码

Location loc = new Location();
            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是

    <asp:Repeater ID="repEdit" runat="server" OnItemDataBound="repEdit_ItemDataBound" OnItemCommand="repEdit_ItemCommand" ViewStateMode="Enabled" EnableViewState="true">
            <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>
 </asp:Repeater>

编辑***

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;
                    }
                }
                else if (kvp.Key.Contains("#bool"))
                {
                    chk.Visible = true;
                    chk.ID = kvp.Key;
                    chk.Attributes["ID"] = kvp.Key;
                    chk.EnableViewState = true;
                    string value = kvp.Value[0].ToString();
                    if (value.Contains("True"))
                    {
                        chk.Checked = true;
                    }
                }
                else if (kvp.Key.Contains("#string"))
                {
                    e.Item.FindControl("txt").ID = kvp.Key;
                    text.ID = kvp.Key;
                    text.EnableViewState = true;
                    text.Visible = true;
                    text.Attributes["ID"] = kvp.Key;
                    text.Text = kvp.Value[0].ToString();
                }

                else
                {
                    drp.ID = kvp.Key.Split('#')[0] + "#0";
                    drp.Attributes["id"] = drp.ID;
                    drp.EnableViewState = true;
                    drp.Visible = true;
                    drp.DataSource = kvp.Value;
                    drp.DataTextField = "Value";
                    drp.DataValueField = "Key";
                    drp.DataBind();
                    string x = kvp.Key.Split('#')[1];
                    ListItem temp = drp.Items.FindByValue(x);
                    if (temp != null)
                    {
                        drp.Items.FindByValue(x).Selected = true;
                    }
                }
            }
        }

所以有人能告诉我是什么问题吗?

4

0 回答 0