Telerik Radgrid检索动态添加到列的TextBox值

时间:2014-08-26 12:35:48

标签: c# telerik radgrid

从代码我添加一个TextBox到列

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


                    }
        }
   }

现在我如何遍历每一行并检索TextBox的行值?我相信这是一个开始:

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???

我试了这个没有运气

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

                        }

                    }

1 个答案:

答案 0 :(得分:0)

请尝试使用以下代码段。

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

    }
}

如果有任何疑虑,请告诉我。