Datagird Question?

Discussion in 'ASP .Net Datagrid Control' started by Arvind P Rangan, Feb 19, 2004.

  1. Hi,

    How do we retrieve the ids of controls that datagrid generates at runtime.

    If this question has been answered early pls tell me how to find it.

    Thanks
    Arvind.
     
    Arvind P Rangan, Feb 19, 2004
    #1
    1. Advertising

  2. "Arvind P Rangan" <> wrote in message
    news:ucOi$...
    > Hi,
    >
    > How do we retrieve the ids of controls that datagrid generates at runtime.
    >
    > If this question has been answered early pls tell me how to find it.
    >
    > Thanks
    > Arvind.
    >
    >

    Hi Arvind,
    You can retrieve id of controls using ClientID property.
    For eg :e.Item.Cells[0].ClientID

    I answered a similar question for a particular requirement which might be
    useful for you.
    I am just copying it below for you.
    HTH
    Regards,
    Marshal Antony
    ..NET Developer
    http://www.dotnetmarshal.com


    From: "Marshal Antony" <>
    Subject: Re: How do I use the ClientID with a dropdownlist?
    Date: Wednesday, February 11, 2004 11:05 PM

    Hi Jim,
    I thought I could create an example for you for your specific need.
    You can put a javascript function on the aspx page client side and pass
    the ClientID of the dropdownlist to it in the onclick of the button
    from the server side.I have a button and dropdown list as template columns
    in the datagrid and a datatable is created on the fly to populate
    the datagid.I am attaching the aspx file and codebehind file from the
    project as well.
    Here is the code :
    Javascript is :
    function GetSelectedIndex(index){
    var selected_dl=document.getElementById(index);
    var selected_index=selected_dl.selectedIndex; alert("Index : " +
    selected_index + " Value : " + selected_dl.options[selected_index].text);
    }
    server side code is :
    private void DataGrid1_ItemDataBound(object sender,
    System.Web.UI.WebControls.DataGridItemEventArgs e)

    {


    if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==

    ListItemType.AlternatingItem || e.Item.ItemType ==

    ListItemType.SelectedItem)

    {

    i=i+1;


    DropDownList dl=(DropDownList)e.Item.FindControl("dlist1");

    dl.SelectedIndex=j;

    Button b1=(Button)e.Item.FindControl("button1");

    b1.Text= " Button " + i;

    b1.Attributes.Add("onclick","GetSelectedIndex('" + dl.ClientID + "');");

    }

    }

    Hope this helps.

    Regards,

    Marshal Antony

    ..NET Developer

    http://www.dotnetmarshal.com





    of the
    "Marshal Antony" <> wrote in message
    news:...
    > Hi Jim,
    > Give an id for the drop down list in the aspx file.
    > If the id of the drop down is is dlist1,
    > In your ItemDataBound event of the grid,
    >
    > if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
    > ListItemType.AlternatingItem || e.Item.ItemType ==
    > ListItemType.SelectedItem)
    >
    > {
    >
    > DropDownList dl=(DropDownList)e.Item.FindControl("dlist1");
    >
    > Put an id for the button as well.If the id of the buton is button1,
    >
    > Button b1=(Button)e.Item.FindControl("button1");
    >
    > build your cleint side script here :
    >
    > AS you need client side event onclick to each button,
    >
    > b1.Attributes.Add("onclick","alert('" + dl.SelectedIndex + "');");
    >
    > This will add client side onclick to each button with their

    correspoding
    > SelectedIndex for the drop down..
    >
    > I didn't test this code yet.So please make sure it works otherwise let

    me
    > know.
    >
    > Hope this helps.
    >
    > Regards,
    >
    > Marshal Antony
    >
    > .NET Developer
    >
    > http://www.dotnetmarshal.com
    >
    >
    >
    >
    >
    >
    > "jm" <> wrote in message
    > news:...
    > > I have datagrid. On this datagrid there is a button and a
    > > dropdownlist.
    > >
    > > When I press the button, I want the selectedIndex of the dropdownlist.
    > >
    > > There is a button and a dropdownlist for each datagrid row that is
    > > populated from the database. Example, five rows returned, five
    > > buttons, and five dropdownlists.
    > >
    > > I know I am supposed to use the ClientID, but I cannot figure out how
    > > to use ClientID to build the Javascript for each row returned. I just
    > > can't picture it.
    > >
    > > In regular Javascript, I can use:
    > >
    > > var the_select =

    > document.getElementById("ISMS_List__ctl1_mni_inv_type_ddl");
    > > var the_index = the_select.selectedIndex;
    > > o.mni_type_code = the_select.options[the_index].text;
    > >
    > > But, of course, I need the correct name of the dropdownlist for the
    > > row that I am on. I don't understand it. Please help. Thank you.

    >
    >
     
    Marshal Antony, Feb 20, 2004
    #2
    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. Ola Myrgart

    How to insert URL:s into datagird

    Ola Myrgart, Oct 21, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    357
    Ola Myrgart
    Oct 21, 2003
  2. Douglas Gage
    Replies:
    0
    Views:
    406
    Douglas Gage
    Jan 14, 2004
  3. Slipperman

    EditItem in datagird

    Slipperman, Feb 15, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    358
    Slipperman
    Feb 15, 2004
  4. =?Utf-8?B?SUdvdFlvdXJEb3ROZXQ=?=

    datagird question

    =?Utf-8?B?SUdvdFlvdXJEb3ROZXQ=?=, Aug 16, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    355
    =?Utf-8?B?SUdvdFlvdXJEb3ROZXQ=?=
    Aug 16, 2004
  5. Gaurav Gargate
    Replies:
    2
    Views:
    580
    Gaurav Gargate
    Oct 6, 2004
Loading...

Share This Page