Modifying controls in EditTemplate prior to display.

by kkikta 1. April 2008 20:19

Today I was working with a few items in an edit template and ran into a situation where I needed to disable one of the controls based on information in another control. I quickly found that this can't really be done with an onload or in the design. I eventualy tied a function to the OnDataBinding and was able to disable a text box. The reasoning for this is that the EditTemplate is not available until the control is in "Edit" mode.

Ex.

<asp:DataList id="dlTest" onEditCommand="dlTest_EditCommand" onUpdateCommand="dlTest_UpdateCommand">
    <EditTemplate>
        <asp:HiddenField id="hdfEditCritera" runat="server" value='<%# DataBinder.Eval(Container.DataItem, "TestCritera") %>' />
        <asp:TextBox ID="txtEditTest" OnDataBinding="TextBox_Enabled" runat="server" text='<%# DataBinder.Eval(Container.DataItem, "TestData") %>' />
    </EditTemplate>
    <ItemTemplate>
        <asp:Lable ID="lblText" runat="server" text='<%# DataBinder.Eval(Container.DataItem, "TestData") %>' />
        <asp:LinkButton id="lnkEdit" runat="server" CommandName="Edit" text="Edit" />
    </ItemTemplate>
</asp:DataList>

<%
    protected void TextBox_Enabled(object sender, EventArgs e)
    {
        DataListItem EditItem = dlTest.Items[dlTest.EditItemIndex];
        if (((HiddenField)EditItem.FindControl("hdfEditCritera")).Value.StartsWith("Test", StringComparison.InvariantCultureIgnoreCase))
            ((TextBox)EditItem.FindControl("txtEditTest")).Enabled = true;
        else
            ((TextBox)EditItem.FindControl("txtEditTest")).Enabled = false;
    }
%>

In this example if the hidden field started with "test" then the textbox would be enabled if not the its disabled.

Tags:

.NET

Add comment



  Country flag
biuquote
  • Comment
  • Preview
Loading


Month List

Page List