English 中文(简体)
如何在数据列表中找到下载列表的控制
原标题:how to find control of dropdownlist inside a datalist
  • 时间:2012-05-22 12:12:08
  •  标签:
  • c#
  • asp.net

我有数据列表,里面有数据列表......我有一个下限列表:

  <asp:DataList ID="dlconfigureItem" runat="server">
    <ItemTemplate>
    <asp:DropDownList CssClass="config-select" ID="ddlitem    runat="server"></asp:DropDownList>
  </ItemTemplate>
  </asp:DataList>

如何在服务器一侧获得选中的 DownList 交换事件? I tried this:

   public void ddlitem_selectedindexchanged (object sender, EventArgs e)
    {

    }

但却不起作用。

问题回答

您定义了服务器侧边方法 :

public void ddlitem_selectedindexchanged (object sender, EventArgs e)
{

}

但你没有告诉客户方面 有一个事件给你, 所以在 html 代码 说它像:

 onselectedindexchanged="ddlitem_selectedindexchanged"

并设置 AutoPostBack 属性为真 。

sectedIndex changed 事件,最容易将 sender 投到 DropDownList

var ddl = (DropDownList)sender;

ender 总是控制事件源。

为完整起见,从 ItemDataBound > ><

protected void dlconfigureItem_ItemDataBound(object sender, DataListItemEventArgs e) 
{
    DropDownList ddlitem = e.Item.FindControl("ddlitem") as DropDownList;
    if (ddlitem != null)
    {
        // ...
    }
}

“强势”编辑

<asp:DropDownList CssClass="config-select" 
    ID="ddlitem" 
    OnSelectedIndexChanged="ddlitem_selectedindexchanged"    
    runat="server">
</asp:DropDownList>

请注意,您不应该将数据列表约束到 s 数据源,否则不会触发事件。请检查页面的 < code> IsPostBack 属性 。

例如,在 page_load 中:

if(!IsPostBack)BindDataList();

注册事件并设定 AutoPostBack=" true"

<asp:DropDownList CssClass="config-select" 
    ID="ddlitem" 
    AutoPostBack="true"
    OnSelectedIndexChanged="ddlitem_selectedindexchanged"    
    runat="server">
</asp:DropDownList>

事件(关于选定的指数变化,您可以得到选定值)

    protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        var ddlList = (DropDownList)sender;
        string selectedValue = ((DropDownList)ddlList.NamingContainer.FindControl("ddlitem")).SelectedValue;   
    }

无法确定您是否无法在服务器上获取所选项目, 或无法找到处理事件的方法 。 如果您的问题与事件处理有关, 请尝试此

  <asp:DataList ID="dlconfigureItem" runat="server">
    <ItemTemplate>
    <asp:DropDownList CssClass="config-select" ID="ddlitem" 
       OnSelectedIndexChanged="ddlitem_selectedindexchanged" 
       AutoPostBack="true" runat="server"></asp:DropDownList>
  </ItemTemplate>
  </asp:DataList>




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签