How do I use the ClientID with a dropdownlist?

J

jm

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

Marshal Antony

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
 
M

Marshal Antony

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 said:
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 said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top