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

GridTemplateColumn Can't retrieve value from Textbox

3 Answers 155 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Martin
Top achievements
Rank 1
Martin asked on 28 Oct 2008, 10:28 AM
Hi Telerik.

Im having problems by retrieving the values entered into a Textbox in the Grid which is wrapped in a GridTemplateColumn.

What i want to do: I want to have one textbox for each row in the grid. then when the user clicks a button i want to loop through the whole grid and save the DataKeyValue and the value enterd in the text box in a StringDictionary for later use...

my acx file look like below
  <telerik:RadGrid ID="RadGrid1" runat="server" PageSize="20" AllowSorting="True" Skin="Black" AllowPaging="True" 
            ShowGroupPanel="True" AutoGenerateColumns="False" GridLines="None">  
            <PagerStyle Mode="NumericPages"></PagerStyle> 
            <MasterTableView Width="100%" TableLayout="Fixed" EditMode="InPlace" DataKeyNames="ProfitCenterNo" Name="Master">  
                <GroupByExpressions> 
                    <telerik:GridGroupByExpression> 
                        <SelectFields> 
                            <telerik:GridGroupByField FieldAlias="CostPlace" HeaderText=" " FieldName="CostPlace"></telerik:GridGroupByField> 
                        </SelectFields> 
                        <GroupByFields> 
                            <telerik:GridGroupByField FieldName="CostPlace"></telerik:GridGroupByField> 
                        </GroupByFields> 
                    </telerik:GridGroupByExpression> 
                </GroupByExpressions> 
                <Columns>                           
                    <telerik:GridTemplateColumn UniqueName="NewTargetCol" HeaderText="Enter Target" Visible="false">  
                        <ItemTemplate> 
                            <asp:Panel ID="Panel1" runat="server">  
                                <asp:TextBox ID="NewTarget" runat="server" Text='<%# Bind("Target") %>'></asp:TextBox>                                  
                            </asp:Panel> 
                        </ItemTemplate> 
                    </telerik:GridTemplateColumn>                                     
                      
                    <telerik:GridBoundColumn SortExpression="CostPlace" HeaderText="CostPlace" HeaderButtonType="TextButton" Visible="false" 
                        DataField="CostPlace">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn SortExpression="ProfitCenterNo" HeaderText="" HeaderButtonType="TextButton" Visible="false" 
                      DataField="ProfitCenterNo">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn ReadOnly="true" SortExpression="ProfitCenter" HeaderText="ProfitCenter" HeaderButtonType="TextButton"   
                        DataField="ProfitCenter">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn ReadOnly="false" SortExpression="TurnOver" HeaderText="TurnOver" HeaderButtonType="TextButton" 
                        DataField="TurnOver">  
                        <HeaderStyle/> 
                    </telerik:GridBoundColumn>                      
                    <telerik:GridBoundColumn SortExpression="Target" HeaderText="Target" HeaderButtonType="TextButton" UniqueName="Target"   
                        DataField="Target">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn ReadOnly="true" SortExpression="GrwLastHour" HeaderText="Growth last hour" HeaderButtonType="TextButton" 
                        DataField="GrwLastHour">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn ReadOnly="true" SortExpression="SpPrPax" HeaderText="Spending per pax" HeaderButtonType="TextButton" 
                        DataField="SpPrPax">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn ReadOnly="true" SortExpression="TgPrPax" HeaderText="Target per pax" HeaderButtonType="TextButton" 
                        DataField="TgPrPax">  
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn ReadOnly="true" SortExpression="RmPrPax" HeaderText="Remaining per pax" HeaderButtonType="TextButton" 
                        DataField="RmPrPax">  
                    </telerik:GridBoundColumn> 
                </Columns>                
 
                  
            </MasterTableView> 
 
</telerik:RadGrid> 
<asp:Button ID="Button2" runat="server" Text="Button" onclick="Button2_Click" /> 


My Code behind where i loop throught the items in the grid and svaed the data to a string dictionary looks like this

       protected void Button2_Click(object sender, EventArgs e)  
        {  
 
            string profitCenterNo = "";  
            string profitCenterTarget= "";  
 
            StringDictionary sdProfitCenter = new StringDictionary();  
 
            foreach (GridDataItem item1 in RadGrid1.Items)  
            {  
                if (item1.OwnerTableView.Name == "Master")  
                {  
                    profitCenterNo = item1.GetDataKeyValue("ProfitCenterNo").ToString();  
                    profitCenterTarget = (item1["NewTargetCol"].FindControl("NewTarget"as TextBox).Text;  
                      
                }  
 
                sdProfitCenter.Add(profitCenterNo, profitCenterTarget);  
            }    
 
 
        } 

but im not able to retrieve the value from the textbox with this code. i only get a blank value.

I have the same problems if i try to use the build in edit functions in Radgrid. I cant switch to edit mode for the individual row enter my changes but im not able to catch the new values entered...

Im looking forward to you quick response.

Martin L

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 28 Oct 2008, 11:45 AM
Hi Martin,

Try accessing the Textbox in the GridTemplateColumn as shown below.

CS:
         
 
 
protected void Button2_Click(object sender, EventArgs e)   
        {   
  
            string profitCenterNo = "";   
            string profitCenterTarget"";   
  
            StringDictionary sdProfitCenter = new StringDictionary();   
  
            foreach (GridDataItem item1 in RadGrid1.Items)   
            {   
                if (item1.OwnerTableView.Name == "Master")   
                {   
                    profitCenterNo = item1.GetDataKeyValue("ProfitCenterNo").ToString();   
                     
                    //Access the panel 
                    Panel panel = (Panel)item1["NewTargetCol"].FindControl("Panel1"); 
 
                   // Access the TextBox 
                    TextBox txtbx = (TextBox)panel.FindControl("NewTarget"); 
                    profitCenterTarget = txtbx.Text; 
 
                       
                }   
  
                sdProfitCenter.Add(profitCenterNo, profitCenterTarget);   
            }     
  
  
        }  


Thanks
Shinu.
0
Martin
Top achievements
Rank 1
answered on 28 Oct 2008, 12:23 PM
Hi Shinu

thanks for the quick reply

I tried your sugestion. But still have problems, when retrieving the value it returns the Old values. 

ex: If the loaded value was "30" and i changes it to "50" in the textbox it returns "30" to me..

how can i get the new values ??

Martin L

0
Shinu
Top achievements
Rank 2
answered on 29 Oct 2008, 05:13 AM
Hi Martin,

I tried this on my end and I am getting the changed values in the button click event. Here is the code which I tried.


ASPX:
<telerik:GridTemplateColumn    HeaderText="TempCol"  DataField="ProductName"  UniqueName="TempCol" > 
  <ItemTemplate> 
    <asp:Panel ID="Panel1" runat="server"
     <asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("ProductName") %>'></asp:TextBox> 
    </asp:Panel> 
  </ItemTemplate> 
</telerik:GridTemplateColumn> 

CS:
protected void Button1_Click(object sender, EventArgs e) 
    { 
        foreach (GridDataItem item in RadGrid1.MasterTableView.Items) 
        { 
            Panel panel = (Panel)item["TempCol"].FindControl("Panel1"); 
            TextBox txtbx = (TextBox)panel.FindControl("TextBox2"); 
            string strtxt = txtbx.Text; 
        } 
    } 


Thanks
Shinu.
Tags
Grid
Asked by
Martin
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Martin
Top achievements
Rank 1
Share this question
or