Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > GridView DropDownList events

Thread Tools

GridView DropDownList events

Posts: n/a
When editing I show a DropDownList.
I want to show/hide a textbox next to that list if the user selects Other.

I've tried many ways but I can get it to work:
1.) If I knew the TextBox controls name/id I could just add an onchange
attribute to my DDL during RowDataBound and then put in some javascript to
show/hide the textbox but I never know in advance the name/id of the textbox
since it changes depending on what row I am on.
2.) I could set the DDL to AutoPostBack but then how would I affect the
textBox in that row?

Thanks for any ideas!

Reply With Quote
Steven Cheng[MSFT]
Posts: n/a
Hello dev648237923,

Regarding on the dropdownlist and textbox interact requirement, I think you
can use the GridView.RowCreated event to set script handler for the
DropDownList( the "onchange" client event). And at that time, you can get
the Textbox instance also and embed its "ClientID"(which represent its id
at client-side html) into your script function call. For example, suppose
you have a TemplateField in gridview as below:

<asp:TemplateField HeaderText="template column">
<ItemTemplate >
<aspropDownList ID="lstTest" runat="server" >
<asp:ListItem Text="aaa" Value="aaa"></asp:ListItem>
<asp:ListItem Text="bbb" Value="bbb"></asp:ListItem>
<asp:ListItem Text="ccc" Value="ccc"></asp:ListItem>
<asp:TextBox ID="txtTest" runat="server">


In GridView's RowCreated event, you can find the DropDownList and Textbox
and do some customization on them:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
if (e.Row.RowType == DataControlRowType.DataRow &&
e.Row.RowState == DataControlRowState.Normal)
DropDownList lst = e.Row.FindControl("lstTest") as
TextBox txt = e.Row.FindControl("txtTest") as TextBox;

lst.Attributes["onchange"] =
"javascript:AdjustTextBox(this, '" + txt.ClientID + "');";


Here is the client script functino
<script type="text/javascript">

function AdjustTextBox(lst, txtid)
var txt = document.getElementById(txtid);

txt.value = lst.selectedIndex;


You can adjust the test code to fit your specific requirement. If you have
any further questions on this, please feel free to let me know.


Steven Cheng

Microsoft MSDN Online Support Lead


Get notification to my posts through email? Please refer to

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at


This posting is provided "AS IS" with no warranties, and confers no rights.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
DropDownList inside GridView: How to set SelectedIndex for the DropDownList? keithb ASP .Net 1 11-01-2006 05:24 AM
GridView Hierarchical View - Gridview in Gridview =?Utf-8?B?bWdvbnphbGVzMw==?= ASP .Net 1 05-09-2006 06:48 PM
GridView: Filter DropDownList for another DropDownList =?Utf-8?B?SnVhbmpv?= ASP .Net 0 12-29-2005 07:44 AM
GridView: Filtr DropDownList from another DropDownList =?Utf-8?B?SnVhbmpv?= ASP .Net 0 12-23-2005 01:31 PM
Events Events Events Please Help Chris ASP .Net Web Controls 0 08-30-2005 08:21 PM