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

10 posts, 0 answers
  1. Tarkon
    Tarkon avatar
    17 posts
    Member since:
    Aug 2007

    Posted 21 Mar 2011 Link to this post

    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!
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 22 Mar 2011 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Tarkon
    Tarkon avatar
    17 posts
    Member since:
    Aug 2007

    Posted 23 Mar 2011 Link to this post

    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;
                }
            }

  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 24 Mar 2011 Link to this post

    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.
  6. Tarkon
    Tarkon avatar
    17 posts
    Member since:
    Aug 2007

    Posted 24 Mar 2011 Link to this post

    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.
  7. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 25 Mar 2011 Link to this post

    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
  8. Tarkon
    Tarkon avatar
    17 posts
    Member since:
    Aug 2007

    Posted 25 Mar 2011 Link to this post

    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. 
  9. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 25 Mar 2011 Link to this post

    Hi Tarkon,
    plz send some more code.

    Thanks,
    Jayesh Goyani
  10. Tarkon
    Tarkon avatar
    17 posts
    Member since:
    Aug 2007

    Posted 25 Mar 2011 Link to this post

    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.
  11. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 28 Mar 2011 Link to this post

    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
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017