2011-09-19 64 views
0

行,所以我有一個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; 
    } 
} 

現在我知道這些控件在運行時創建的,但不應該事件仍然爲他們工作?

謝謝!

回答

3

您需要在下拉菜單中設置Autopostback =「true」。

+0

DOH !!謝啦! – TheGateKeeper