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ó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>
<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."
<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ó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>
<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."