2016-08-15 70 views
-1

的html代碼:如何獲取datalist中更改的值下拉列表?

<asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow" OnItemDataBound="DataList1_ItemDataBound" OnSelectedIndexChanged="DataList1_SelectedIndexChanged"> 
<ItemTemplate> 

<asp:DropDownList ID="listedad" data-max="30" runat="server" OnSelectedIndexChanged="DataList1_SelectedIndexChanged"> 

後面我的代碼:

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) 
    { 
int tedadkala = Convert.ToInt32(((DropDownList)e.Item.FindControl("listedad")).Text); 

int gheymat = 
Convert.ToInt32(((Label)e.Item.FindControl("lblvahed")).Text); 

int batedad = gheymat * tedadkala; 

((Label)e.Item.FindControl("lblhesab")).Text = batedad.ToString(); 
((Label)e.Item.FindControl("lblhesab")).DataBind(); 

    } 

    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e) 
    {  

    } 
+0

它只有當頁面加載正在擔心,但我改變下拉列表後: –

+0

我不能使用dropdown_textchange事件因爲在數據列表中,我真的需要你的幫幫我 –

回答

0

試試這個:

而不是做數據綁定在頁面加載直接放在一個功能,並調用它在任何你需要運行你的ItemDataBound。像這樣..

//common function for databound 
Public void databind() 
{ 
    DataList1.DataSource = // ypur datasource 
    DataList1.DataBind(); 
} 

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     databind(); 
    } 
} 

//Do the necessary coding in DataList1_SelectedIndexChanged 

Protected void DataList1_SelectedIndexChanged(Object sender, EventArgs e) 
{ 
    DropDownList ddl = (DropDownList)sender; 
    //get whatever value you need 

    databind();//Call To Function that binds To datalist 
} 

當你綁定到datalist然後itembound函數將工作的所有時間