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

How to change a hidden field value before inseert in Radgrid

6 Answers 449 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Amy Liu
Top achievements
Rank 1
Amy Liu asked on 30 Jun 2010, 04:46 PM
Hello!

I have built a grid report with edit and insert feature. so far everything is fine except for validation which I will do it next and the problem I face below:

I have two fileds --SID and Username ( we are not talking about database design problem here) which are related. so one sid to one username ( unique).

I have hidden sid and username in Edit form and use ForceExtractValue="InEditMode" which works in insert and edit. I used codebehind to hide them in edit mode and show them in insert mode.
    protected void RadGrid3_ItemCommand(object source, GridCommandEventArgs e)  
    {  
        ///I have to hide the fields SID and Username in the edit mode but show them in the insert mode ///  
        RadGrid grid = (source as RadGrid);  
        if (e.CommandName == RadGrid.InitInsertCommandName)  
        {  
            grid.MasterTableView.ClearEditItems();  
            RadGrid3.MasterTableView.GetColumn("SID").EditFormHeaderTextFormat = "SID:";  
            RadGrid3.MasterTableView.GetColumn("Username").EditFormHeaderTextFormat = "Username:";  
        }  
        if (e.CommandName == RadGrid.EditCommandName)  
        {  
            e.Item.OwnerTableView.IsItemInserted = false;  
            RadGrid3.MasterTableView.GetColumn("SID").EditFormHeaderTextFormat = "";  
            RadGrid3.MasterTableView.GetColumn("Username").EditFormHeaderTextFormat = "";  
        }    
 
        ///show export  
        if (e.CommandName == Telerik.Web.UI.RadGrid.ExportToExcelCommandName ||  
            e.CommandName == Telerik.Web.UI.RadGrid.ExportToWordCommandName ||  
            e.CommandName == Telerik.Web.UI.RadGrid.ExportToCsvCommandName ||  
            e.CommandName == Telerik.Web.UI.RadGrid.ExportToPdfCommandName)  
        {  
            ConfigureExport();  
        }  
    }  
 
    protected void RadGrid3_ItemDataBound(object sender, GridItemEventArgs e)  
    {  
        // update form     
        if (e.Item is GridEditFormItem && e.Item.IsInEditMode && e.Item.OwnerTableView.IsItemInserted == false)  
        {  
            GridEditFormItem editFormItem = (GridEditFormItem)e.Item;  
            editFormItem["SID"].Visible = false;  
            editFormItem["Username"].Visible = false;  
        }  
        // insert     
        else if (e.Item is GridEditFormInsertItem && e.Item.IsInEditMode && e.Item.OwnerTableView.IsItemInserted == true)  
        {  
            RadGrid3.MasterTableView.ClearEditItems();  
        }    
 
    } 


Now I want to when user input anything in sid or username field the other field is field with right data.
Or I want to hide sid and only show username in Insert mode. when user input username and I will get sid from database and add the value into the sid insert parameter then do insert.

How could I accomplish this.

6 Answers, 1 is accepted

Sort by
0
Veli
Telerik team
answered on 06 Jul 2010, 07:14 AM
Hello Amy Liu,

To update the value of one field after another field has changed in the RadGrid edit form, you can set the fields to AutoPostBack and handle their OnTextChanged events. Inside the event handlers, the NamingContainer of your textboxes is the GridEditFormItem that is your edit form. You can use it to find the other respective textbox and set its value:

protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
    {
        GridEditFormItem editForm = (GridEditFormItem)e.Item;
        ((TextBox)editForm["SID"].Controls[0]).AutoPostBack = true;
        ((TextBox)editForm["SID"].Controls[0]).TextChanged += new EventHandler(SIDBox_TextChanged);
 
        ((TextBox)editForm["Username"].Controls[0]).AutoPostBack = true;
        ((TextBox)editForm["Username"].Controls[0]).TextChanged += new EventHandler(UsernameBox_TextChanged);
        }
}
 
protected void SIDBox_TextChanged(object sender, EventArgs e)
{
    TextBox SIDBox = (TextBox)sender;
    GridEditFormItem editForm = (GridEditFormItem)SIDBox.NamingContainer;
    ((TextBox)editForm["Username"].Controls[0]).Text = "set Username here";
}
 
protected void UsernameBox_TextChanged(object sender, EventArgs e)
{
    TextBox UsernameBox = (TextBox)sender;
    GridEditFormItem editForm = (GridEditFormItem)UsernameBox.NamingContainer;
    ((TextBox)editForm["SID"].Controls[0]).Text = "set SID here";
}

The other scenario you mention is to have the user fill one of the fields, the other getting set automatically before insert. You can use the ItemCommand event of the grid to implement this behavior when the command is of type PerformInsertCommand:

protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.PerformInsertCommandName)
    {
        GridEditFormInsertItem editForm = (GridEditFormInsertItem)e.Item;
        ((GridTextBoxColumnEditor)editForm.EditManager.GetColumnEditor("SID")).Text = "set SID here";
    }
}


Sincerely yours,
Veli
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
0
Vin
Top achievements
Rank 1
answered on 16 Nov 2010, 06:23 PM
Attempting to use this code, VS 2010 .NET 4 w/ latest tools.... receiving the following error:

Unable to cast object of type 'Telerik.Web.UI.GridDataInsertItem' to type 'Telerik.Web.UI.GridEditFormInsertItem'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Unable to cast object of type 'Telerik.Web.UI.GridDataInsertItem' to type 'Telerik.Web.UI.GridEditFormInsertItem'.

Source Error:

Line 125:        if (e.CommandName == RadGrid.PerformInsertCommandName)
Line 126:        {
Line 127: GridEditFormInsertItem editForm = (GridEditFormInsertItem)e.Item;

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1
RadControls: v2010.2.929.40 Trial
0
Veli
Telerik team
answered on 17 Nov 2010, 02:49 PM
Hi Vin,

You seem to be using EditMode="InPlace" where the editor fields are rendered on the same data item. Try casting the e.Item to GridDataInsertItem instead:

protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.PerformInsertCommandName)
    {
        GridDataInsertItem editForm = (GridDataInsertItem)e.Item;
        ((GridTextBoxColumnEditor)editForm.EditManager.GetColumnEditor("SID")).Text = "set SID here";
    }
}

Everything else can stay the same.

Veli
the Telerik team
Browse the vast support resources we have to jumpstart 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.
0
Helmy
Top achievements
Rank 1
answered on 07 Feb 2011, 05:57 PM
when i tried your code i get this error "  Item in insert mode does implement indexer only when the edit form is autogenerated"
as im not using autogenerate for edit form .
 plz give me the solution
0
Princy
Top achievements
Rank 2
answered on 08 Feb 2011, 08:09 AM
Hello Helmi,

You can access the control in edit form and set the value.
C#:
protected void RadGrid2_ItemCommand(object sender, GridCommandEventArgs e)
   {
       if (e.CommandName == "PerformInsert")
       {
           GridEditFormInsertItem editForm = (GridEditFormInsertItem)e.Item;
           TextBox txtid = (TextBox)editForm.FindControl("TextBox1");
           txtid.Text = "set SID here";
        }
   }

Hope this helps,
Princy.
0
Helmy
Top achievements
Rank 1
answered on 08 Feb 2011, 03:37 PM
Thank you very much Princy, it works well thanks for your greate efforts .

  

 
Tags
Grid
Asked by
Amy Liu
Top achievements
Rank 1
Answers by
Veli
Telerik team
Vin
Top achievements
Rank 1
Helmy
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or