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

Getting/setting a value in editMode

15 Answers 878 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tim
Top achievements
Rank 1
Tim asked on 03 Jun 2008, 06:51 PM
I have a complex rule that requires getting a value in edit mode. depending on the on the value that was retrieved, set a value in another cell that is in editmode.  And, set a value in another cell on a previous row (if it exists).

 All this needs to be done by clicking a button other than the Update Button. (assuming I will be using the event RadGrid1_ItemCommad)

grid is AutoGenerateColumns="False"  EditMode="EditForms"

IS IT POSSIBLE?

Thanks

15 Answers, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 05 Jun 2008, 03:07 PM
Hi Tim,

It should be possible to do this in the ItemCommand event handler of RadGrid, where you can get the value of a control in the current item and set the value of another control, also inside the edit form of the current item. You could use something similar to:

void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
    if (e.CommandName == "MyCustomCommandName"
    { 
        GridEditableItem item = (GridEditableItem)e.Item; 
        GridEditManager manager = item.EditManager; 
        GridTextBoxColumnEditor editor = (GridTextBoxColumnEditor)manager.GetColumnEditor("ColumnUniqueName"); 
        //...Implement custom code here 
    } 


Best regards,
Veli
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Tim
Top achievements
Rank 1
answered on 05 Jun 2008, 06:38 PM
Let me rephrase this.

Here is a subset of the code.  Please, please, please do not give me an example with accessing the grid through an event arg.  I just want to access the values from the fields in the Formtemplate on a postback.

thanks

<

EditFormSettings EditFormType="Template">

<EditColumn UniqueName="EditCommandColumn1">

</EditColumn>

<FormTemplate>

<table id="table2" border="1" cellpadding="1" cellspacing="2" rules="none"

style="border-collapse: collapse" width="250">

<table id="table3" border="0" cellpadding="1" cellspacing="1" width="250">

<tr>

<td class="style16">

</td>

<td>

</td>

</tr>

<tr>

<td class="style16">

Industry Classif Value:

</td>

<td>

<telerik:RadComboBox ID="INDUSTRY_CLASSIF_VALUE" Text='<%# Eval( "INDUSTRY_CLASSIF_VALUE") %>' runat="server" DataSourceID="SdsGlobalIndustryCode"

DataTextField="GLOBAL_INDUSTRY_CODE" DataValueField="GLOBAL_INDUSTRY_CODE">

</telerik:RadComboBox>

</td>

</tr>

<tr>

<td class="style16">

Status Code:

</td>

<td>

<telerik:RadComboBox ID="SOURCE_OF_DATA_CODE" Text='<%# Eval( "SOURCE_OF_DATA_CODE") %>' runat="server" DataSourceID="SdsClassifStatusCode"

DataTextField="CLASSIF_STATUS_CODE" DataValueField="CLASSIF_STATUS_CODE">

</telerik:RadComboBox>

</td>

</tr>

<tr>

<td class="style16">

Effective From Date:

</td>

<td>

<telerik:RadDatePicker ID="RadDatePicker1" runat="server" DbSelectedDate = '<%# Eval( "EFF_FROM_DATE") %>'>

</telerik:RadDatePicker>

</td>

</tr>

<tr>

<td class="style16">

Effective Thru Date:

</td>

<td>

<telerik:RadDatePicker ID="RadDatePicker2" runat="server" DbSelectedDate = '<%# Eval( "EFF_THRU_DATE") %>'>

</telerik:RadDatePicker>

</td>

</tr>

<tr>

<td class="style16">

Notes:

</td>

<td>

<asp:TextBox ID="CLASSIF_NOTE" runat="server"

Text='<%# Eval( "CLASSIF_NOTE" ) %>' TextMode="MultiLine" CausesValidation="True">

</asp:TextBox>

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="CLASSIF_NOTE"></asp:RequiredFieldValidator>

</td>

</tr>

<tr>

<td class="style16">

Who Last Reviewed:

</td>

<td>

<asp:TextBox ID="WHO_LAST_REVIEWED" runat="server"

Text='<%# Eval( "WHO_LAST_REVIEWED" ) %>' TextMode="SingleLine" ReadOnly="True"></asp:TextBox>

</td>

</tr>

<tr>

<td class="style16">

Dtime Last Reviewed:

</td>

<td>

<asp:TextBox ID="DTIME_LAST_REVIEWED" runat="server"

Text='<%# Eval( "DTIME_LAST_REVIEWED" ) %>' TextMode="SingleLine" ReadOnly="True"></asp:TextBox>

</td>

</tr>

</table>

</td>

</tr>

<tr>

<td colspan="2">

<b>GICS Info:</b></td>

</tr>

<tr>

<td align="right" colspan="2">

<asp:Button ID="btnUpdate" runat="server"

CommandName='<%# (Container as GridItem).OwnerTableView.IsItemInserted ? "PerformInsert" : "Update" %>'

Text='<%# (Container as GridItem).OwnerTableView.IsItemInserted ? "Insert" : "Update" %>' />

<b>

<asp:Button ID="btnCancel" runat="server" CausesValidation="False"

CommandName="Cancel" Text="Cancel" />

</b>

<b>

<asp:Button ID="BtnReview" runat="server" CausesValidation="False"

CommandName="SpnyReview" Text="Review" onclick="BtnReview_Click1" />

</b>

&nbsp;

</td>

</tr>

</table>

</FormTemplate>

0
Veli
Telerik team
answered on 06 Jun 2008, 12:03 PM
Hi Tim,

If you need to access a control in a FormTemplate on a regular postback outside of RadGrid, please refer to the code below. Assuming we have a custom edit form template with a LinkButton inside, you need to loop through the items in RadGrid, check if they are in edit mode, and retrieve the respective GridEditFormItem, from where to find the control with its ID:

void Button1_Click(object sender, EventArgs e) 
    foreach (GridItem item in RadGrid1.MasterTableView.Items) 
    { 
        if (item.Edit) 
        { 
            GridEditFormItem editItem = (GridEditFormItem)RadGrid1.MasterTableView.GetItems(GridItemType.EditFormItem)[item.ItemIndex]; 
            LinkButton linkButton = (LinkButton)editItem.FindControl("LinkButton1"); 
            linkButton.Text = "zzz"
        } 
    } 


Kind regards,
Veli
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Tim
Top achievements
Rank 1
answered on 06 Jun 2008, 02:06 PM
Thanks.  This is exactly what I needed!

 I was struggling on getting the values out of the controls.  It seems that most/all the examples used are for auto generated edit forms.  And to get the values from this types of examples wasn't working for my situation because I used the custom edit form template scenarios (more flexibility).

I am creating complex data entry forms and validations.  Therefore, I need total access and control for all the data in the rows.  I have situations where a value on a row in a gridview affects multiples values in complete different gridviews on the same page.  whew!

BTW great support.  Thank you
0
Tim
Top achievements
Rank 1
answered on 09 Jun 2008, 04:58 PM
One more question.  I can get values from the edit form.  But I am still not able to get values from the rest of the rows in grid.  The following code in the "else" part is not working.  What am I doing wrong?  Thanks

foreach (GridItem item in RgSpnyGics.MasterTableView.Items)

{

if (item.Edit)

{

GridEditFormItem editItem = (GridEditFormItem)RgSpnyGics.MasterTableView.GetItems(GridItemType.EditFormItem)[item.ItemIndex];

TextBox whoReviewed = (TextBox)editItem.FindControl("WHO_LAST_REVIEWED");
whoReviewed.Text =
"ILLL";

}

else

{

GridDataItem rowItem = (GridDataItem)RgSpnyGics.MasterTableView.GetItems(GridItemType.Item)[item.ItemIndex];

TextBox dateReviewed = (TextBox)item.FindControl("DTIME_LAST_REVIEWED");

}

}


GridDataItem rowItem = (GridDataItem)RgSpnyGics.MasterTableView.GetItems(GridItemType.Item)[item.ItemIndex];

TextBox dateReviewed = (TextBox)item.FindControl("DTIME_LAST_REVIEWED");

0
Tim
Top achievements
Rank 1
answered on 09 Jun 2008, 04:59 PM
One more question.  I can get values from the edit form.  But I am still not able to get values from the rest of the rows in grid.  The following code in the "else" part is not working.  What am I doing wrong?  Thanks

foreach (GridItem item in RgSpnyGics.MasterTableView.Items)

{

if (item.Edit)

{

GridEditFormItem editItem = (GridEditFormItem)RgSpnyGics.MasterTableView.GetItems(GridItemType.EditFormItem)[item.ItemIndex];

TextBox whoReviewed = (TextBox)editItem.FindControl("WHO_LAST_REVIEWED");
whoReviewed.Text =
"ILLL";

}

else

{

GridDataItem rowItem = (GridDataItem)RgSpnyGics.MasterTableView.GetItems(GridItemType.Item)[item.ItemIndex];

TextBox dateReviewed = (TextBox)item.FindControl("DTIME_LAST_REVIEWED");

}

}


GridDataItem rowItem = (GridDataItem)RgSpnyGics.MasterTableView.GetItems(GridItemType.Item)[item.ItemIndex];

TextBox dateReviewed = (TextBox)item.FindControl("DTIME_LAST_REVIEWED");

0
Veli
Telerik team
answered on 10 Jun 2008, 06:57 AM
Hello Tim,

We are glad to hear we helped!

As for the code in the last thread, we need to mention that the "ELSE" clause is executed when the item is not in edit mode. This means that the controls in the edit form are not yet created on the server. Therefore, if TextBox with ID "DTIME_LAST_REVIEWD" is placed inside the edit form, it is not accessible if the item is not in edit mode. Please check if this is the case.

Best regards,
Veli
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Tim
Top achievements
Rank 1
answered on 10 Jun 2008, 02:01 PM

Wow.  That is indeed unfortunate.  I have asked this question repeatedly and was not given an answer until now.  Either Telerik help read the question hastily and responsed poorly or I just didn't understand.

To clarify ...

You cannot access values in a GridView unless you are in editmode.  IF THIS IS NOT THE CASE, PLEASE ,PLEASE,

PLEASE 

PROVIDE AN EXAMPLE. (please look at my example code)


Thank you

0
Veli
Telerik team
answered on 11 Jun 2008, 12:21 PM
Hello Tim,

Unfortunately creating all the controls inside the edit form for all the items in all the TableViews beforehand would be an undue burden for the performance of RadGrid. Therefore the edit form and the controls inside are created when an item goes into edit/insert mode. At any moment only the controls of the items currently in edit/insert mode are available and can be accessed in the code. Therefore, to make sure we are not accessing non-existing controls, in the ItemDataBound we check for the type of the item to be GridEditableItem and also if it is InEditMode. Only if these conditions are true, then the controls in the edit form have been created on the server and we can access them using FindControl().

To make sure I have covered everything in your question, let me say that values inside DataCells of a regular DataItem can be accessed any time by the unique name of the respective column:

void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridDataItem) 
        { 
            GridDataItem item = (GridDataItem)e.Item; 
            string value = item["ColumnUniqueName"].Text; 
        } 
    } 

But this is not true for controls inside edit forms, custom controls, form templates or the <EditItemTemplate> part of GridTemplateColumn.

Greetings,
Veli
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
sl6rp
Top achievements
Rank 1
answered on 16 Sep 2008, 10:33 PM
What if you want to do this with a new item (not editing but adding).  here is my code:
 private void SelectAllCheckBoxes(string checkBoxList, bool selected) 
    { 
        foreach (GridItem item in rgMessages.MasterTableView.Items) 
        { 
            if (item.Edit) 
            { 
                GridEditFormItem editItem = (GridEditFormItem)rgMessages.MasterTableView.GetItems(GridItemType.EditFormItem)[item.ItemIndex]; 
                CheckBoxList cbl = (CheckBoxList)editItem.FindControl(checkBoxList); 
 
                if (selected) 
                    for (int i = 0; i < cbl.Items.Count; i++) 
                    { 
                        cbl.Items[i].Selected = true
                    } 
                else 
                    for (int i = 0; i < cbl.Items.Count; i++) 
                    { 
                        cbl.Items[i].Selected = false
                    } 
            } 
        } 
    } 
    protected void chkProjectSelectAll(object sender, EventArgs e) 
    { 
        CheckBox cb = (CheckBox)sender; 
        SelectAllCheckBoxes("cblProjects", cb.Checked); 
    } 

This works if I am editing an existing row/item but not for a new one.





0
Veli
Telerik team
answered on 17 Sep 2008, 08:11 PM
Hello Tim,

The inserted item cannot be accessed as the other edited items. In fact, the inserted item cannot be accessed outside of RadGrid at all. It can only be accessed from within a command handler initiated by a control inside the inserted item. This means that you can only get a reference to the inserted item in the ItemCommand event where e.Item would be your inserted item.

The only exception to this rule is when the edit mode is "InPlace". In this case, when an item is inserted, it is a GridEditableItem, and can be referenced through the GetItems method.

RadGrid1.MasterTableView.GetItems(GridItemType.EditItem)[0] as GridEditableItem;

Regards,
Veli
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Alan
Top achievements
Rank 1
answered on 06 Feb 2009, 09:08 PM
Has the last part of this changed?  Where you can't access the inserted items except as part of the RadGrid command handler?

I want to do template editing.  When they edit or insert an item, I have an image button that they click and it launches a panel where they can search for an item.  When they find an item, I want it to put the item in the text box.  Works fine on the edit.  Just not on the insert.

If I can't do it server side, can I do it client side?  If so, how would I access a textbox in insert mode.

Thanks,
0
Sebastian
Telerik team
answered on 09 Feb 2009, 08:31 AM
Hi Alan,

I think that the projects from the following public forum posts will help you attain the functionality in question:

http://www.telerik.com/community/forums/aspnet/grid/update-with-external-button.aspx
http://www.telerik.com/community/forums/aspnet/window/controlling-radwindows-from-a-grid.aspx

To convert the code for the ASP.NET AJAX version of RadGrid, following the guidelines from the help article linked below:

http://www.telerik.com/help/aspnet-ajax/grdmigrationtoprometheus.html

Kind regards,
Sebastian
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
aashis
Top achievements
Rank 1
answered on 15 Feb 2011, 09:21 PM
Hi,
I'm doing the loop part
foreach (GridItem item in marketVolatilityGrid.MasterTableView.Items)
{
            if (item.Edit)
            {
                GridEditFormItem editItem = (GridEditFormItem)marketVolatilityGrid.MasterTableView.GetItems(GridItemType.EditFormItem)[item.ItemIndex];
                TextBox linkButton = (TextBox)editItem.FindControl("txtMarket_nme");
                string test = linkButton.Text;
            }


the "linkButton.Text" always returns old value to the string variable "test". Could you please help me on this..
I've been trying different approach to get new value from field since last week.

Thanks
0
Veli
Telerik team
answered on 16 Feb 2011, 09:29 AM
Hello aashish,

Can you explain what exactly is your scenario and what you need to achieve. Where do you call this code from?

Veli
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
Grid
Asked by
Tim
Top achievements
Rank 1
Answers by
Veli
Telerik team
Tim
Top achievements
Rank 1
sl6rp
Top achievements
Rank 1
Alan
Top achievements
Rank 1
Sebastian
Telerik team
aashis
Top achievements
Rank 1
Share this question
or