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

RadGrid, EditItemTemplate sending null values for CheckBox + more

1 Answer 240 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Craig
Top achievements
Rank 1
Craig asked on 10 Sep 2012, 01:56 PM
<telerik:RadGrid ID="RadGrid3" runat="server" Width="100%"
    AllowAutomaticUpdates="True"
    AllowMultiRowEdit="true"
    AllowPaging="false"
    DataSourceID="SqlDataSource3"
    EnableLinqExpressions="false"
    GridLines="None"
                         
    OnItemDataBound="RadGrid3_ItemDataBound"
    OnPreRender="RadGrid3_PreRender"
 
    PageSize="10">
    <MasterTableView Width="100%" CommandItemDisplay="Bottom" EditMode="InPlace" DataSourceID="SqlDataSource3" DataKeyNames="ProductAttributeDefKey,Value" AutoGenerateColumns="false">
        <CommandItemTemplate>
            <asp:LinkButton ID="btnUpdateEdited" runat="server" CommandName="UpdateEdited" Visible='<%# RadGrid3.EditIndexes.Count > 0 %>'>Update Products</asp:LinkButton>
              
            <asp:LinkButton ID="btnCancel" runat="server" CommandName="CancelAll" CausesValidation="false" Visible='<%# RadGrid3.EditIndexes.Count > 0 || RadGrid3.MasterTableView.IsItemInserted %>'>Cancel editing</asp:LinkButton>
        </CommandItemTemplate>
 
        <Columns>
            <telerik:GridTemplateColumn DefaultInsertValue="1" DataField="SortOrder" HeaderText="Sort Order" SortExpression="SortOrder" UniqueName="SortOrder">
                <EditItemTemplate>
                    <telerik:RadNumericTextBox runat="server" ID="tbSortOrder" DbValue='<%# Bind("SortOrder") %>' ShowSpinButtons="true" MinValue="1" MaxValue="999" NumberFormat-DecimalDigits="0" Width="50px">
                    </telerik:RadNumericTextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <%# Eval("SortOrder")%>
                </ItemTemplate>
                <ItemStyle CssClass="sortColumn" />
                <HeaderStyle CssClass="sortColumn" />
            </telerik:GridTemplateColumn>
            <telerik:GridBoundColumn DataField="ProductId" HeaderText="ProductId" SortExpression="ProductId" UniqueName="ProductId" Visible="false">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="ProductAttributeDefKey" HeaderText="Def Key" SortExpression="ProductAttributeDefKey" UniqueName="ProductAttributeDefKey" Visible="false">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Name" HeaderText="Name" SortExpression="Name" UniqueName="Name" ReadOnly="true">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="AttributeDataType" HeaderText="AttributeDataType" SortExpression="AttributeDataType" UniqueName="AttributeDataType" Visible="false">
            </telerik:GridBoundColumn>
 
            <telerik:GridTemplateColumn DataField="Value" HeaderText="Value" SortExpression="Value" UniqueName="Value" ForceExtractValue="Always">
                <EditItemTemplate>
 
                </EditItemTemplate>
                <ItemTemplate>
                    <%# Eval("Value")%>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
        <EditFormSettings>
            <EditColumn ButtonType="ImageButton" />
        </EditFormSettings>
    </MasterTableView>
</telerik:RadGrid>
 
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
    ConnectionString=""
    SelectCommand="procProductAttribute_Select" SelectCommandType="StoredProcedure"
    UpdateCommand="procProductAttribute_Update" UpdateCommandType="StoredProcedure">
    <SelectParameters>
        <asp:Parameter Name="ProductId" Type="Int32" DefaultValue="52964" />
        <asp:Parameter Name="IsCustom" Type="Boolean" DefaultValue="false" />
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Name="ProductId" Type="Int32" />
        <asp:Parameter Name="ProductAttributeDefKey" Type="String" />
        <asp:Parameter Name="AttributeDataType" Type="Int32" />
        <asp:Parameter Name="Value" Type="String" />
    </UpdateParameters>
</asp:SqlDataSource>


protected void RadGrid3_PreRender(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        foreach (GridItem item in RadGrid3.MasterTableView.Items)
        {
            if (item is GridEditableItem)
            {
                GridEditableItem editableItem = item as GridDataItem;
                editableItem.Edit = true;
            }
        }
        RadGrid3.Rebind();
    }
}
 
 
protected void RadGrid3_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        GridEditableItem item = (GridEditableItem)e.Item;
        GridTemplateColumnEditor editor = (GridTemplateColumnEditor)item.EditManager.GetColumnEditor("Value");
        TableCell cell = (TableCell)editor.ContainerControl;
 
        Int32 id = (Int32)DataBinder.Eval(e.Item.DataItem, "AttributeDataType");
        String value = null;
        if(DataBinder.Eval(e.Item.DataItem, "Value") != System.DBNull.Value) {
            value = (String)DataBinder.Eval(e.Item.DataItem, "Value");
        }
 
        switch (id)
        {
            case 1:
                CheckBox checkBox = new CheckBox();
                checkBox.Attributes.Add("Value", "1");
                checkBox.Text = "1";
 
                if (!String.IsNullOrWhiteSpace(value))
                {
                    if (value.Substring(0, 1) == "1" || value.Substring(0, 1).ToLower() == "t" || value.Substring(0, 1).ToLower() == "y")
                    {
                        checkBox.Checked = true;
                    }
                    else
                    {
                        checkBox.Checked = false;
                    }
                }
 
                cell.Controls.Add(checkBox);
                break;
            case 2:
            case 6:
            case 7:
                RadNumericTextBox numericTextBox = new RadNumericTextBox();
                numericTextBox.ID = "NumericTextBox1";
                numericTextBox.ShowSpinButtons = true;
                Int32 tempInt;
 
                if (Int32.TryParse(value, out tempInt))
                {
                    numericTextBox.Value = tempInt;
                }
 
                cell.Controls.Add(numericTextBox);
                break;
            case 3:
            default:
                TextBox textBox = new TextBox();
                textBox.ID = "TextBox1";
 
                if (!String.IsNullOrWhiteSpace(Server.HtmlDecode(value)))
                {
                    textBox.Text = value;
                }
                cell.Controls.Add(textBox);
                break;
            case 4:
                RadTextBox textArea = new RadTextBox();
                textArea.ID = "TextArea1";
                textArea.TextMode = InputMode.MultiLine;
 
                if (!String.IsNullOrWhiteSpace(Server.HtmlDecode(value)))
                {
                    textArea.Text = value;
                }
                cell.Controls.Add(textArea);
                break;
            case 5:
                RadDatePicker dp = new RadDatePicker();
                dp.ID = "DatePicker1";
                DateTime tempDateTime;
 
                if (DateTime.TryParse(value, out tempDateTime))
                {
                    dp.SelectedDate = tempDateTime;
                }
 
                cell.Controls.Add(dp);
                break;
            case 8:
                //lookup here
                break;
        }
    }
}

Having some trouble updating my grid. Ive added the controls in programatically as they differ per row, the text box, rad text box and numeric text boxes update no problem, but the date picker sends through an empty string and checkbox is always false. I will also be adding dropdown box which will no doubt have the same problems.

edit: checkbox always has original value, so if its true it will always get set to true on update no matter the state of the checkbox, same is happening for textbox and any other control

Any idea whats causing this or what im missing?

Thanks
Craig

1 Answer, 1 is accepted

Sort by
0
Kostadin
Telerik team
answered on 13 Sep 2012, 02:00 PM
Hi Craig,

Adding the controls programmatically cannot be done in the way you are trying. A possible solution is to use Form Template Edit Form. This way you can create all the needed controls in the FormTemplate and hide the ones you do not want to show.
For example: you could create edit form template which contains RadDatePicker and CheckBox. On the first row you could hide the RadDatePicker and leave the CheckBox. On the second row you could hide the CheckBox and leave the RadDatePicker.
You could check out this demo which demonstrates how to implement FormTemplate.
Note that you will need to retrieve the values from controls in form template with the FindControl(controlId) method for such type of edit form. You could find a sample code and more information in this help article.

All the best,
Kostadin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Craig
Top achievements
Rank 1
Answers by
Kostadin
Telerik team
Share this question
or