2016-09-02 33 views
0

我一直在開發一個網頁,它使用n個在GridView中動態綁定的下拉列表。我想根據下拉列表中的selectedindexchanged事件執行操作。我已經做到了這一點,並且運行良好,但是當我第二次更改dropdownlist時它會回發,但不會調用該事件。Gridview中動態創建的Dropdownlist沒有第二次觸發事件

您可以在這裏看到

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridDropDownTest.aspx.cs" 
Inherits="gridDropDownTest" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title></head><body> <form id="form1" runat="server"> <div>  <asp:GridView ID="gridLedgeDetails" runat="server" OnRowDataBound="OnRowDataBound" 
     OnDataBound="gridLedgeDetails_DataBound">  </asp:GridView> 
</div> 
</form></body></html> 


using System; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 

public partial class gridDropDownTest : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     databind(); 
    } 
    public void databind() 
    { 
     DataTable dt = new DataTable(); 
     dt.Columns.Add("Mode"); 
     dt.Rows.Add(""); 
     dt.Rows.Add(""); 
     gridLedgeDetails.DataSource = dt; 
     gridLedgeDetails.DataBind(); 
    } 
    protected void OnRowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      DropDownList ddlMode = new DropDownList(); 
      ddlMode.Width = 90; 
      ddlMode.Attributes.Add("style", "background-color:#ff6600;"); 
      ddlMode.Items.Add("Regular"); 
      ddlMode.Items.Add("Monthwise"); 
      ddlMode.SelectedIndexChanged += new EventHandler(ddlMode_Indexchanged); 
      ddlMode.AutoPostBack = true; 
      ddlMode.ID = "ddlMode_"; 
      e.Row.Cells[0].Controls.Add(ddlMode); 
     } 
    } 
    protected void gridLedgeDetails_DataBound(object sender, EventArgs e) { } 
    protected void ddlMode_Indexchanged(object sender, EventArgs e) 
    { 
     string uid = this.Page.Request.Params.Get("__EVENTTARGET"); 
     if (uid != null && uid.Contains("ddlMode_")) 
     { 
      string[] values = uid.Split('$'); 
      string row = values[1].Replace("ctl", ""); 
      Control ctrl = Page.FindControl(uid); 
      DropDownList ddl = (DropDownList)ctrl; 
      if (ddl.SelectedIndex == 1) 
      { 

      } 
     } 
    } 
} 

enter image description here

+0

次要尖端,web表單被棄用。含義Asp.net 4.6是最後一個版本。 –

回答

0

我的代碼,爲此,你需要採取和Page_PreInit頁方法再次綁定的下拉列表。

對於實例....

保護無效Page_PreInit(對象發件人,EventArgs的)

{

//這裏您需要重新建立gridview的。

//那麼狀態將保持相同......

}

+0

我已經完成了三天的工作。謝謝 –

相關問題