在运行时创建的控件的事件

时间:2011-09-19 13:21:02

标签: c# asp.net drop-down-menu

好的,所以我有一个像这样的DetailsView ......

<asp:DetailsView DataSourceID="sqldsNewItem" CssClass="marginLeftRightBottom10px"
                DefaultMode="Insert" FieldHeaderStyle-CssClass="dwHeader" GridLines="None" ID="dwNewItem"
                runat="server">
                <Fields>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <h1>
                                Item Name:</h1>
                        <p>
                            The name of the item.</p>
                    </HeaderTemplate>
                    <InsertItemTemplate>
                        <asp:TextBox runat="server" ID="tbItemName"></asp:TextBox>
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <h1>
                            Item Description:</h1>
                        <p>
                            The description of the item.</p>
                    </HeaderTemplate>
                    <InsertItemTemplate>
                        <asp:TextBox runat="server" ID="tbItemDescription"></asp:TextBox>
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <h1>
                            Item Image:</h1>
                        <p>
                            The image of the item.</p>
                    </HeaderTemplate>
                    <InsertItemTemplate>
                        <asp:TextBox runat="server" ID="tbItemImage"></asp:TextBox>
                    </InsertItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <h1>
                            Item Type:</h1>
                        <p>
                            Specifies the item type.</p>
                    </HeaderTemplate>
                    <InsertItemTemplate>
                        <asp:DropDownList OnSelectedIndexChanged="ddlItemTypes_SelectedIndexChanged" DataTextField="itemType" DataValueField="typeId"
                            DataSourceID="sqldsTier1Category" ID="ddlItemTypes" runat="server">
                        </asp:DropDownList>
                        <asp:SqlDataSource ConnectionString="<%$ ConnectionStrings:myDbConnection%>" ID="sqldsTier1Category"
                            runat="server" SelectCommand="dbo.getItemCategories" SelectCommandType="StoredProcedure">
                        </asp:SqlDataSource>
                    </InsertItemTemplate>
            </Fields>
        </asp:DetailsView>

现在你可以看到,其中一个DroDownLists有一个事件:

OnSelectedIndexChanged="ddlItemTypes_SelectedIndexChanged"

但由于某种原因,当我从DropdownList中选择一个新项目时,它不会触发。以下是我在代码隐藏中处理它的方式:

protected void ddlItemTypes_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList ddl = (DropDownList)sender;
    if (ddl.SelectedItem.Text.ToLower() == "equipment")
    {
        TextBox tb = (TextBox)FindControl("tbItemBonusStr");
        tb.Enabled = true;
        tb = (TextBox)FindControl("tbItemBonusAgl");
        tb.Enabled = true;
        tb = (TextBox)FindControl("tbItemBonusMP");
        tb.Enabled = true;
        tb = (TextBox)FindControl("tbItemBonusHP");
        tb.Enabled = true;
        tb = (TextBox)FindControl("tbItemBonusMana");
        tb.Enabled = true;
        tb = (TextBox)FindControl("tbItemBonusIni");
        tb.Enabled = true;

        tb = (TextBox)FindControl("tbItemPermanentStr");
        tb.Enabled = false;
        tb = (TextBox)FindControl("tbItemPermanentAgl");
        tb.Enabled = false;
        tb = (TextBox)FindControl("tbItemPermanentMP");
        tb.Enabled = false;
        tb = (TextBox)FindControl("tbItemRestoresHp");
        tb.Enabled = false;
        tb = (TextBox)FindControl("tbItemRestoresMana");
        tb.Enabled = false;
    }

    else if (ddl.SelectedItem.Text.ToLower() == "consumable")
    {
        TextBox tb = (TextBox)FindControl("tbItemBonusStr");
        tb.Enabled = false;
        tb = (TextBox)FindControl("tbItemBonusAgl");
        tb.Enabled = false;
        tb = (TextBox)FindControl("tbItemBonusMP");
        tb.Enabled = false;
        tb = (TextBox)FindControl("tbItemBonusHP");
        tb.Enabled = false;
        tb = (TextBox)FindControl("tbItemBonusMana");
        tb.Enabled = false;
        tb = (TextBox)FindControl("tbItemBonusIni");
        tb.Enabled = false;

        tb = (TextBox)FindControl("tbItemPermanentStr");
        tb.Enabled = true;
        tb = (TextBox)FindControl("tbItemPermanentAgl");
        tb.Enabled = true;
        tb = (TextBox)FindControl("tbItemPermanentMP");
        tb.Enabled = true;
        tb = (TextBox)FindControl("tbItemRestoresHp");
        tb.Enabled = true;
        tb = (TextBox)FindControl("tbItemRestoresMana");
        tb.Enabled = true;
    }
}

现在我知道这些控件是在运行时创建的,但是事件仍然适用于它们吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

您需要在下拉列表中设置Autopostback =“true”。