Dropdownlist in template column to fire a SelectedIndexChanged event?

Discussion in 'ASP .Net Datagrid Control' started by MarkMurphy, Aug 23, 2004.

  1. MarkMurphy

    MarkMurphy Guest

    I have a ddl in a template column of a data grid. The ddl displays
    fine. I'm having no luck though getting an event to fire when the
    user selects a new value in a row's ddl. I can put a button in the
    template and get the command event for the grid, but nothing for the
    ddl.

    I have the ddl set to use view state and to autopostback.

    Thanks

    Mark
    MarkMurphy, Aug 23, 2004
    #1
    1. Advertising

  2. Hi Mark,

    As for the Datagrid's nested dropdownlist's postback issue, I think we can
    check the following things first:
    1. Has the dropdownlist been set as AutoPostBack= true?

    2. How do you wireup the selectedIndexchanged event handler for the
    dropdownlist?

    Here is a test page , you may have a look to see whether there are any
    difference:

    =============aspx=============
    <HTML>
    <HEAD>
    <title>ddlgrid</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema"
    content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body>
    <form id="Form1" method="post" runat="server">
    <table width="100%" align="center">
    <tr>
    <td>
    <asp:DataGrid id="dgMain" runat="server" AutoGenerateColumns="False">
    <Columns>
    <asp:TemplateColumn HeaderText="DropDownList">
    <ItemTemplate>
    <asp:DropDownList AutoPostBack="True" id="lstItems"
    runat="server" OnSelectedIndexChanged="lstItems_SelectedIndexChanged"
    SelectedIndex='<%# Container.DataItem %>'>
    <asp:ListItem Value="aaaa" Selected="True">aaaa</asp:ListItem>
    <asp:ListItem Value="bbbb">bbbb</asp:ListItem>
    <asp:ListItem Value="cccc">cccc</asp:ListItem>
    <asp:ListItem Value="dddd">dddd</asp:ListItem>
    </asp:DropDownList>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>
    </td>
    </tr>
    <tr>
    <td></td>
    </tr>
    </table>
    </form>
    </body>
    </HTML>

    ======code behind==============
    public class ddlgrid : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataGrid dgMain;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here
    if(!IsPostBack)
    {
    int[] values = new int[]{0,1,2,3,2,3,1,2};
    dgMain.DataSource = values;
    dgMain.DataBind();
    }

    }




    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    protected void lstItems_SelectedIndexChanged(object sender,
    System.EventArgs e)
    {
    System.Web.UI.WebControls.DropDownList lst =
    (System.Web.UI.WebControls.DropDownList)sender;
    Response.Write("<br>New selected index of " + lst.ID + "is " +
    lst.SelectedIndex );
    }
    }
    ===================================================

    Thanks.

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
    Steven Cheng[MSFT], Aug 24, 2004
    #2
    1. Advertising

  3. MarkMurphy

    MarkMurphy Guest

    Hi Steven,

    Your example works. Is there a way though that I can get data from the
    datagrid row to the event handler? That's why I was looking a getting
    a datagrid event to fire. I guess I could encode what I need into the
    list for each row. BTW, I'm not using the grid's
    datasource/dataadapter for doing updates.

    Thanks, Mark
    MarkMurphy, Aug 25, 2004
    #3
  4. Hi Mark,

    Since the dropdownlist control is nested in the each DataGridItem's certain
    cell. We can use the "Control" 's "Parent" property to reference its parent
    control. Then in the dropdownlist's selectedindexchanged event, we can use
    the following code to reference the DataGridItem :

    protected void lstItems_SelectedIndexChanged(object sender,
    System.EventArgs e)
    {
    System.Web.UI.WebControls.DropDownList lst =
    (System.Web.UI.WebControls.DropDownList)sender;
    DataGridItem dgi = lst.Parent.Parent as DataGridItem;

    if(dgi != null)
    {
    Response.Write("<br>ItemIndex: " + dgi.ItemIndex );
    Response.Write("<br>Cells Count: " + dgi.Cells.Count);
    }

    }

    Then, if you want to retrieve the values in other cells, just search the
    certain SubControl in the DataGridItem's other Cells.

    Thanks.'

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
    Steven Cheng[MSFT], Aug 26, 2004
    #4
  5. MarkMurphy

    MarkMurphy Guest

    Thanks again! That does the trick.
    MarkMurphy, Aug 26, 2004
    #5
  6. My Pleasure!:)
    Steven Cheng[MSFT], Aug 27, 2004
    #6
    1. Advertising

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. CW
    Replies:
    2
    Views:
    7,052
  2. Daniel
    Replies:
    2
    Views:
    862
    Steven Cheng[MSFT]
    Jun 9, 2004
  3. Daniel
    Replies:
    7
    Views:
    4,369
    Steven Cheng[MSFT]
    Jun 10, 2004
  4. Replies:
    2
    Views:
    18,724
  5. Damien
    Replies:
    7
    Views:
    696
    Mark Rae
    Jan 26, 2007
Loading...

Share This Page