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

RadGridTemplateColumn DropDownList saving blank string back to database

2 Answers 10 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tim
Top achievements
Rank 1
Tim asked on 21 Jul 2014, 02:45 PM
Hello all first time poster here, I have a bit of a problem with a RadGrid GridTemplateColumn and a dropdownlist is saving back a blank string. Here is the code I am using. I don't know how much you need so I will post the minimum.
<telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource1" runat="server" OnUpdateCommand="RadGrid1_UpdateCommand"
        ShowStatusBar="true" AutoGenerateColumns="False" AllowSorting="True" AllowMultiRowSelection="False"
        AllowPaging="False" AllowAutomaticDeletes="True" AllowAutomaticInserts="True"
        AllowAutomaticUpdates="True" OnItemUpdated="RadGrid1_ItemUpdated" OnItemDeleted="RadGrid1_ItemDeleted"
        OnItemInserted="RadGrid1_ItemInserted" OnInsertCommand="RadGrid1_InsertCommand" OnItemDataBound="RadGrid1_ItemDataBound"
        OnItemCreated="RadGrid1_ItemCreated" Width="950">
        <MasterTableView DataSourceID="SqlDataSource1" DataKeyNames="ID" AllowMultiColumnSorting="True"
            Width="100%" CommandItemDisplay="Top" Name="Questions">
            <DetailTables>
                <telerik:GridTableView DataKeyNames="ID" DataSourceID="SqlDataSource2" Width="100%"
                    runat="server" CommandItemDisplay="Top" Name="Answers">
                    <ParentTableRelation>
                        <telerik:GridRelationFields DetailKeyField="QuestionID" MasterKeyField="ID"></telerik:GridRelationFields>
                    </ParentTableRelation>
                    <Columns>
                        <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn1">
                            <HeaderStyle Width="20px"></HeaderStyle>
                            <ItemStyle CssClass="MyImageButton"></ItemStyle>
                        </telerik:GridEditCommandColumn>
                        <telerik:GridBoundColumn SortExpression="QuestionID" HeaderText="Question" HeaderButtonType="TextButton"
                            DataField="QuestionID" UniqueName="QuestionID" ReadOnly="true">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn SortExpression="AnswerText" HeaderText="Answer Text" HeaderButtonType="TextButton"
                            DataField="AnswerText" UniqueName="AnswerText">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn SortExpression="PanelAmount" HeaderText="Panel Amount" HeaderButtonType="TextButton"
                            DataField="PanelAmount" UniqueName="PanelAmount">
                        </telerik:GridBoundColumn>
                        <telerik:GridTemplateColumn  SortExpression="IsEnd" HeaderText="Is End"
                            HeaderButtonType="TextButton" DataField="IsEnd" UniqueName="IsEnd">
                            <ItemTemplate>
                                <asp:Label ID="LabelIsEnd" runat="server"><%# DataBinder.Eval(Container.DataItem, "IsEnd")%></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:CheckBox ID="CheckBoxIsEnd" runat="server" Checked='<%#Eval("IsEnd") %>' />
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridBoundColumn SortExpression="JumpToQuestion" HeaderText="Jump To Question"
                            HeaderButtonType="TextButton" DataField="JumpToQuestion" UniqueName="JumpToQuestion">
                        </telerik:GridBoundColumn>
                        <telerik:GridTemplateColumn  SortExpression="StartSurvey" HeaderText="Start Survey"
                            HeaderButtonType="TextButton" DataField="StartSurvey" UniqueName="StartSurvey">
                            <ItemTemplate>
                                <asp:Label ID="LabelStartSurvey" runat="server"><%# DataBinder.Eval(Container.DataItem, "StartSurvey")%></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:CheckBox ID="CheckBoxStartSurvey" runat="server" Checked='<%#Eval("StartSurvey") %>' />
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="DeleteColumnAnswer">
                        </telerik:GridButtonColumn>
                    </Columns>
                </telerik:GridTableView>
            </DetailTables>
            <Columns>
                <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn1">
                    <HeaderStyle Width="20px"></HeaderStyle>
                    <ItemStyle CssClass="MyImageButton"></ItemStyle>
                </telerik:GridEditCommandColumn>
                <telerik:GridBoundColumn SortExpression="ID" HeaderText="ID" HeaderButtonType="TextButton"
                    DataField="ID" UniqueName="ID" ReadOnly="true">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn SortExpression="QuestionText" HeaderText="Question Text"
                    HeaderButtonType="TextButton" DataField="QuestionText" UniqueName="QuestionText">
                </telerik:GridBoundColumn>
                <telerik:GridTemplateColumn  SortExpression="QuestionType" HeaderText="Question Type"
                    HeaderButtonType="TextButton" DataField="QuestionType" UniqueName="QuestionType">
                    <ItemTemplate>
                        <asp:Label ID="LabelQuestionType" runat="server"><%# DataBinder.Eval(Container.DataItem, "QuestionType")%></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownListQUestionType" runat="server"></asp:DropDownList>
                        <%--<asp:DropDownList ID="DropDownListQUestionType" runat="server" SelectedValue='<%#Eval("QuestionType") %>'></asp:DropDownList>--%>
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridBoundColumn SortExpression="QuestionPosition" HeaderText="Question Position"
                    HeaderButtonType="TextButton" DataField="QuestionPosition" UniqueName="QuestionPosition">
                </telerik:GridBoundColumn>
                <telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="DeleteColumnQuestion">
                </telerik:GridButtonColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Local_TypingTool_NewConnectionString %>"
        UpdateCommand="UPDATE [Screener_Questions] SET [QuestionText] = @QuestionText, [QuestionType] = @QuestionType, [QuestionPosition] = @QuestionPosition WHERE [ID] = @ID"
        SelectCommand="SELECT [ID], [QuestionText], [QuestionType], [QuestionPosition] FROM Screener_Questions WHERE [Survey_ID] = @Survey_ID"
        InsertCommand="INSERT INTO [Screener_Questions] ([Survey_ID], [QuestionText], [QuestionType], [QuestionPosition]) VALUES (@Survey_ID, @QuestionText, @QuestionType, @QuestionPosition)"
        DeleteCommand="DELETE FROM [Screener_Questions] WHERE [ID] = @ID">
        <DeleteParameters>
            <asp:Parameter Name="ID" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="Survey_ID" Type="Int32" />
            <asp:Parameter Name="QuestionText" Type="String" />
            <asp:Parameter Name="QuestionType" Type="Int32" />
            <asp:Parameter Name="QuestionPosition" Type="Int32" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="ID" Type="Int32" />
            <asp:Parameter Name="QuestionText" Type="String" />
            <asp:Parameter Name="QuestionType" Type="Int32" />
            <asp:Parameter Name="QuestionPosition" Type="Int32" />
        </UpdateParameters>
        <SelectParameters>
            <asp:Parameter Name="Survey_ID" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>

protected void RadGrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            DropDownList list = editedItem.FindControl("DropDownListQUestionType") as DropDownList;
            Session["updatedValueQUestionType"] = list.SelectedValue;
        }

When I run the code I see the dropdownlist and it has data and it shows the correct drop down value when displayed.
On the RadGrid1_UpdateCommand it does see the value to hold it in the session but for some reason it actauly saves it back as a blank string.

2 Answers, 1 is accepted

Sort by
0
Ed
Top achievements
Rank 1
answered on 22 Jul 2014, 03:46 AM
What I'm seeing, if I'm looking at the right control is, you have two dropdownlists with the same name there, the first of which isn't bound to anything.  The second, which is the one I think you mean to use, is using "Eval" to bind up the SelectedValue property.  But that is one-way binding, you need to use "Bind" instead.
 
<EditItemTemplate>
 
     <asp:DropDownList ID="DropDownListQUestionType" runat="server"></asp:DropDownList>
 
     <%--<asp:DropDownList ID="DropDownListQUestionType" runat="server" SelectedValue='<%#Eval("QuestionType") %>'></asp:DropDownList>--%>
 
 </EditItemTemplate>
0
Princy
Top achievements
Rank 1
answered on 31 Jul 2014, 10:56 AM
Hi Tim,

Since you are using Automatic Crud operation its better you bind the DropDownList in ASPX page as below:

ASPX:
<asp:DropDownList ID="DropDownListQUestionType" runat="server" DataSourceID="SqlDataSource1" DataTextField="QuestionType" DataValueField="QuestionType" SelectedValue='<%# Bind("QuestionType") %>' >
</asp:DropDownList>

OR
Another solution is to attach the SelectedValue to the UpdateParameter in the ItemCommand event as follows:

ASPX:
<asp:DropDownList ID="DropDownListQUestionType" runat="server">
</asp:DropDownList>

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
     GridEditableItem editItem = (GridEditableItem)e.Item;
     DropDownList DropDownListQUestionType = (DropDownList)editItem.FindControl("DropDownListQUestionType");
     DropDownListQUestionType.DataTextField = "QuestionType";
     DropDownListQUestionType.DataValueField = "QuestionType";
     DropDownListQUestionType.DataSourceID = "SqlDataSource1";
     DropDownListQUestionType.SelectedValue = DataBinder.Eval(editItem.DataItem, "QuestionType").ToString();
    }
}
void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.UpdateCommandName)
    {
     GridEditableItem editedItem = e.Item as GridEditableItem;
     DropDownList list = editedItem.FindControl("DropDownListQUestionType") as DropDownList;
     SqlDataSource1.UpdateParameters["QuestionType"].DefaultValue = list.SelectedValue;
    }
}

Thanks,
Princy
Tags
Grid
Asked by
Tim
Top achievements
Rank 1
Answers by
Ed
Top achievements
Rank 1
Princy
Top achievements
Rank 1
Share this question
or