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

insert/update usecontrol gridtemplatecolumn

1 Answer 32 Views
Grid
This is a migrated thread and some comments may be shown as answers.
first last
Top achievements
Rank 1
first last asked on 08 Dec 2008, 11:23 PM
Hi,

I have a
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
 
<%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %>
 
<!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>
        <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True"  
            AllowSorting="True" GridLines="None"  
            onneeddatasource="RadGrid1_NeedDataSource" AllowAutomaticDeletes="True"  
            AllowAutomaticInserts="True" AllowAutomaticUpdates="True"  
            AutoGenerateDeleteColumn="True" AutoGenerateEditColumn="True">
<HeaderContextMenu EnableTheming="True">
<CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>
</HeaderContextMenu>
 
<MasterTableView>
<RowIndicatorColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
 
<ExpandCollapseColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
<Columns>
<telerik:GridTemplateColumn HeaderText="someheader">
<ItemTemplate>
some user control
</ItemTemplate>
<EditItemTemplate>
<uc1:WebUserControl ID="WebUserControl1" runat="server" />
</EditItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
 
            <ClientSettings>
                <Scrolling AllowScroll="True" UseStaticHeaders="True" />
            </ClientSettings>
 
<FilterMenu EnableTheming="True">
<CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>
</FilterMenu>
        </telerik:RadGrid>
    </div>
     
    </form>
</body>
</html>


And the usercontrol:



<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<asp:ListBox ID="ListBox1"  SelectionMode="Multiple" runat="server">
    <asp:ListItem>12</asp:ListItem>
    <asp:ListItem>3</asp:ListItem>
    <asp:ListItem>45</asp:ListItem>
    <asp:ListItem>5</asp:ListItem>
    <asp:ListItem>5</asp:ListItem>
    <asp:ListItem>44</asp:ListItem>
    <asp:ListItem>3434</asp:ListItem>
</asp:ListBox>
 

how do i get the values from the listbox in the usercontrol when performing a save or update? and how do i select pre-defined values(the ones saved in an edit to a db) in the listbox when displaying it in itemtemplate?


 

1 Answer, 1 is accepted

Sort by
0
Sebastian
Telerik team
answered on 11 Dec 2008, 02:16 PM
Hi Kjartan,

You can reference the listbox from the user control inside the edit template by its id inside the UpdateCommand/InsertCommand handler of the grid as follows:

ListBox lbox = e.Item.FindControl("WebUserControl1").FindControl("ListBox1") as ListBox;

To set the selected value in the listbox to match the underlying source field value, intercept the ItemDataBound event of the grid and use the following code implementation:

private void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)  
{  
if(e.Item is GridEditFormItem && e.Item.IsInEditMode)  
{  
  ListBox lbox = e.Item.FindControl("WebUserControl1").FindControl("ListBox1"as ListBox;  
   string selValue = DataBinder.Eval(e.Item.DataItem, "<MySourceFieldName>").ToString();  
  //select the item in the listbox which matches the selValue value  
}  
}  

Best regards,
Sebastian
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
first last
Top achievements
Rank 1
Answers by
Sebastian
Telerik team
Share this question
or