user1928473
user1928473

Reputation: 41

getting data from repeater controls

i have a repeater that contains different controls like textbox, dropdownlist,etc... the values of these controls are populated in the itemdatabound of the repeater. I want to get values from these controls, when I click on a button. This repeater is in a page that has masterpage.

The button code is as follows

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

                }
            }
        }

and the 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>

Upvotes: 0

Views: 1083

Answers (1)

Anjith K P
Anjith K P

Reputation: 2158

I have used Item.FindControl() method to get values. I need to get value inside ItemCommand function.

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

Like this you can get other values also from repeater. This worked for me, please try this.

Upvotes: 0

Related Questions