简体   繁体   中英

Telerik Radgrid retrieve TextBox value that was dynamically added to column

From code I add a TextBox to a column

protected void grdPartsBeingMonitored_ItemDataBound(object sender, GridItemEventArgs e)
{


if (e.Item is GridDataItem)
        {
            GridDataItem item = e.Item as GridDataItem;
            foreach (GridColumn column in item.OwnerTableView.RenderColumns)
            {

                    if (column.UniqueName == "MyColumn")
                    {
                        TextBox tbEditBox = new TextBox();
                        tbEditBox.Text = item[column].Text;
                        tbEditBox.Width = Unit.Pixel(50);
                        tbEditBox.ID = "editDemand";
                        item[column].Controls.Clear();
                        item[column].Controls.Add(tbEditBox);


                    }
        }
   }

Now how do I loop through each row and retrieve that rows value of the TextBox? Here's a start I believe:

foreach (GridDataItem item in grd1.MasterTableView.Items)
        {
            foreach (GridColumn column in item.OwnerTableView.RenderColumns)
            {
                if (column.UniqueName == "MyColumn")
                {
                      //HOW TO RETRIEVE VALUE OF THE TEXTBOX HERE???

I tried this with no luck

 foreach (Object c in item[column].Controls)
                    {
                        if (c.GetType() == typeof(TextBox))
                        {
                            TextBox tbEditBox = (TextBox)c;
                            System.Diagnostics.Debug.Write(tbEditBox.Text);

                        }

                    }

Please try with the below code snippet.

ASPX

<div>
    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource" OnItemDataBound="RadGrid1_ItemDataBound">
        <MasterTableView>
            <Columns>
                <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID"></telerik:GridBoundColumn>
                <telerik:GridTemplateColumn UniqueName="Name" DataField="Name">
                    <ItemTemplate>
                        <asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                        <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
    <asp:Button ID="Button1" runat="server" Text="Show edit" OnClick="Button1_Click" />
    <asp:Button ID="Button2" runat="server" Text="Get edit value" OnClick="Button2_Click" />
    <asp:Button ID="Button3" runat="server" Text="hide edit" OnClick="Button3_Click" />
</div>

ASPX.CS

public bool IsEditable
{
    get
    {
        if (ViewState["IsEdit"] == null)
            return false;
        else
            return (bool)ViewState["IsEdit"];
    }
    set
    {
        ViewState["IsEdit"] = value;
    }
}


protected void Page_Load(object sender, EventArgs e)
{

}

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    DataTable dt = new DataTable();
    dt.Columns.Add("ID", typeof(int));
    dt.Columns.Add("Name", typeof(string));
    dt.Rows.Add(1, "Name1");
    dt.Rows.Add(2, "Name2");
    dt.Rows.Add(3, "Name3");
    RadGrid1.DataSource = dt;
}

protected void Button1_Click(object sender, EventArgs e)
{
    IsEditable = true;
    RadGrid1.Rebind();
}
protected void Button2_Click(object sender, EventArgs e)
{
    foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
    {
        Response.Write((item.FindControl("txtName") as TextBox).Text);
        //perform your DB update here
    }


}
protected void Button3_Click(object sender, EventArgs e)
{
    IsEditable = false;
    RadGrid1.Rebind();


}

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = e.Item as GridDataItem;

        Label lblName = item.FindControl("lblName") as Label;
        TextBox txtName = item.FindControl("txtName") as TextBox;
        lblName.Visible = !IsEditable;
        txtName.Visible = IsEditable;

    }
}

Let me know if any concern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM