Question

I have a DropDownExtender with a TargetControl of a TextBox and a DropDownControl of a Panel that contains a GridView. I'm using the GridView so that I can display multiple columns, but I'm open to other suggestions.

When the user selects the a row in the GridView, I'd like to populate the TextBox with a value from a specific column in the GridView. However, the GridView OnSelectedIndexChanged event isn't firing in the code behind.

As you can see, there's no real code to speak of. I don't know what event to use. Apparently the OnChange/OnSelect events work for other controls i.e. ListBox, just wondering if anyone's had any sucess with a 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>
Was it helpful?

Solution

Solution to your problem is as below :

ASPX Code :

> <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>

Code Behind :

>  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; }
>     }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top