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

Getting Value from a grid that is not in Edit Mode

2 Answers 52 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Bill
Top achievements
Rank 2
Bill asked on 23 Aug 2010, 09:01 PM
All I want to do, is to get a values from a grid when it is not in edit mode. Note that I'm able to get two of the values (both the int32 fields), but I cannot get a reference to the "IsActive" Checkbox or the string "Delimiter" field.

Please let me know how I can accomplish this. Below is my code-behind. Notice the boldfaced fields I can't get...

protected void itemUp_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton imgB = (ImageButton)sender;
            GridDataItem dataItem = (GridDataItem)imgB.NamingContainer;
           
           
            string IsActive = dataItem["IsActive"].Text.ToString();
            if (IsActive == "1")
            {
                Int32 mapID = Int32.Parse(dataItem["Mapid"].Text.ToString());
                Int32 fTypeID = Int32.Parse(dataItem["FileTypeID"].Text.ToString());
                string Delimiter = dataItem["Delimiter"].Text.ToString();

                if (fTypeID == 1)
                    Response.Redirect("~/mapping/mapping.aspx?id=" + mapID.ToString());
                else
                    Response.Redirect("~/mapping/mappingDelimiter.aspx?id=" + mapID.ToString() + "&Del=" + Delimiter.ToString() + "&Ftype=" + fTypeID.ToString());
            }
            else
            {
                String s = "alert('This item must be set to Active before any processing is done with it!');";
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "alert", s, true);
            }

Below, is my markup. Once again, notice the boldfaced fields:
<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" AllowSorting="True"
                            OnNeedDataSource="RadGrid1_NeedDataSource" OnUpdateCommand="RadGrid1_UpdateCommand"
                            OnInsertCommand="RadGrid1_InsertCommand" Skin="Outlook" AutoGenerateColumns="false">
                            <MasterTableView Width="100%" DataKeyNames="Mapid" AutoGenerateColumns="false" CommandItemDisplay="Top"
                                EditMode="EditForms">
                                <Columns>
                                    <telerik:GridEditCommandColumn UniqueName="EditCommandColumn">
                                    </telerik:GridEditCommandColumn>
                                 
                                    <telerik:GridTemplateColumn HeaderText="Multi Rec" SortExpression="IsMultiRecordType"
                                        UniqueName="IsMultiRecordType">
                                        <ItemTemplate>
                                            <asp:CheckBox ID="chkIsMultiRecordType" runat="server" Checked='<%# Bind("IsMultiRecordType") %>' Enabled="false" />
                                        </ItemTemplate>
                                        <EditItemTemplate>
                                            <asp:CheckBox ID="IsMultiRecordType" runat="server" AutoPostBack="true" OnCheckedChanged="IsMultiRecordType_OnCheckedChanged"
                                                Checked='<%# NFE.Common.Classes.UIUtilities.getCheckedValue(Eval( "IsMultiRecordType" )) %>' />
                                        </EditItemTemplate>
                                    </telerik:GridTemplateColumn>
                                    <telerik:GridTemplateColumn HeaderText="File Type" SortExpression="fileType" UniqueName="validationType">
                                        <ItemTemplate>
                                            <asp:Label ID="lblFileType" runat="server" Text='<%# Bind("fileType") %>' />
                                        </ItemTemplate>
                                        <EditItemTemplate>
                                            <asp:DropDownList ID="FileTypeID" runat="server" DataSourceID="LinqFileTypeID" DataTextField="Description"
                                                AppendDataBoundItems="true" DataValueField="FileTypeID" AutoPostBack="true" OnSelectedIndexChanged="FileTypeID_SelectedIndexChanged"
                                                SelectedValue='<%# Bind("FileTypeID") %>'>
                                                <asp:ListItem Selected="True" Text="Select" Value=""></asp:ListItem>
                                            </asp:DropDownList>
                                        </EditItemTemplate>
                                    </telerik:GridTemplateColumn>
                                    <telerik:GridTemplateColumn HeaderText="Start Position" SortExpression="RecordIndicatorStartPosition"
                                        UniqueName="RecordIndicatorStartPosition">
                                        <ItemTemplate>
                                            <asp:Label ID="lblStartPosition" runat="server" Text='<%# Bind("RecordIndicatorStartPosition") %>' />
                                        </ItemTemplate>
                                        <EditItemTemplate>
                                            <asp:TextBox ID="StartPosition" runat="server" Text='<%# Eval("RecordIndicatorStartPosition") %>'>
                                            </asp:TextBox>
                                            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Start Position must be numeric"
                                                ControlToValidate="StartPosition" Font-Bold="true" ValidationExpression="^[1-9]+[0-9]*$"></asp:RegularExpressionValidator>
                                        </EditItemTemplate>
                                    </telerik:GridTemplateColumn>
                                    <telerik:GridTemplateColumn HeaderText="End Position" SortExpression="RecordIndicatorEndPosition"
                                        UniqueName="RecordIndicatorEndPosition">
                                        <ItemTemplate>
                                            <asp:Label ID="lblEndPosition" runat="server" Text='<%# Bind("RecordIndicatorEndPosition") %>' />
                                        </ItemTemplate>
                                        <EditItemTemplate>
                                            <asp:TextBox ID="EndPosition" runat="server" Text='<%# Eval("RecordIndicatorEndPosition") %>'>
                                            </asp:TextBox>
                                            <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="End Position must be numeric"
                                                ControlToValidate="EndPosition" Font-Bold="true" ValidationExpression="^[1-9]+[0-9]*$"></asp:RegularExpressionValidator>
                                        </EditItemTemplate>
                                    </telerik:GridTemplateColumn>
                                    <telerik:GridTemplateColumn HeaderText="Delimiter" SortExpression="Delimiter" UniqueName="Delimiter">
                                        <ItemTemplate>
                                            <asp:Label ID="lblDelimiter" runat="server" Text='<%# Bind("Delimiter") %>' />
                                        </ItemTemplate>
                                        <EditItemTemplate>
                                            <asp:TextBox ID="Delimiter" runat="server" MaxLength="1" Text='<%# Bind("Delimiter") %>' />
                                        </EditItemTemplate>
                                    </telerik:GridTemplateColumn>
                                    <telerik:GridTemplateColumn HeaderText="First Rec Indicator" SortExpression="FirstRecordIndicator"
                                        UniqueName="FirstRecordIndicator">
                                        <ItemTemplate>
                                            <asp:Label ID="lblFirstRecordIndicator" runat="server" Text='<%# Bind("FirstRecordIndicator") %>' />
                                        </ItemTemplate>
                                        <EditItemTemplate>
                                            <asp:TextBox ID="FirstRecordIndicator" runat="server" Text='<%# Bind("FirstRecordIndicator") %>' />
                                        </EditItemTemplate>
                                    </telerik:GridTemplateColumn>
                                    <telerik:GridTemplateColumn HeaderText="Test/Prod" SortExpression="MapStatus" UniqueName="MapStatus">
                                        <ItemTemplate>
                                            <asp:Label ID="lblStatus" runat="server" Text='<%# Bind("MapStatus") %>' />
                                        </ItemTemplate>
                                        <EditItemTemplate>
                                            <asp:DropDownList ID="MapStatusID" runat="server" DataSourceID="LinqMapStatusID"
                                                DataTextField="Description" AppendDataBoundItems="true" DataValueField="mapStatusID"
                                                SelectedValue='<%# Bind("MapStatusID") %>'>
                                                <asp:ListItem Selected="True" Text="Select" Value=""></asp:ListItem>
                                            </asp:DropDownList>
                                        </EditItemTemplate>
                                    </telerik:GridTemplateColumn>
                                    <telerik:GridTemplateColumn HeaderText="IsActive" SortExpression="IsActive" UniqueName="IsActive">
                                        <ItemTemplate>
                                            <asp:CheckBox ID="chkIsActive" runat="server" Checked='<%# Bind("IsActive") %>' Enabled="false" />
                                        </ItemTemplate>
                                        <EditItemTemplate>
                                            <asp:CheckBox ID="IsActive" runat="server" Checked='<%# NFE.Common.Classes.UIUtilities.getCheckedValue(Eval( "IsActive" )) %>' />
                                        </EditItemTemplate>
                                    </telerik:GridTemplateColumn>
                                    <telerik:GridTemplateColumn HeaderText="" UniqueName="idM">
                                        <ItemTemplate>
                                            <asp:ImageButton ID="mDetail" runat="server" ImageUrl="~/images/campaign.gif" OnClick="itemUp_Click"
                                                ToolTip="Detail Map" />
                                        </ItemTemplate>
                                    </telerik:GridTemplateColumn>
                                    <telerik:GridBoundColumn DataField="Mapid" HeaderText="Mapid" HtmlEncode="true" SortExpression="Mapid"
                                        UniqueName="Mapid" Visible="false" ReadOnly="true">
                                    </telerik:GridBoundColumn>
                                    <telerik:GridBoundColumn DataField="FileTypeID" HeaderText="FileTypeID" HtmlEncode="true"
                                        SortExpression="FileTypeID" UniqueName="FileTypeID" Visible="false" ReadOnly>
                                    </telerik:GridBoundColumn>
                                </Columns>
                            </MasterTableView>
                            <HeaderStyle Font-Bold="True"></HeaderStyle>
                            <ClientSettings EnableRowHoverStyle="True">
                            </ClientSettings>
                        </telerik:RadGrid>

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 25 Aug 2010, 07:52 AM
Hello William,

Since 'IsActive' and 'Delimiter' columns of grid are GridTemplateColumn, you cannot access the column value using 'ColumnUniqueName' . You need to access the control first and then get the value of that control. Check out the following code snippet.

C#:
GridDataItem dataItem = (GridDataItem)imgB.NamingContainer;
CheckBox chk = (CheckBox)dataItem.FindControl("chkIsActive");
string IsActive = chk.Checked.ToString(); // get the value of CheckBox (value is either 'True' or 'False')
Label lbl = (Label)dataItem.FindControl("lblDelimiter");
string Delimiter = lbl.Text; // get the text inside Label

Thanks,
Princy.
0
Bill
Top achievements
Rank 2
answered on 25 Aug 2010, 03:03 PM
Thanks Princy! It resolved the issue.
Tags
Grid
Asked by
Bill
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
Bill
Top achievements
Rank 2
Share this question
or