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

[Solved] Radgrid Update Parameter Strange behavior in FormTemplate Update Event.

2 Answers 148 Views
Grid
This is a migrated thread and some comments may be shown as answers.
gc_0620
Top achievements
Rank 1
gc_0620 asked on 08 Jan 2010, 09:14 PM

Folks,

 

Environment: RadControls for ASP.NET AJAX Q2 2009 SP1 with Visual Studio 2008 SP1.  I am using Northwind Employee Table as prototype for my RadGrid and Form template with just 5 columns ([EmployeeID], [LastName], [FirstName], [City], [Country]).

 

Currently, if DataField ‘Country’ is Null it updates the Datafield value to “USA”. However if it is not Null, it does not update the Datafield value to “USA”; keeps the edited value.

 

My intention: 

1)  For each rows, I would like to assign automatically
SqlDataSource1.UpdateParameters["Country"].DefaultValue = “USA"  (i.e. GridBoundColumnCountry’ value  be “USA”) after each RadGrid1_UpdateCommand whether it is null or has any existing values.

 

2) Can I change the GridBoundColumnCountry’ to read only so that user’s can’t make any changes to that column? But I should be to update it automatically as per #1.

 

Below is my codes. Thanks for any help.

 

GC_0620



_________
.ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
 
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title></title>  
</head> 
<body> 
    <form id="form1" runat="server">  
    <div> 
      
        <telerik:RadScriptManager ID="RadScriptManager1" Runat="server">  
        </telerik:RadScriptManager> 
      
    </div> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"   
        ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString %>"   
        DeleteCommand="DELETE FROM [Employees] WHERE [EmployeeID] = @EmployeeID"   
        InsertCommand="INSERT INTO [Employees] ([LastName], [FirstName], [City], [Country]) VALUES (@LastName, @FirstName, @City, @Country)"   
        SelectCommand="SELECT [EmployeeID], [LastName], [FirstName], [City], [Country] FROM [Employees] ORDER BY [LastName]"   
        UpdateCommand="UPDATE [Employees] SET [LastName] = @LastName, [FirstName] = @FirstName, [City] = @City, [Country] = @Country WHERE [EmployeeID] = @EmployeeID">  
        <DeleteParameters> 
            <asp:Parameter Name="EmployeeID" Type="Int32" /> 
        </DeleteParameters> 
        <UpdateParameters> 
            <asp:Parameter Name="LastName" Type="String" /> 
            <asp:Parameter Name="FirstName" Type="String" /> 
            <asp:Parameter Name="City" Type="String" /> 
            <asp:Parameter Name="Country" Type="String" /> 
            <asp:Parameter Name="EmployeeID" Type="Int32" /> 
        </UpdateParameters> 
        <InsertParameters> 
            <asp:Parameter Name="LastName" Type="String" /> 
            <asp:Parameter Name="FirstName" Type="String" /> 
            <asp:Parameter Name="City" Type="String" /> 
            <asp:Parameter Name="Country" Type="String" /> 
        </InsertParameters> 
    </asp:SqlDataSource> 
    <telerik:RadGrid ID="RadGrid1" runat="server" AllowFilteringByColumn="True"   
        AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"   
        AllowAutomaticDeletes="True" AllowAutomaticInserts="True" 
            AllowAutomaticUpdates="True" 
        DataSourceID="SqlDataSource1" GridLines="None"   
        AutoGenerateDeleteColumn="True" AutoGenerateEditColumn="True"   
        onupdatecommand="RadGrid1_UpdateCommand" > 
<MasterTableView datakeynames="EmployeeID" CommandItemDisplay="TopAndBottom"  Name = "Master" datasourceid="SqlDataSource1">  
<RowIndicatorColumn> 
<HeaderStyle Width="20px"></HeaderStyle> 
</RowIndicatorColumn> 
 
<ExpandCollapseColumn> 
<HeaderStyle Width="20px"></HeaderStyle> 
</ExpandCollapseColumn> 
    <Columns> 
        <telerik:GridBoundColumn DataField="EmployeeID" DataType="System.Int32"   
            HeaderText="EmployeeID" ReadOnly="True" SortExpression="EmployeeID"   
            UniqueName="EmployeeID">  
        </telerik:GridBoundColumn> 
        <telerik:GridBoundColumn DataField="LastName" HeaderText="LastName"   
            SortExpression="LastName" UniqueName="LastName">  
        </telerik:GridBoundColumn> 
        <telerik:GridBoundColumn DataField="FirstName" HeaderText="FirstName"   
            SortExpression="FirstName" UniqueName="FirstName">  
        </telerik:GridBoundColumn> 
        <telerik:GridBoundColumn DataField="City" HeaderText="City"   
            SortExpression="City" UniqueName="City">  
        </telerik:GridBoundColumn> 
        <telerik:GridBoundColumn DataField="Country" HeaderText="Country"     
            SortExpression="Country" UniqueName="Country">  
        </telerik:GridBoundColumn> 
    </Columns> 
                    <EditFormSettings EditFormType="Template">  
                    <FormTemplate> 
                        <table id="Table2" cellspacing="2" cellpadding="1" width="100%" border="0" rules="none" 
                            style="border-collapse: collapse; background: white;">  
                             
                            <tr> 
                                <td> 
                                    <table id="Table4" cellspacing="1" cellpadding="1" width="250" border="0" class="module">  
                                        <tr> 
                                            <td> 
                                                FirstName:</td> 
                                            <td> 
                                                <asp:TextBox ID="TextBox2" Text='<%# Bind( "FirstName") %>' runat="server" > 
                                                </asp:TextBox></td>  
                                        </tr> 
                                        <tr> 
                                            <td> 
                                                Last Name:</td> 
                                            <td> 
                                                <asp:TextBox ID="TextBox3" Text='<%# Bind( "LastName") %>' runat="server" > 
                                                </asp:TextBox></td>  
                                        </tr> 
                                        <tr> 
                                            <td> 
                                                City:</td> 
                                            <td> 
                                                <asp:TextBox ID="Textbox5" Text='<%# Bind( "City") %>' runat="server">  
                                                </asp:TextBox></td>  
                                        </tr> 
                                        <tr> 
                                            <td> 
                                                Country:</td> 
                                            <td> 
                                                <asp:TextBox ID="TextBox4" Text='<%# Bind( "Country") %>' runat="server" > 
                                                </asp:TextBox></td>  
                                        </tr> 
                                    </table> 
                                </td> 
                                <td> 
                                </td> 
                            </tr> 
                            <tr> 
                                <td align="right" colspan="2">  
                                    <asp:Button ID="btnUpdate" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>' 
                                        runat="server"  CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'>  
                                    </asp:Button>&nbsp;  
                                    <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False" 
                                        CommandName="Cancel"></asp:Button></td>  
                            </tr> 
                        </table> 
                    </FormTemplate> 
                </EditFormSettings> 
 
</MasterTableView> 
        <ClientSettings> 
            <Selecting AllowRowSelect="True" /> 
        </ClientSettings> 
    </telerik:RadGrid> 
    </form> 
</body> 
</html> 
 

.Code Behind
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using Telerik.Web.UI;  
 
 
public partial class _Default : System.Web.UI.Page  
{  
    protected void Page_Load(object sender, EventArgs e)  
    {  
 
    }  
    protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)  
    {  
        if (e.Item is GridEditableItem && e.Item.IsInEditMode && e.Item.OwnerTableView.Name == "Master")  
        {  
            SqlDataSource1.UpdateParameters["Country"].DefaultValue = "USA";  
 
       }  
    }  
      
}  
 

2 Answers, 1 is accepted

Sort by
0
gc_0620
Top achievements
Rank 1
answered on 12 Jan 2010, 01:30 AM
Any solution for my issue? All I want how to change an update parameter in update event if automatic update is set to True in Radgrid.

Thanks
0
Radoslav
Telerik team
answered on 13 Jan 2010, 03:31 PM
Hello Ghulam,

You could set DefaultValue of UpdateParameter not only from the code behind in RadGrid1_UpdateCommand event handler, but also directly in the markup into  <UpdateParameters> on SqlDataSource:
<UpdateParameters>
   <asp:Parameter Name="LastName" Type="String" />
   <asp:Parameter Name="FirstName" Type="String" />
   <asp:Parameter Name="City" Type="String" />
   <asp:Parameter Name="Country" DefaultValue="USA" Type="String" />
   <asp:Parameter Name="EmployeeID" Type="Int32" />
</UpdateParameters>

As for your second intention: I see in  your code you use FormTemplate, so you do not need to change GridBoundColumn "Country" to readonly, you could set the ReadOnly property the TextBox which is shown in edit mode to true:
<asp:TextBox ReadOnly="true" ID="TextBox4" Text='<%# Bind( "Country") %>' runat="server"></asp:TextBox>

Additionally I am sending you a simple example which illustrates how to achieve the desired scenario.
I hope this helps.

Kind regards,
Radoslav
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
gc_0620
Top achievements
Rank 1
Answers by
gc_0620
Top achievements
Rank 1
Radoslav
Telerik team
Share this question
or