DataGrid中dropdownlist事件的觸發ImageButton的事件觸發

aspx頁面部分代碼:

<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
    <Columns>
     <asp:BoundColumn DataField="txtId"></asp:BoundColumn>
     <asp:BoundColumn DataField="txtName"></asp:BoundColumn>
     <asp:TemplateColumn>
      <ItemTemplate>
        <asp:ImageButton id="btnItem" runat="server" CommandName="LookDetail"></asp:ImageButton>
        <asp:DropDownList id="ddl" runat="server" AutoPostBack="True">
         <asp:ListItem Value="asdf">asdf</asp:ListItem>
         <asp:ListItem Value="asdfasdf">asdfasdf</asp:ListItem>
         <asp:ListItem Value="wrwwewewee">wrwwewewee</asp:ListItem>
        </asp:DropDownList>
      </ItemTemplate>
     </asp:TemplateColumn>
    </Columns>
   </asp:DataGrid>

cs部分代碼:

cs部分代碼:

/// <summary>
  /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  /// </summary>
  private void InitializeComponent()
  {    
              this.DataGrid1.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemCreated);
             this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);
             this.Load += new System.EventHandler(this.Page_Load);

  }

private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   if(e.CommandName=="LookDetail")
   {
    Response.Write("clicked item--->" + e.Item.Cells[0].Text);
   }
  }

  private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  {
   DropDownList ddl=(DropDownList)e.Item.FindControl("ddl");
   if(ddl!=null)
   {
    ddl.SelectedIndexChanged+=new EventHandler(ddl_SelectedIndexChanged);
   }
  }

  private void ddl_SelectedIndexChanged(object sender, EventArgs e)
  {
   DropDownList ddl=(DropDownList)sender;
   Response.Write("當前選擇的:" + ddl.SelectedValue);

   TableCell cell=(TableCell)ddl.Parent;
   DataGridItem item=(DataGridItem)cell.Parent;
           
   Response.Write("當前行:" + item.Cells[0].Text);
  }


 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章