This is a migrated thread and some comments may be shown as answers.

[Solved] Require new text with each update

1 Answer 49 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rick
Top achievements
Rank 1
Rick asked on 06 Mar 2013, 08:42 PM
I have a radgrid with a column for comments.  Its purpose is for the user to explain why they made a change to that particular item. To enable that I have made it a template column and in the EditItemTemplate, I've added a required field validator.  So far so good.  However, the requirement means that EVERY time a change is made, a new comment is required.  As it is, when the user clicks the edit button, the text from the previous comment is shown in the textbox.  If the user clicks update, the required field validator sees that text and validates the field. 
That code looks like this

 

<telerik:GridTemplateColumn DataField="Comments" AllowFiltering="false" FilterControlAltText="Filter Comments column" HeaderText="Comments" SortExpression="Comments" UniqueName="Comments">

 

<EditItemTemplate>

 

<asp:TextBox ID="txtComments" runat="server" Text='<%# Bind("Comments")%>' TextMode="MultiLine"></asp:TextBox>

 

<asp:RequiredFieldValidator ID="rfvComments" ControlToValidate="txtComments" Display="Dynamic" runat="server" ErrorMessage="Please enter a reason for changing this budget item."></asp:RequiredFieldValidator>

 

</EditItemTemplate>

 

<ItemTemplate>

 

<asp:Label ID="CommentsLabel" runat="server" Text='<%# Eval("Comments") %>'></asp:Label>

 

</ItemTemplate>

 

</telerik:GridTemplateColumn>




I tried replacing the  Text='<%# Bind("Comments")%>'  with  Text='' but then the new text was not returned.  ( A trace showed that parameter value to be null).  I have tried to keep the application simple and so am not currently using any code-behind.  If possible, I'd like to avoid that.

So, I'm looking for some help in how to clear the textbox when the edit button is pushed so the required field validator will do its work, while still having the new comment included when the page is posted back.

I appreciate any help you can give.
Thanks
Rick

 

 

 



1 Answer, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 07 Mar 2013, 05:05 AM
Hello,

Please try with below code snippet.

<MasterTableView>
               <Columns>
                   <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                   </telerik:GridBoundColumn>
                   <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                   </telerik:GridBoundColumn>
                   <telerik:GridTemplateColumn DataField="Comments" AllowFiltering="false" FilterControlAltText="Filter Comments column"
                       HeaderText="Comments" SortExpression="Comments" UniqueName="Comments">
                       <EditItemTemplate>
                           <asp:TextBox ID="txtComments" runat="server"  TextMode="MultiLine"></asp:TextBox>
                           <asp:RequiredFieldValidator ID="rfvComments" ControlToValidate="txtComments" Display="Dynamic"
                               runat="server" ErrorMessage="Please enter a reason for changing this budget item."></asp:RequiredFieldValidator>
                       </EditItemTemplate>
                       <ItemTemplate>
                           <asp:Label ID="CommentsLabel" runat="server" Text='<%# Eval("Comments") %>'></asp:Label>
                       </ItemTemplate>
                   </telerik:GridTemplateColumn>
                   <telerik:GridEditCommandColumn>
                   </telerik:GridEditCommandColumn>
               </Columns>
           </MasterTableView>
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        GridEditableItem item = e.Item as GridEditableItem;
        TextBox txtComments = item.FindControl("txtComments") as TextBox;
        string str1 = txtComments.Text; // Access new text
        string str2 = (RadGrid1.MasterTableView.Items[e.Item.ItemIndex]["Comments"].FindControl("CommentsLabel") as Label).Text; // Access Old Text
    }
 
  protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        dynamic data = new[] {
            new { ID = 1, Name ="Name1",Comments=""},
            new { ID = 2, Name = "Name2",Comments=""},
            new { ID = 3, Name = "Name3",Comments="ABC"},
             new { ID = 4, Name = "Name4",Comments="XYZ"},
            new { ID = 5, Name = "Name5",Comments=""}
        };
 
        RadGrid1.DataSource = data;
 
    }


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Rick
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Share this question
or