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

[Solved] Problem in using Find Control in a Dynamically created Grid

8 Answers 202 Views
Grid
This is a migrated thread and some comments may be shown as answers.
MKDurga Durga
Top achievements
Rank 1
MKDurga Durga asked on 09 Nov 2009, 11:10 AM
Hello All,
       According to my functionality i need a grid dynamically generated in which first three columns are non editable and rest ( not known) are editable. So i buit it  from server side using Template columns and RADNumericTextbox. 

private

class myNTemplate : ITemplate

 

{

 

string TempField = string.Empty;

 

 

public myNTemplate(string Field)

 

{

TempField = Field;

}

#region

ITemplate Members

 

 

public void InstantiateIn(Control container)

 

{

 

RadNumericTextBox ntextBox = new RadNumericTextBox();

 

ntextBox.EnableEmbeddedSkins =

false;

 

ntextBox.Width =

Unit.Pixel(25);

 

ntextBox.ID =

"ntxt_" + TempField;

 

ntextBox.DataBinding +=

new EventHandler(ntextBox_DataBinding);

 

container.Controls.Add(ntextBox);

}

 

void ntextBox_DataBinding(object sender, EventArgs e)

 

{

 

RadNumericTextBox ntextBox = (RadNumericTextBox)sender;

 

 

GridDataItem container = (GridDataItem)ntextBox.NamingContainer;

 

 

string fieldValue = DataBinder.Eval(container.DataItem, TempField).ToString();

 

ntextBox.Text = (

string)((DataRowView)container.DataItem)[TempField];

 

}

#endregion

}

As the gris is ready i started entering values in each of mueric box in each column and need to insert into Db
As they were template columns i used FindControls to get the values and found only null was returning, not able to find the reason, for this iam sending my code for inserting

 

 

foreach (GridDataItem item in this.Rg_LOB.MasterTableView.Items)

 

{

_obj_smhr_lob.LOB_EMPID =

Convert.ToInt32(item.Cells[2].Text);

 

 

for (int j = 0; j < Rg_LOB.Columns.Count - 5; j++)

 

{

strQry.Append(

"EXEC USP_SMHR_LEAVEOPENINGBALANCES ");

 

_obj_smhr_lob.LOB_EMPID =

Convert.ToInt32(item.Cells[2].Text);

 

 

string LT = Convert.ToString(Rg_LOB.MasterTableView.Columns[j + 4].UniqueName);

 

 

string[] T = LT.Split(new char[] { '-' });

 

_obj_smhr_lob.LOB_LEAVETYPEID =

Convert.ToInt32(T[0]);

 

 

string ctrl = "ntxt_" + LT;

 

 

RadNumericTextBox ntxt;

 

ntxt = (

RadNumericTextBox)item[this.Rg_LOB.MasterTableView.Columns[j + 4].UniqueName].FindControl(ctrl);

 

 

_obj_smhr_lob.LOB_NOOFDAYS =

Convert.ToInt32(ntxt.Text);

 

_obj_smhr_lob.LOB_FINALISE = 0;

_obj_smhr_lob.LOB_CREATEDBY = 1;

_obj_smhr_lob.LOB_CREATEDDATE = System.

DateTime.Now;

 

_obj_smhr_lob.LOB_LASTMDFBY = 1;

_obj_smhr_lob.LOB_LASTMDFDATE = System.

DateTime.Now;

 

_obj_smhr_lob.OPERATION =

operation.Insert;

 

 

string str = "@Operation = 'Insert'" +

 

 

",@LOB_EMPID = '" + _obj_smhr_lob.LOB_EMPID + "'" +

 

 

",@LOB_LEAVETYPEID = '" + _obj_smhr_lob.LOB_LEAVETYPEID + "'" +

 

 

",@LOB_NOOFDAYS = '" + _obj_smhr_lob.LOB_NOOFDAYS + "'" +

 

 

",@LOB_FINALISE = '" + _obj_smhr_lob.LOB_FINALISE + "'" +

 

 

",@LOB_CREATEDBY = '" + _obj_smhr_lob.LOB_CREATEDBY + "'" +

 

 

",@LOB_CREATEDDATE = '" + _obj_smhr_lob.LOB_CREATEDDATE + "'" +

 

 

",@LOB_LASTMDFBY = '" + _obj_smhr_lob.LOB_LASTMDFBY + "'" +

 

 

",@LOB_LASTMDFDATE = '" + _obj_smhr_lob.LOB_LASTMDFDATE + "'";

 

strQry.Append(str);

}

}

I kept the line in bold where iam getting null. Please help me out in finding the error

Thanks
MKD

8 Answers, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 11 Nov 2009, 04:19 PM
Hi MKDurga,

Through debugging could you see the control in the Controls collection of the TableCell ( item[.....].Controls )?

Regards,
Tsvetoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
MKDurga Durga
Top achievements
Rank 1
answered on 12 Nov 2009, 05:04 AM
Hello Tsvetoslav,
When debugging I am getting control count as 0. For template Columns where we add control at serverside and give its ID, is there any other way to get the control. This is very important for me. My total functionality is stopped at this point. Please help me out.

Thanks & Regards
MKDurga
0
Tsvetoslav
Telerik team
answered on 16 Nov 2009, 04:11 PM
Hello MKDurga,

I have prepared a small sample which demonstrates the programmatic creation of a template column.  I hope it will help you with your own implementation. However, if the problem persists, I'd suggest that you open a formal support ticket and attach a runnable example of your project for us to debug since on the basis of the provided code snippets it is very difficult to pinpoint the problem

Thanks.

Best wishes,
Tsvetoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
MKDurga Durga
Top achievements
Rank 1
answered on 21 Nov 2009, 09:04 AM
Hello Tsvetoslav,
      I tried with your sent example and found that my scenario was different from that. My problem was , I was not able to fetch values from the control added runtime in grid . But in your example you were trying to fetch the control added dynamically. As I could not do it, I changed the method of dynamic generation with a normal grid at design time, generating columns at runtime. But I data from server side as my first 3 columns should be non editable and rest ( dynamic) using GRidEditable column.

void GetGridStructure()
    {
        DataTable dt;
        dt = BLL.get_EmpLOB(-1);

        Rg_LOB.Columns.Clear();

        GridBoundColumn GCol = new GridBoundColumn();
        GCol.DataField = "EMP_ID";
        GCol.HeaderText = "ID";
        GCol.UniqueName = "EMP_ID";
        GCol.Visible = false;
        GCol.ReadOnly = true;
        Rg_LOB.Columns.Add(GCol);


        GCol = new GridBoundColumn();
        GCol.DataField = "EMP_CODE";
        GCol.HeaderText = "Employee Code";
        GCol.UniqueName = "EMP_CODE";
        GCol.ReadOnly = true;
        Rg_LOB.Columns.Add(GCol);

        GCol = new GridBoundColumn();
        GCol.DataField = "EMP_NAME";
        GCol.HeaderText = "Employee Name";
        GCol.UniqueName = "EMP_NAME";
        GCol.ReadOnly = true;
        Rg_LOB.Columns.Add(GCol);

        GCol = new GridBoundColumn();
        GCol.DataField = "EMP_DOJ";
        GCol.HeaderText = "Date of Join";
        GCol.UniqueName = "EMP_DOJ";
        GCol.ReadOnly = true;
        Rg_LOB.Columns.Add(GCol);

        for (int i = 4; i < dt.Columns.Count; i++)
        {
            GCol = new GridBoundColumn();
            GCol.DataFormatString = "{0:C}";
            GCol.DataType = typeof(double);
            GCol.HeaderStyle.Width = Unit.Percentage(10);
            GCol.DataField = dt.Columns[i].ColumnName;
            GCol.HeaderText = dt.Columns[i].ColumnName.Split(new char[] { '-' })[1];
            GCol.UniqueName = dt.Columns[i].ColumnName;
            Rg_LOB.Columns.Add(GCol);
        }

        GridEditCommandColumn gedit = new GridEditCommandColumn();
        gedit.UniqueName = "gedit";
        Rg_LOB.Columns.Add(gedit);
    }

Presently when iam opening grid in Edit Mode = 'In Place', iam getting an error
Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Specified argument was out of the range of valid values.Parameter name: index

 in IE7 browser.  I am getting this error if click on normal asp save button or any where on grid.

I was debugging the teleric examples but they were running fine. I could not copy paste the same grid from  example as my grid structure was different.
Please help me, because of this issue iam running late in my delivarables. For your complete reference iam attaching both my .aspx and .cs files, please find them


Iam unable to attach a rar or zip file.





0
Tsvetoslav
Telerik team
answered on 24 Nov 2009, 02:36 PM
Hello MKDurga,

Unfortunately, I am not able to understand your scenario neither what exactly the issue is. I thought that you needed your grid generated dynamically in code-behind. Do note that you cannot attach files to forum posts. You need to open up a support ticket and attach the files to the formal support ticket. Please, do so and we will debug your project and get back to you with our findings.

Regards,
Tsvetoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
MKDurga Durga
Top achievements
Rank 1
answered on 25 Nov 2009, 01:40 PM
Hello Tsvetoslav,

    I found why I was getting unhandled exception of Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Specified argument was out of the range of valid values.Parameter name: index.
I have a update command in which i want to update row , there in

e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
here new values is returning null, the error is coming here. and code for that event is     

--Update_Command

 

 

GridEditableItem editedItem = e.Item as GridEditableItem;

 

 

 

DataTable newTable = GridSource();

 

 

//Locate the changed row in the DataSource

 

 

 

 

 

DataRow[] changedRows = newTable.Select("emp_id = " +

 

editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex][

"emp_id"].ToString());

 

 

if (changedRows.Length != 1)

 

{

BLL.ShowMessage(

this, "Unable to locate the Order for updating");

 

e.Canceled =

true;

 

 

return;

 

}

 

Hashtable newValues = new Hashtable();

 

 

// editedItem.ExtractValues(newValues);

 

 

 

 

e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);

changedRows[0].BeginEdit();

 

try

 

 

 

 

{

 

foreach (DictionaryEntry entry in newValues)

 

{

changedRows[0][(

string)entry.Key] = entry.Value;

 

}

changedRows[0].EndEdit();

}

 

catch (Exception ex)

 

{

changedRows[0].CancelEdit();

BLL.ShowMessage(

this, "Unable to update EmpRecords. Reason: " + ex.Message);

 

e.Canceled =

true;

 

}

///ASPX Page///

 

 

<telerik:RadGrid ID="Rg_LOB" runat="server" AutoGenerateColumns="False" EnableEmbeddedSkins="False"

 

 

 

 

 

AllowPaging="True" OnNeedDataSource="Rg_LOB_NeedDataSource" GridLines="None"

 

 

 

 

 

AllowMultiRowEdit="true" OnUpdateCommand="Rg_LOB_UpdateCommand">

 

 

 

 

 

<MasterTableView EditMode="InPlace" DataKeyNames="emp_id" AllowAutomaticUpdates="true">

 

 

 

 

 

<EditFormSettings>

 

 

 

 

 

<EditColumn InsertImageUrl="Update.gif" UpdateImageUrl="Update.gif" EditImageUrl="Edit.gif"

 

 

 

 

 

CancelImageUrl="Cancel.gif">

 

 

 

 

 

</EditColumn>

 

 

 

 

 

</EditFormSettings>

 

 

 

 

 

</MasterTableView>

 

 

 

 

 

<HeaderContextMenu EnableEmbeddedSkins="False">

 

 

 

 

 

</HeaderContextMenu>

 

 

 

 

 

<PagerStyle AlwaysVisible="True" />

 

 

 

 

 

<FilterMenu EnableEmbeddedSkins="False">

 

 

 

 

 

</FilterMenu>

 

 

 

 

 

</telerik:RadGrid>

 

 

 

 

why am i not getting values in newvalues variable, i read from other forum posts that grid should not be rebinded and for a normal editable grid (without any controls) e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem) works, why its not working for me.

Please  help me out, hope atleast this time  I am clear.

0
MKDurga Durga
Top achievements
Rank 1
answered on 26 Nov 2009, 01:38 PM
Hello Tsvetoslav,
 I feel my issue is solved to 50% as I understood where is the issue ? :)
As I told You in my previous messages I could get rid of that Unhandled exception if I rebind the grid.  But if I rebind the grid Iam loosing the new values, without rebinding iMa unable to get values in  newValues HashTable variable.
Hashtable newValues = new Hashtable();
e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);


here newvalues in returning null.

Hope Iam clear in my messge.Hope to see a solution to my issue.

regards,
MKDurga




0
Nikolay Rusev
Telerik team
answered on 30 Nov 2009, 09:35 AM
Hello MKDurga,

I am not sure what goes wrong in your scenario, but as you can see on the following demo same approach is working just fine:
http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/extractvalues/defaultcs.aspx
Please examine the code in the demo above and see whether there are some differences.

Regards,
Nikolay
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
MKDurga Durga
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
MKDurga Durga
Top achievements
Rank 1
Nikolay Rusev
Telerik team
Share this question
or