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

Problemn using Grid FormTemplate

1 Answer 53 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dan
Top achievements
Rank 2
Dan asked on 07 Nov 2008, 01:32 PM
I have a FormTemplate where the user enters a non-bound text message.  In the UpdateCommand code-behind, I want to capture the new message (txtBody) and combine it with the text of a GridBoundColumn value (body) from the owning grid.  In the UpdateCommand code-behind I cannot seem to access either value.  Can you point me in the right direction?  Thanks.

My aspx:

<radg:radgrid Skin="Monochrome" ID="rgData" runat="server"
            AllowPaging="True" AllowSorting="True"
            AutoGenerateColumns="False" BorderStyle="None" EnableAJAX="True" EnableAJAXLoadingTemplate="True"
            GridLines="None" Width="99%" AllowAutomaticDeletes="True" AllowAutomaticInserts="True"
            AllowAutomaticUpdates="False" OnUpdateCommand="GridUpdateCommand" OnItemCommand="rgData_ItemCommand">
            <MasterTableView CommandItemDisplay="Bottom" PageSize="10">
                <PagerStyle Mode="NextPrevAndNumeric" />
                <EditFormSettings EditFormType="Template">
                    <FormStyle CssClass="radEnabledCss_WebBlue" />
                    <FormTemplate>
                        <table style="width: 80%" align="center">
                            <tr>
                                <td style="width: 15%">Category:</td>
                                <td style="width: 85%">
                                    <div class='field'>
                                        <radC:RadComboBox ID="cmbCategory" runat="server" Skin="WebBlue" Width="180px"
                                         DataTextField="poptext" DataValueField="MSG_CATEGORY" SelectedValue='<%# Bind("MSG_CATEGORY") %>'>
                                         <Items>
                                                <radC:RadComboBoxItem Text="Select" Value="" />
                                                <radC:RadComboBoxItem Text="Question" Value="Question" />
                                                <radC:RadComboBoxItem Text="Request" Value="Request" />
                                                <radC:RadComboBoxItem Text="Comment" Value="Comment" />
                                                <radC:RadComboBoxItem Text="Other" Value="Other" />
                                            </Items>
                                        </radC:RadComboBox>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td>Subject:</td>
                                <td>
                                    <asp:TextBox ID="txtSubject" runat="server" Width="375" Text='<%# Bind("SUBJECT") %>' />
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2">
                                    <asp:Label ID="lblMessage" runat="server" Text='<%# (Container is GridEditFormInsertItem) ? "Message:" : "Reply:" %>' />
                                </td>
                            </tr>
                            <tr>
                            <td colspan="2">
                                <radI:RadTextBox ID="txtBody" runat="server"  TextMode="MultiLine" Width="500" Height="100" />
                            </td>
                            </tr>
                            <tr>
                                <td colspan="2" align="center">
                                    <asp:Button ID="btnInsert" Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                                        runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>' />
                                    <asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
                                </td>
                            </tr>
                        </table>
                    </FormTemplate>
                </EditFormSettings>
                <ExpandCollapseColumn Visible="False" Resizable="False">
                    <HeaderStyle Width="20px" />
                </ExpandCollapseColumn>
                <RowIndicatorColumn Visible="False">
                    <HeaderStyle Width="20px" />
                </RowIndicatorColumn>
                <Columns>
                    <radG:GridBoundColumn HeaderText="ID" UniqueName="pk" DataField="CPK_WEB_MESSAGES"
                        Display="False" ReadOnly="True" />
                    <radG:GridBoundColumn UniqueName="subject" DataField="subject" Visible="False" ReadOnly="False" />
                    <radG:GridBoundColumn UniqueName="body" DataField="body" Visible="False" ReadOnly="False" />
                    <radG:GridBoundColumn HeaderText="Message Source" UniqueName="SOURCE" DataField="web_message_source" HeaderButtonType="TextButton" />
                    <radG:GridBoundColumn HeaderText="Category" UniqueName="msg_category" DataField="MSG_CATEGORY" HeaderButtonType="TextButton" />
                    <radG:GridBoundColumn HeaderText="Date Sent" UniqueName="RECEIVED" DataField="received" HeaderButtonType="TextButton" />
                    <radG:GridBoundColumn HeaderText="Subject / Message" UniqueName="subjectbody" DataField="subjectbody" HeaderButtonType="TextButton" />
                    <radG:GridBoundColumn HeaderText="Status" UniqueName="msg_status" DataField="msg_status" HeaderButtonType="TextButton" />
                    <radG:GridButtonColumn ButtonType="PushButton" Text="Reply" CommandName="Edit" UniqueName="edit">
                    </radG:GridButtonColumn>
                    <radG:GridButtonColumn ButtonType="PushButton" Text="Delete" CommandName="Delete" UniqueName="delete">
                    </radG:GridButtonColumn>
                </Columns>
                <CommandItemTemplate>
                    <asp:Button ID="btnInsert" CommandName="InitInsert" ImageUrl="~/Images/add_small_off.png"
                        runat="server" Text="Add Message" />
                    <asp:Button ID="btnRefresh" CommandName="RebindGrid" ImageUrl="~/Images/refresh_list_off.png"
                        runat="server" Text="Refresh List" />
                </CommandItemTemplate>
            </MasterTableView>
            <HeaderStyle Font-Bold="True" Font-Size="14px" Font-Underline="False" HorizontalAlign="Left" />
            <ItemStyle Font-Size="12px" />
            <AlternatingItemStyle Font-Size="12px" />
        </radg:radgrid>

My code-behind:

protected void rgData_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)                   <---This does not work
        {
            TextBox txtBox = e.Item.FindControl("txtBody") as TextBox;
            Session["savedOldBody"] = txtBox.Text;
        }
    }

    protected void GridUpdateCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
    {
        GridEditableItem editedItem = (GridEditableItem)e.Item;
        int iPKey = (int)editedItem.GetDataKeyValue("CPK_WEB_MESSAGES");

        Hashtable newValues = new Hashtable();
        e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
        string subject = newValues["SUBJECT"].ToString().Trim();
        string body = newValues["txtBody"].ToString().Trim();                          <---This does not work
        string category = newValues["MSG_CATEGORY"].ToString().Trim();
        string orig_body = Session["savedOldBody"].ToString().Trim();            <---This does not work

        string strSqlCmd = "exec dbo.wa_Update_Web_Messages " + iPKey.ToString().Trim() + "," + category + ",'" + subject.Replace("'", "''") + "','" + body.Replace("'", "''") + "','Sent'";
        DataAccess sda = new DataAccess();
        sda.RunSqlCmd(strSqlCmd);
    }

1 Answer, 1 is accepted

Sort by
0
Dan
Top achievements
Rank 2
answered on 07 Nov 2008, 09:42 PM
I solved this for myself.  In the form template, I bound the textbox to a '' value in the SqlDataSource:

<

 

radI:RadTextBox ID="txtBody" runat="server" Text='<%# Bind("TOKEN1") %>' TextMode="MultiLine" Width="500" Height="100" />

and i bound the grid text body to a non-visible label:

 

<

 

asp:Label ID="lblOrigBody" runat="server" Text='<%# Bind("BODY") %>' Visible="false" />

then in the code-behind:

 

protected

 

void GridUpdateCommand(object source, Telerik.WebControls.GridCommandEventArgs e)

 

{

 

GridEditableItem editedItem = (GridEditableItem)e.Item;

 

 

int iPKey = (int)editedItem.GetDataKeyValue("CPK_WEB_MESSAGES");

 

 

Hashtable newValues = new Hashtable();

 

e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);

 

string subject = newValues["SUBJECT"].ToString().Trim();

 

 

string body = newValues["TOKEN1"].ToString().Trim();

 

 

string category = newValues["MSG_CATEGORY"].ToString().Trim();

 

 

string orig_body = newValues["BODY"].ToString().Trim();

 

body = orig_body +

"\n\n----------- Reply by " + Session["FirstName"] + " " + Session["LastName"] + " " + DateTime.Now + " : -----------\n\n" + body;

 

 

string strSqlCmd = "exec dbo.wa_Update_Web_Messages " + iPKey.ToString().Trim() + "," + category + ",'" + subject.Replace("'", "''") + "','" + body.Replace("'", "''") + "','Sent'";

 

 

DataAccess sda = new DataAccess();

 

sda.RunSqlCmd(strSqlCmd);

}

Tags
Grid
Asked by
Dan
Top achievements
Rank 2
Answers by
Dan
Top achievements
Rank 2
Share this question
or