2013-01-07 79 views
2

我有一個DropDownExtender TextBox的TargetControl和包含GridView的Panel的DropDownControl。我正在使用GridView,因此我可以顯示多列,但我願意接受其他建議。AJAX DropDownExtender:如何從一個GridView中獲取值到一個TextBox中?

當用戶在GridView中選擇一行時,我想使用GridView中特定列的值填充TextBox。但是,GridView OnSelectedIndexChanged事件不會在後面的代碼中觸發。

正如你所看到的,沒有真正的代碼可以說。我不知道要使用什麼事件。顯然,OnChange/OnSelect事件適用於其他控件,即ListBox,只是想知道是否有人使用GridView獲得任何成功。

<asp:DropDownExtender ID="DropDownExtender2" runat="server" 
     TargetControlID="TextBox2" 
     DropDownControlID="Panel1"> 
</asp:DropDownExtender> 
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox> 
<asp:Panel ID="Panel1" runat="server" > 
    <asp:GridView ID="GridView1" runat="server"></asp:GridView> 
</asp:Panel> 

回答

2

解決你的問題如下:

ASPX代碼:

> <cc1:DropDownExtender ID="DropDownExtender2" runat="server" 
> TargetControlID="TextBox1" 
>   DropDownControlID="divDataDropdown"> 
>  </cc1:DropDownExtender> 
>  <asp:TextBox ID="TextBox1" runat="server" AutoCompleteType="None" ></asp:TextBox> 
>  <div id="divDataDropdown" style="overflow-y:scroll; height:200px;" runat="server" > 
>   <asp:GridView ID="GridView1" runat="server" onselectedindexchanged="GridView1_SelectedIndexChanged"> 
>    <Columns> 
>     <asp:CommandField HeaderText="Select Data" ShowSelectButton="True" /> 
>    </Columns> 
>   </asp:GridView> 
>  </div> 

代碼背後:

> public partial class WebForm1 : System.Web.UI.Page 
>  { 
>   protected void Page_Load(object sender, EventArgs e) 
>   { 
>    List<Employee> lstEmployee = new List<Employee>(); 
>    for (int RowDataIndex = 0; RowDataIndex < 50; RowDataIndex++) 
>    { 
>     Employee objEmployee = new Employee(); 
>     objEmployee.Id = RowDataIndex; 
>     objEmployee.Name = "Employee" + RowDataIndex; 
>     lstEmployee.Add(objEmployee); 
>    } 
>    GridView1.DataSource = lstEmployee; 
>    GridView1.DataBind(); 
>   } 
> 
>   protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) 
>   { 
>    TextBox1.Text = GridView1.SelectedRow.Cells[2].Text; 
>   } 
>  } 
> 
>  public class Employee 
>  { 
>   public int Id { get; set; } 
>   public string Name { get; set; } 
>  } 
相關問題