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

Command in EditMode/InsertMode

4 Answers 106 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dave
Top achievements
Rank 1
Dave asked on 22 Sep 2010, 10:11 AM
Hello!

I've a RadGrid where the User can manage some data.

In Edit- or Insert-Mode the User should be able to provide some data, the data will be checked against a Webservice and the result should be visible in Edit-Mode so the User can view them and finally Click "Finsh" to save the data and end the Edit-Mode.

My Problem is, that I don't know how to handle the Button-Click, where the data is checked.

In ItemCommand I can handle the Button-Event, but I have no access to the EditForm (as far as I know)

protected void DataRadGridItemCommand(object source, GridCommandEventArgs e)
{
    switch (e.CommandName)
    {
        case "CheckData":
            //Check data
            break;
    }
}

In ItemDataBound I have access to the Edit-Form, butI don't know how to handle the Button-Event.

protected void DataRadGridOnItemDataBound(object sender, GridItemEventArgs e)
{
    if (!(e.Item is GridDataItem)) return;
 
    if (e.Item.IsInEditMode)
    {
        var item = (GridEditableItem) e.Item;
        // Access the EditForm
    }
}

Any suggestions, hints?

Thanks in advance.

Dieter

4 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 22 Sep 2010, 12:17 PM
Hello Dieter,

I guess the 'CheckData' button is inside edit form. Then one way to achieve this is by attaching an 'OnClick' event to that 'CheckData' Button. And in the event handler access the edit form using NamingContainer propery of button.

ASPX:
<Columns>
    <telerik:GridBoundColumn DataField="FirstName" UniqueName="FirstName" HeaderText="FirstName">
    </telerik:GridBoundColumn>
     <telerik:GridTemplateColumn>
        <EditItemTemplate>
            <asp:Button ID="Button2" runat="server" Text="CheckData" CommandName="CheckData" OnClick="Button2_Click" />
        </EditItemTemplate>
    </telerik:GridTemplateColumn>
</Columns>

C#:
protected void Button2_Click(object sender, EventArgs e)
   {
       Button chkButton = (Button)sender;
       GridEditFormItem editItem = (GridEditFormItem)chkButton.NamingContainer; // access editform
       TextBox txt = (TextBox)editItem["FirstName"].Controls[0];
       string value = txt.Text;
   }

You can also access the edit form in ItemCommand event like below.

C#:
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
   {
       switch (e.CommandName)
       {
           case "CheckData":
               {
                   Button chkButton = (Button)e.CommandSource;
                   GridEditFormItem editItem = (GridEditFormItem)chkButton.NamingContainer;
                   TextBox txt = (TextBox)editItem["FirstName"].Controls[0];
                   string value = txt.Text;
               }
       }
   }

Thanks,
Princy.
0
Dave
Top achievements
Rank 1
answered on 22 Sep 2010, 02:04 PM
Thank You, Princy. It did the job :->
0
Dave
Top achievements
Rank 1
answered on 23 Sep 2010, 07:59 AM
Hello!

Sorry, but now I'm facing another problem:

protected void DataRadGridItemCommand(object source, GridCommandEventArgs e)
{
    switch (e.CommandName)
    {
        case "CheckData":
            var chkButton = (Button)e.CommandSource;
            var editItem = (GridEditFormItem)chkButton.NamingContainer;
 
            var tb1 = (TextBox)editItem.FindControl("TextBox1");
 
            var result = CheckData(tb1.Text);
 
            var tb2 = (TextBox)editItem.FindControl("TextBox2");
            tb2.Text = result;
 
            break;
    }
}

The Text of the TextBox2 stays empty, even if I try this

protected void DataRadGridItemCommand(object source, GridCommandEventArgs e)
{
    switch (e.CommandName)
    {
        case "CheckData":
            var chkButton = (Button)e.CommandSource;
            var editItem = (GridEditFormItem)chkButton.NamingContainer;
 
            var tb2 = (TextBox)editItem.FindControl("TextBox2");
            tb2.Text = "TEST";
 
            break;
    }
}

Maybe there's a Bind-Action on the Grid afterwards. I tried several ways to avoid this, but without luck.

Thanks for any hint.

Dieter
0
Accepted
Daniel
Telerik team
answered on 28 Sep 2010, 09:22 AM
Hello Dieter,

Your code runs fine on my end. Please download the attached project and let me know if I'm missing something.

Regards,
Daniel
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Dave
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Dave
Top achievements
Rank 1
Daniel
Telerik team
Share this question
or