RadGrid Insert records using EditFormTemplate

6 posts, 1 answers
  1. Vinay
    Vinay avatar
    15 posts
    Member since:
    Mar 2013

    Posted 22 Jul 2014 Link to this post

    Hello Team,

    I am trying to insert the record using EditFormTemplate of RadGrid. I need to calculate the value based on the one textbox which is in editformtemplate and show the value in another textbox. I can get the controls if I edit the existing records by using grid.MasterTableView.get_insertItem().

    How can I get the template controls while adding new records.

    Please help..

    Thanks,

    Vinay
  2. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 22 Jul 2014 in reply to Vinay Link to this post

    Hello Vinay,

    Method 1:
    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" AllowPaging="true"
        PageSize="10" OnNeedDataSource="RadGrid1_NeedDataSource">
        <PagerStyle AlwaysVisible="true" />
        <MasterTableView CommandItemDisplay="Top">
            <Columns>
                <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                </telerik:GridBoundColumn>
            </Columns>
            <EditFormSettings EditFormType="Template">
                <FormTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </FormTemplate>
            </EditFormSettings>
        </MasterTableView>
    </telerik:RadGrid>
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        dynamic data = new[] {
          new { ID = 1, Name ="name1"},
          new { ID = 2, Name = "name2"},
          new { ID = 3, Name = "name3"},
          new { ID = 4, Name = "Name4"},
           new { ID = 5, Name ="name5"}};
        RadGrid1.DataSource = data;
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        TextBox TextBox1 = sender as TextBox;
        GridEditFormItem item = TextBox1.NamingContainer as GridEditFormItem;
        (item.FindControl("TextBox2") as TextBox).Text = TextBox1.Text;
    }


    Thanks,
    Jayesh Goyani
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Vinay
    Vinay avatar
    15 posts
    Member since:
    Mar 2013

    Posted 22 Jul 2014 in reply to Jayesh Goyani Link to this post

    Hi Jayesh,

    Thanks for your reply.

    I will implement the same and let you know how it goes on my end.

    Thanks,

    Vinay
  5. Vinay
    Vinay avatar
    15 posts
    Member since:
    Mar 2013

    Posted 23 Jul 2014 in reply to Vinay Link to this post

    Hi Jayesh,

    Sorry, but it is not working for me as the value gets to the 0 again as page refreshes as text box contains autopostback property to true.

    Can't we do it at client side?

    Thanks,

    Vinay
  6. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 23 Jul 2014 in reply to Vinay Link to this post

    Hi Vinay,

    Please try the following code snippet to set a TextBox value from clientside. Please note that the values in the TextBox will not persisted if the grid Rebinds. To restore the values in TextBox you will have to handle them manually.

    ASPX:
    <FormTemplate>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox
    </FormTemplate>

    C#:
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
      if (e.Item is GridEditableItem && e.Item.IsInEditMode)
      {
       GridEditableItem editItem = (GridEditableItem)e.Item;
       TextBox TextBox1 = (TextBox)editItem.FindControl("TextBox1");
       TextBox TextBox2 = (TextBox)editItem.FindControl("TextBox2");
       TextBox1.Attributes.Add("onBlur", "return TextChanged(" + TextBox1.ClientID + "," + TextBox2.ClientID + ")");
      }
    }

    JS:
    <script type="text/javascript">
        function TextChanged(txt1, txt2) {
            txt2.value = (txt1.value);
        }
    </script>

    Thanks,
    Princy
  7. Vinay
    Vinay avatar
    15 posts
    Member since:
    Mar 2013

    Posted 23 Jul 2014 in reply to Princy Link to this post

    Excellent Princy.

    Thanks for your reply and it is working like a charm.

    Thanks Again.

    Vinay
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017