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

Change field to read-only in edit mode (and blocking databind)

9 Answers 309 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tarkon
Top achievements
Rank 1
Tarkon asked on 21 Mar 2011, 08:02 PM
Hi,

I'm working on a time sheet application that has a dropdownlist on the first column to select the charge code.  The requirements I have is that this column be read-only in edit mode to disallow the user to change the charge code.  I've seen some code examples online, but they don't completely solve the problem because sometimes the charge codes are inactivated for a user, so they will no longer show up on in the dropdownlist, which throws an error. 

Is there a way to switch this to readonly on the PreRender, or otherwise prevent the dropdownlist from databinding?

Thanks!

9 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 22 Mar 2011, 06:24 AM
Hello Tarkon,

Try the following code snippet to disable DropDownList in edit form.

ASPX:
<telerik:GridTemplateColumn>
      <EditItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"
                    DataTextField="FirstName" DataValueField="FirstName">
                </asp:DropDownList>
       </EditItemTemplate>
 </telerik:GridTemplateColumn>

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
         if (e.Item.IsInEditMode && e.Item is GridEditFormItem)
        {
            GridEditFormItem edititem = (GridEditFormItem)e.Item;
            DropDownList drplist = (DropDownList)edititem.FindControl("DropDownList1");
            if(//your condition)
              drplist.Enabled = false;
         }
    }

Thanks,
Princy.
0
Tarkon
Top achievements
Rank 1
answered on 23 Mar 2011, 06:34 PM
I took your advice, but I'm getting an

'AccountNoDropDown' has a SelectedValue which is invalid because it does not exist in the list of items.

error on editing it. 

<telerik:GridTemplateColumn DataField="AccountNo" DefaultInsertValue="" ItemStyle-Width="350px"
            HeaderText="Account Number" SortExpression="AccountNo" UniqueName="AccountNo" >
            <EditItemTemplate>
                <asp:Label ID="AccountNoLabel" runat="server" Text='<%# Eval("ChargeCode") %>' Visible="false"></asp:Label>
                <asp:DropDownList ID="AccountNoDropDown" runat="server" DataSourceID="DirectChargeCodesDataSource" DataValueField="ChargeCodeName" DataTextField="ChargeCodeName" SelectedValue='<%# Bind("ChargeCode") %>' Font-Size=".9em" ></asp:DropDownList>
                <asp:RequiredFieldValidator ID="AccountNoValidator" runat="server" CssClass="error" ControlToValidate="AccountNoDropDown" ValidationGroup="Timesheet" EnableClientScript="false" SetFocusOnError="true" Display="Dynamic">*</asp:RequiredFieldValidator>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="AccountNoLabel" runat="server" Text='<%# Eval("ChargeCode") %>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <span style="font-weight:bold;">TOTAL HOURS:</span>
            </FooterTemplate>
        <ItemStyle CssClass="chargeCodeHeader"></ItemStyle>
        </telerik:GridTemplateColumn>

protected void TimesheetRadGrid_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode) && !e.Item.OwnerTableView.IsItemInserted)
            {
                GridEditableItem edititem = (GridEditableItem)e.Item;
 
                DropDownList AccountNoDropDown = (DropDownList)e.Item.FindControl("AccountNoDropDown");
                AccountNoDropDown.Enabled = false;
                AccountNoDropDown.Visible = false;
 
                Label AccountNoLabel = (Label)e.Item.FindControl("AccountNoLabel");
                AccountNoLabel.Visible = true;
            }
        }

0
Princy
Top achievements
Rank 2
answered on 24 Mar 2011, 10:07 AM
Hello Tarkon,

Please change the 'DataValueField' property of DropDownList to 'ChargeCode' and see if it works now.

ASPX:
<EditItemTemplate>
     <asp:DropDownList ID="AccountNoDropDown" runat="server" DataSourceID="DirectChargeCodesDataSource"
                 DataValueField="ChargeCode"
                    DataTextField="ChargeCodeName" SelectedValue='<%# Bind("ChargeCode") %>' Font-Size=".9em" >
    </asp:DropDownList>
 </EditItemTemplate>

Thanks,
Princy.
0
Tarkon
Top achievements
Rank 1
answered on 24 Mar 2011, 07:04 PM
No, I'm afraid not (ChargeCode and ChargeCode name are the same fields from different tables).

The problem is that sometimes charge codes can be removed, so it'll no longer be in the dropdownlist - so when the person edits the row that dropdownlist will fail on databinding whether it's enabled or not.  I need to be able to prevent it from databinding beforehand so that it doesn't fail, such as setting the column to readonly.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 25 Mar 2011, 07:49 AM
hi  Tarkon,


if( AccountNoDropDown.Items.FindItemByValue(Convert.ToString(AccountNoLabel.Text)) != null)
{
     AccountNoDropDown.Items.FindItemByValue(Convert.ToString(AccountNoLabel.Text)).Selected = true;
}
else
 
{
    // Item was not found dropdown
}
 
//You may also use FindItemByText in place of FindItemByValue.

Remove this property from aspx page :  SelectedValue='<%# Bind("ChargeCode") %>'


Thanks,
Jayesh Goyani
0
Tarkon
Top achievements
Rank 1
answered on 25 Mar 2011, 05:02 PM
I tried that, but now it's not saving the value of the dropdownlist when I insert - it just inserts a NULL into the table.  This solution seems to be the reverse of what I want to achieve. 
0
Jayesh Goyani
Top achievements
Rank 2
answered on 25 Mar 2011, 05:11 PM
Hi Tarkon,
plz send some more code.

Thanks,
Jayesh Goyani
0
Tarkon
Top achievements
Rank 1
answered on 25 Mar 2011, 06:52 PM
Just to remind: I have a column that contains a dropdownlist.  The requirements from the client is that this list only be active on adding a new row, and should be read-only on edit mode.  One of the problems is that items in the dropdownlist DataSource will drop-out, which throws an error on databind. 

I either need to find a way to make the column read-only on edit (to prevent the dropdownlist from databinding), or somehow stop the databind event on the dropdownlist beforehand (such as in PreRender) - otherwise it throws an error.

I'm using a SQLDataAdapaters to populate the RadGrid, and another adapter to populate the DropDownList.

If there's code you require that I haven't sent, please let me know what you require.

Thank you for your help.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 28 Mar 2011, 06:48 AM
Hi Tarkon,

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
                    OnInsertCommand="RadGrid1_InsertCommand" OnItemDataBound="RadGrid1_ItemDataBound">
                    <MasterTableView CommandItemDisplay="Top">
                        <Columns>
                            <telerik:GridTemplateColumn HeaderText="ID">
                                <ItemTemplate>
                                    <asp:Label ID="lblID" runat="server" Text=' <%#Eval("StudentID")%>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <telerik:RadTextBox ID="txtID" runat="server" Text=' <%#Eval("StudentID")%>'>
                                    </telerik:RadTextBox>
                                </EditItemTemplate>
                            </telerik:GridTemplateColumn>
                            <telerik:GridTemplateColumn HeaderText="Name">
                                <ItemTemplate>
                                    <asp:Label ID="lblFirstName" runat="server" Text=' <%#Eval("FirstName")%>'></asp:Label>
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <telerik:RadComboBox ID="ddlFirstName" runat="server">
                                    </telerik:RadComboBox>
                                    <asp:Label ID="lblFirstNameEdit" runat="server" Text=' <%#Eval("FirstName")%>' Visible="false"></asp:Label>
                                </EditItemTemplate>
                            </telerik:GridTemplateColumn>
                            <telerik:GridEditCommandColumn>
                            </telerik:GridEditCommandColumn>
                        </Columns>
                    </MasterTableView>
                </telerik:RadGrid>

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    RadGrid1.DataSource = new StudentDomain().SelectAllStudents();
}
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditFormItem && e.Item.IsInEditMode && !e.Item.OwnerTableView.IsItemInserted)
    ////if the item is about to edit
    {
        GridEditFormItem insertItem = (GridEditFormItem)e.Item;
        RadComboBox ddlFirstName = (RadComboBox)insertItem.FindControl("ddlFirstName");
        Label lblFirstNameEdit = (Label)insertItem.FindControl("lblFirstNameEdit");
        DataTable dt = new DataTable();
        dt.Columns.Add("FirstName", typeof(string));
        dt.Rows.Add("ABC");
        dt.Rows.Add("XYZ");
        ddlFirstName.DataSource = dt;
        ddlFirstName.DataTextField = "FirstName";
        ddlFirstName.DataValueField = "FirstName";
        ddlFirstName.DataBind();
 
        if (ddlFirstName.Items.FindItemByValue(Convert.ToString(lblFirstNameEdit.Text)) != null)
        {
            ddlFirstName.Items.FindItemByValue(Convert.ToString(lblFirstNameEdit.Text)).Selected = true;
        }
        else
        {
            //// Item was not found dropdown
        }
 
        ddlFirstName.Enabled = false;
         
    }
    if (e.Item is GridEditFormInsertItem && e.Item.OwnerTableView.IsItemInserted)
    ////if the item is about to insert
    {
        GridEditFormInsertItem insertItem = (GridEditFormInsertItem)e.Item;
        RadComboBox ddlFirstName = (RadComboBox)insertItem.FindControl("ddlFirstName");
        DataTable dt = new DataTable();
        dt.Columns.Add("FirstName",typeof(string));
        dt.Rows.Add("ABC");
        dt.Rows.Add("XYZ");
        ddlFirstName.DataSource = dt;
        ddlFirstName.DataTextField = "FirstName";
        ddlFirstName.DataValueField = "FirstName";
        ddlFirstName.DataBind();
 
 
    }
}
protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
{
    GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;
    string strID = ((RadTextBox)insertedItem.FindControl("txtID")).Text;
    string strFirstName = ((RadComboBox)insertedItem.FindControl("ddlFirstName")).SelectedItem.Text;
}


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Tarkon
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Tarkon
Top achievements
Rank 1
Jayesh Goyani
Top achievements
Rank 2
Share this question
or