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

How to bind a DropDownList in a Template form

4 Answers 108 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bricton Perez
Top achievements
Rank 1
Bricton Perez asked on 25 May 2010, 04:43 AM
This is my ascx code

<td  colspan="2" style="height:auto">
                    <telerik:RadGrid ID="gv_data_contract" runat="server" AutoGenerateColumns="False" GridLines="None" OnNeedDataSource="gv_data_contract_NeedDataSource" Width="1034px" AllowPaging="True" Skin="Web20" OnItemCommand="gv_data_contract_ItemCommand" AllowAutomaticInserts="true" AllowAutomaticUpdates="true" OnItemDataBound="gv_data_Contract_ItemDataBound">
                        <MasterTableView CommandItemDisplay="Top" DataKeyNames="ContractID" NoMasterRecordsText="No hay Contratos para mostrar." EditMode="PopUp">
                            <Columns>
                                <telerik:GridEditCommandColumn ButtonType="ImageButton" CancelText="Cancelar" EditText="Editar"
                                    InsertText="Adicionar" UpdateText="Actualizar">
                                    <HeaderStyle Font-Names="Arial" Font-Size="12px" Width="15px" />
                                    <ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
                                        Font-Underline="False" HorizontalAlign="Center" Wrap="True" />
                                </telerik:GridEditCommandColumn>
                                <telerik:GridBoundColumn DataField="ContractCode" UniqueName="ContractCode" HeaderText="C&#243;digo">
                                    <HeaderStyle Font-Bold="True" Font-Names="Verdana" Font-Size="13px" HorizontalAlign="Center" />
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="ReferenceID" HeaderText="Referencia" UniqueName="ReferenceID">
                                    <HeaderStyle Font-Bold="True" Font-Names="Verdana" Font-Size="13px" HorizontalAlign="Center" />
                                </telerik:GridBoundColumn>
                                <telerik:GridTemplateColumn DataField="PersonConpanyName" HeaderText="Contratante" UniqueName="PersonConpanyName">
                                    <ItemTemplate>
                                    
                                        <asp:Label ID="lbl_gv_companyName" runat="server" ></asp:Label>
                                           <asp:Label id="lbl_gv_data_PersonCompanyName" runat="server">
                                              <%# DataBinder.Eval(Container.DataItem, "Person.PersonCompanyName") %>
                                           </asp:Label>
                                    </ItemTemplate>
                                    <HeaderStyle Font-Bold="True" Font-Names="Verdana" Font-Size="13px" HorizontalAlign="Center" />
                                </telerik:GridTemplateColumn>
                                <telerik:GridBoundColumn DataField="StartingDate" DataFormatString="{0:d}" DataType="System.DateTime"
                                    HeaderText="Fecha Inicia" UniqueName="StartingDate">
                                    <HeaderStyle Font-Bold="True" Font-Names="Verdana" Font-Size="13px" HorizontalAlign="Center" />
                                    <ItemStyle HorizontalAlign="Right" />
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="EndingDate" DataFormatString="{0:d}" DataType="System.DateTime"
                                    HeaderText="Fecha Termina" UniqueName="EndingDate">
                                    <HeaderStyle Font-Bold="True" Font-Names="Verdana" Font-Size="13px" HorizontalAlign="Center" />
                                    <ItemStyle HorizontalAlign="Right" />
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn DataField="TotalAmount" DataFormatString="{0:c2}" DataType="System.Decimal"
                                    HeaderText="Valor Total" UniqueName="TotalAmount">
                                    <HeaderStyle Font-Bold="True" Font-Names="Verdana" Font-Size="13px" HorizontalAlign="Center" />
                                    <ItemStyle HorizontalAlign="Right" />
                                </telerik:GridBoundColumn>
                                <telerik:GridCheckBoxColumn DataField="ContractStatus" DataType="System.Boolean"
                                    HeaderText="Estado" UniqueName="ContractStatus">
                                    <HeaderStyle Font-Bold="True" Font-Names="Verdana" Font-Size="13px" HorizontalAlign="Center" />
                                    <ItemStyle HorizontalAlign="Center" />
                                </telerik:GridCheckBoxColumn>
                            </Columns>
                            <EditFormSettings EditFormType="Template">
                                <EditColumn UniqueName="EditCommandColumn1">
                                </EditColumn>
                                <FormTemplate>
                                    <table style="padding-left: 8px; font-family: Arial; font-size: 12pt; width: 98%;">
                                        <tr>
                                            <td style="width: 187px">
                                            </td>
                                            <td style="width: 275px">
                                            </td>
                                            <td>
                                            </td>
                                            <td>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="width: 187px">
                                                Seleccione Cliente</td>
                                            <td style="width: 275px">
                                                <asp:DropDownList ID="ddl_gv_person" runat="server" DataTextField="Person.PersonCompanyName" DataValueField="PersonID" >
                                                </asp:DropDownList>
                                            </td>
                                            <td>
                                            </td>
                                            <td>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="width: 187px">
                                                Referencia Contrato</td>
                                            <td style="width: 275px">
                                                <telerik:RadTextBox ID="txb_rd_ReferenceID" runat="server" EmptyMessage="--- Introduzca Referencia ---"
                                                    Skin="WebBlue" Width="177px" Text='<%# Bind("ReferenceID") %>'>
                                                    <EnabledStyle HorizontalAlign="Right" />
                                                    <DisabledStyle HorizontalAlign="Right" />
                                                    <EmptyMessageStyle ForeColor="Blue" HorizontalAlign="Center" />
                                                </telerik:RadTextBox></td>
                                            <td>
                                            </td>
                                            <td>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="width: 187px">
                                                Fecha Inicia</td>
                                            <td style="width: 275px">
                                                <telerik:RadDatePicker ID="txb_rdp_StartingDate" runat="server" Skin="WebBlue" Width="175px" Culture="English (United States)" DbSelectedDate='<%# Bind("StartingDate") %>'>
                                                    <Calendar runat="server" Skin="WebBlue" UseColumnHeadersAsSelectors="False" UseRowHeadersAsSelectors="False"
                                                        ViewSelectorText="x">
                                                        <FastNavigationSettings CancelButtonCaption="Cancelar" DateIsOutOfRangeMessage="Fecha fuera de rango.">
                                                        </FastNavigationSettings>
                                                        <DisabledDayStyle HorizontalAlign="Right" />
                                                        <SelectedDayStyle HorizontalAlign="Right" />
                                                    </Calendar>
                                                    <DatePopupButton HoverImageUrl="" ImageUrl="" ToolTip="Mostrar Calendario." />
                                                    <DateInput runat="server" DateFormat="M/d/yyyy" DisplayDateFormat="M/d/yyyy" EmptyMessage="--- Seleccione Fecha Inicial ---">
                                                        <EmptyMessageStyle ForeColor="Blue" HorizontalAlign="Center" />
                                                        <EnabledStyle HorizontalAlign="Right" />
                                                    </DateInput>
                                                </telerik:RadDatePicker>
                                            </td>
                                            <td>
                                            </td>
                                            <td>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="width: 187px">
                                                Fecha Termina</td>
                                            <td style="width: 275px">
                                                <telerik:RadDatePicker ID="txb_rdp_EndingDate" runat="server" Skin="WebBlue" Width="175px" Culture="English (United States)" DbSelectedDate='<%# Bind("EndingDate") %>'>
                                                    <Calendar runat="server" Skin="WebBlue" UseColumnHeadersAsSelectors="False" UseRowHeadersAsSelectors="False"
                                                        ViewSelectorText="x">
                                                        <FastNavigationSettings CancelButtonCaption="Cancelar" DateIsOutOfRangeMessage="Fecha fuera de rango.">
                                                        </FastNavigationSettings>
                                                        <DisabledDayStyle HorizontalAlign="Right" />
                                                        <SelectedDayStyle HorizontalAlign="Right" />
                                                    </Calendar>
                                                    <DatePopupButton HoverImageUrl="" ImageUrl="" ToolTip="Mostrar Calendario." />
                                                    <DateInput runat="server" DateFormat="M/d/yyyy" DisplayDateFormat="M/d/yyyy" EmptyMessage="--- Seleccione Fecha Final ---">
                                                        <EmptyMessageStyle ForeColor="Blue" HorizontalAlign="Center" />
                                                        <EnabledStyle HorizontalAlign="Right" />
                                                    </DateInput>
                                                </telerik:RadDatePicker>
                                            </td>
                                            <td>
                                            </td>
                                            <td>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="width: 187px">
                                                Valor Total</td>
                                            <td style="width: 275px">
                                                <telerik:RadNumericTextBox ID="txb_rn_TotalAmount" runat="server" Culture="English (United States)"
                                                    DataType="System.Decimal" Skin="WebBlue" Type="Currency" Value="0" Width="186px" DbValue='<%# Bind("TotalAmount") %>'>
                                                    <DisabledStyle HorizontalAlign="Right" />
                                                    <IncrementSettings InterceptArrowKeys="False" InterceptMouseWheel="False" />
                                                    <EmptyMessageStyle ForeColor="Blue" HorizontalAlign="Center" />
                                                    <EnabledStyle HorizontalAlign="Right" />
                                                </telerik:RadNumericTextBox></td>
                                            <td>
                                            </td>
                                            <td>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="width: 187px">
                                                Estado Contrato</td>
                                            <td style="width: 275px">
                                                </td>
                                            <td>
                                            </td>
                                            <td>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="width: 187px">
                                            </td>
                                            <td style="width: 275px">
                                            </td>
                                            <td>
                                            </td>
                                            <td>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="width: 187px; height: 26px">
                                            </td>
                                            <td align="right" style="width: 275px; height: 26px">
                                                <asp:Button ID="btnUpdate" Text='<%# (Container as GridItem).OwnerTableView.IsItemInserted ? "Adicionar" : "Actualizar" %>'
                                                    runat="server" CommandName='<%# (Container as GridItem).OwnerTableView.IsItemInserted ? "PerformInsert" : "Update" %>'>
                                                </asp:Button>&nbsp;
                                                <asp:Button ID="btnCancel" Text="Cancelar" runat="server" CausesValidation="False"
                                                CommandName="Cancel"></asp:Button>
                                            </td>
                                            <td style="height: 26px">
                                            </td>
                                            <td style="height: 26px">
                                            </td>
                                        </tr>
                                    </table>
                                </FormTemplate>


My problem is that I don't know how to bind a DDL in code behind in order to Insert or Edit

I have this codebehind:

protected void gv_data_Contract_ItemDataBound(object sender, GridItemEventArgs e)
    {

        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem item = e.Item as GridEditableItem;
            // access/modify the edit item template settings here
            DropDownList list = item.FindControl("lbl_gv_data_PersonCompanyName") as DropDownList;
            list.DataSource = GetCompany();
            list.DataBind();

            if (Session["Updated"] != null)
            {
                list.SelectedValue = Session["updatedValue"].ToString();
            }
        }
        else if (e.Item is GridDataItem && !e.Item.IsInEditMode && Page.IsPostBack)
        {
            GridDataItem item = e.Item as GridDataItem;
            Label label = item.FindControl("ddl_gv_person") as Label;
            // update the label value
            if (label != null) label.Text = Session["updatedValue"];
        }

    }

And I get this error

System.NullReferenceException was unhandled by user code
  Message="Object reference not set to an instance of an object."
 



4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 25 May 2010, 12:34 PM
Hello Bricton,

From your code i can see that you tried to access the DropDownList by using the ID of Label and Label by using the ID of DropDownList. I think that would be the reason for that error. Make sure that you are using correct ID for accessing control and see whether it solves this issue.

Feel free to ask if it doesn't work,
Princy.
0
Bricton Perez
Top achievements
Rank 1
answered on 25 May 2010, 03:31 PM
I did change the code, this is the new one:

 protected void gv_data_Contract_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem item = e.Item as GridEditableItem;
            // access/modify the edit item template settings here
            DropDownList list = item.FindControl("ddl_gv_person") as DropDownList;
            list.DataSource = GetCompany();
            list.DataBind();

            if (Session["updatedValue"] != null)
            {
                list.SelectedValue = Session["updatedValue"];  ---> this line has an error:"Cannot Convert source type "object" to target type "string"
            }
        }
        else if (e.Item is GridDataItem && !e.Item.IsInEditMode && Page.IsPostBack)
        {
            GridDataItem item = e.Item as GridDataItem;
            Label label = item.FindControl("lbl_gv_data_PersonCompanyName") as Label;
             //update the label value
            label.Text = Session["updatedValue"]; ---> this line has an error:"Cannot Convert source type "object" to target type "string"
        }
    }

The error still remain,
0
Princy
Top achievements
Rank 2
answered on 26 May 2010, 07:26 AM
Hello Bricton,

Session["updatedValue"] always returns an object. You need an explicit convertion by using ToString() method if you want to use the value as a string.

         label.Text = Session["updatedValue"].ToString();
                                       or
         label.Text = (string)Session["updatedValue"];

Regards,
Princy.
0
Bricton Perez
Top achievements
Rank 1
answered on 27 May 2010, 04:31 AM
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" GridLines="None" 
                        Width="1035px"
                        <MasterTableView CommandItemDisplay="Top" DataKeyNames="ContractID"
                            <Columns> 
                                <telerik:GridEditCommandColumn ButtonType="ImageButton"
                                </telerik:GridEditCommandColumn> 
                                <telerik:GridBoundColumn DataField="ContractCode" HeaderText="Contract Code" UniqueName="ContractCode"
                                </telerik:GridBoundColumn> 
                                <telerik:GridBoundColumn DataField="ReferenceID" HeaderText="Reference ID" UniqueName="ReferenceID"
                                </telerik:GridBoundColumn> 
                                <telerik:GridBoundColumn DataField="StartingDate" DataFormatString="{0:d}" HeaderText="StartingDate" 
                                    UniqueName="StartingDate"
                                </telerik:GridBoundColumn> 
                                <telerik:GridTemplateColumn DataField="PersonCompanyName" HeaderText="Company Name" UniqueName="PersonCompanyName"
                                    <ItemTemplate> 
                                         <asp:Label id="lbl_personcompanyname" runat="server"><%# DataBinder.Eval(Container.DataItem, "Person.PersonCompanyName") %> 
                                         </asp:Label> 
                                     </ItemTemplate> 
                                </telerik:GridTemplateColumn> 
                            </Columns> 
                            <EditFormSettings EditFormType="Template"
                                <EditColumn UniqueName="EditCommandColumn1"
                                </EditColumn> 
                                <FormTemplate> 
                                    <table width="100%" > 
                                        <tr> 
                                            <td style="width: 159px"
                                            </td> 
                                            <td> 
                                            </td> 
                                            <td> 
                                            </td> 
                                        </tr> 
                                        <tr> 
                                            <td style="width: 159px"
                                                Select a Client</td> 
                                            <td> 
                                                <asp:DropDownList ID="ddl_personcompanyname" runat="server" Width="183px"
                                                </asp:DropDownList></td
                                            <td> 
                                            </td> 
                                        </tr> 
                                        <tr> 
                                            <td style="width: 159px; height: 20px"
                                            </td> 
                                            <td style="height: 20px"
                                            </td> 
                                            <td style="height: 20px"
                                            </td> 
                                        </tr> 
                                        <tr> 
                                            <td style="width: 159px"
                                            </td> 
                                            <td> 
                                            </td> 
                                            <td> 
                                            </td> 
                                        </tr> 
                                        <tr> 
                                            <td style="width: 159px"
                                            </td> 
                                            <td> 
                                            </td> 
                                            <td> 
                                            </td> 
                                        </tr> 
                                    </table> 
                                </FormTemplate> 
                            </EditFormSettings> 
                        </MasterTableView> 
                        <ClientSettings> 
                            <Selecting AllowRowSelect="True" /> 
                        </ClientSettings> 
                    </telerik:RadGrid> 
This is the event from a MasterList.cs class
public static void GetCompany(DropDownList ddl, bool enabled, bool itemSelection, bool itemAll) 
    { 
        if (ddl != null) 
        { 
            ddl.DataTextField = "PersonCompanyName"
            ddl.DataValueField = "PersonID"
 
            ddl.DataSource = AWFacade.Person.GetAll(enabled); 
            ddl.DataBind(); 
 
            if (itemSelection && !itemAll) 
            { 
                ddl.Items.Insert(0, new ListItem("[Seleccione]", "0")); 
            } 
        } 
    } 
As I said before I've been looking for the solution for how can I bind the "GetCompany" into my DropDownList in my Template form, is there a solution?

I appreciate your help

Regards

Tags
Grid
Asked by
Bricton Perez
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Bricton Perez
Top achievements
Rank 1
Share this question
or