I am new to rad grid. I have tried the following sample code for <EditItemTemplate>. But when I clicked the edit button, nothing was in the grid. When I saw the demo, everything was done using SQLDataSource. So I have confusion on what I have missed. Can anyone suggest me what I have missed in the following code.
<telerik:RadGrid ID="RadGrid1" runat="server" Skin="Black" OnItemDeleted="RadGrid1_ItemDeleted" OnItemUpdated="RadGrid1_ItemUpdated">
<MasterTableView AutoGenerateColumns="False" DataKeyNames="ProductID" CommandItemDisplay="Top"
EditMode="InPlace">
<Columns>
<telerik:GridTemplateColumn DataField="ProductID" HeaderText="Product ID" UniqueName="ProductID">
<ItemTemplate>
<asp:Label runat="server" ID="lblProductID" Text='<%# Eval("ProductID") %>'></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn DataField="ProductName" HeaderText="Product Name" UniqueName="ProductName">
<ItemTemplate>
<asp:Label runat="server" ID="lblProductName" Text='<%# Eval("ProductName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtProductName" Text='<%# Bind("ProductName") %>'></asp:TextBox>
<span style="color: Red">*</span>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtProductName"
ErrorMessage="Required" runat="server">
</asp:RequiredFieldValidator>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="UnitPrice" SortExpression="UnitPrice" UniqueName="TemplateColumn"
EditFormColumnIndex="1">
<ItemTemplate>
<asp:Label runat="server" ID="lblUnitPrice" Text='<%# Eval("UnitPrice", "{0:C}") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtUnitPrice" Text='<%# Bind("UnitPrice") %>'></asp:TextBox>
<span style="color: Red">*</span>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtUnitPrice"
ErrorMessage="Required" runat="server">
</asp:RequiredFieldValidator>
</EditItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridEditCommandColumn ButtonType="ImageButton">
<ItemStyle CssClass="MyImageButton" />
</telerik:GridEditCommandColumn>
<telerik:GridButtonColumn ConfirmText="Delete this product?" ConfirmDialogType="RadWindow"
ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" Text="Delete"
UniqueName="DeleteColumn">
<ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" />
</telerik:GridButtonColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindProducts();
}
}
private void BindProducts()
{
try
{
conn = new SqlConnection("Data Source=server1;Initial Catalog=Northwind;User ID=sa;Password=sa");
conn.Open();
SqlCommand cmd = new SqlCommand("select * from Products", conn);
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ad.Fill(ds);
RadGrid1.DataSource = ds;
RadGrid1.DataBind();
}
finally
{
if (conn != null)
{
conn.Close();
}
}
}
protected void RadGrid1_ItemDeleted(object source, GridDeletedEventArgs e)
{
GridDataItem dataItem = (GridDataItem)e.Item;
String id = dataItem.GetDataKeyValue("ProductID").ToString();
....
// code to handle deletion process
....
}
protected void RadGrid1_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e)
{
GridEditableItem item = (GridEditableItem)e.Item;
String id = item.GetDataKeyValue("ProductID").ToString();
....
// code to handle Updation process
....
}
Thanks in advance,
Katte