Events for controls that are created at runtime
Ok so i have a DetailsView like so...
<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>
开发者_JAVA技巧 </Fields>
</asp:DetailsView>
Now as you can see, one of the DroDownLists has an event:
OnSelectedIndexChanged="ddlItemTypes_SelectedIndexChanged"
But for some reason, it isnt firing when i select a new item from the DropdownList. Here is how i handled it in the codebehind:
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;
}
}
Now i know that these controls are created at runtime, but shouldnt events still work for them?
Thanks!
You need to set Autopostback="true" on your dropdown.
精彩评论