GridView DropDownList events

Discussion in 'ASP .Net Datagrid Control' started by dev648237923, Feb 22, 2007.

  1. dev648237923

    dev648237923 Guest

    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!
    dev648237923, Feb 22, 2007
    1. Advertisements

  2. 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 >
    <asp:DropDownList 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.
    Steven Cheng[MSFT], Feb 23, 2007
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SnVhbmpv?=

    GridView: Filtr DropDownList from another DropDownList

    =?Utf-8?B?SnVhbmpv?=, Dec 23, 2005, in forum: ASP .Net
    Dec 23, 2005
  2. =?Utf-8?B?SnVhbmpv?=

    GridView: Filter DropDownList for another DropDownList

    =?Utf-8?B?SnVhbmpv?=, Dec 29, 2005, in forum: ASP .Net
    Dec 29, 2005
  3. keithb
    Nov 1, 2006
  4. =?Utf-8?B?bWdvbnphbGVzMw==?=

    GridView Hierarchical View - Gridview in Gridview

    =?Utf-8?B?bWdvbnphbGVzMw==?=, May 9, 2006, in forum: ASP .Net
    May 9, 2006
  5. Chris

    Events Events Events Please Help

    Chris, Aug 30, 2005, in forum: ASP .Net Web Controls
    Aug 30, 2005

Share This Page