|
Subject:
|
how to set selectedindex
|
|
Posted By:
|
azitanosrati
|
Post Date:
|
8/10/2004 5:31:38 AM
|
how to set selectedindex in dropdownlist with optional value when i use from datalist in run time (in html code)
|
|
Reply By:
|
mehdi62b
|
Reply Date:
|
8/10/2004 9:11:08 AM
|
hi azita, I didnt get what you mean exactly.... but I think you want to change selectedindex of a dropdownlist according to the DataList item selected by user..am I right?
-------------------------------------------- Mehdi.
|
|
Reply By:
|
mehdi62b
|
Reply Date:
|
8/10/2004 9:48:02 AM
|
or you have a DropDownList in your DataList(ItemTemplate) and want to change it's selectedindex ?
-------------------------------------------- Mehdi.
|
|
Reply By:
|
bmains
|
Reply Date:
|
8/10/2004 11:05:41 AM
|
Hey,
In HTML, you have to use javascript to do so by looping through each option and find the right one.
var list = document.getElementById('DropDownList1'); for (var intI = 0; intI < list.options.count -1; intI++) { if (list.options[intI].value == 'some value') { list.options[intI].selected = true; } }
Something like that (may be a little off). At the server level, you can do:
DropDownList1.SelectedIndex = <value>
Brian
|
|
Reply By:
|
mehdi62b
|
Reply Date:
|
8/10/2004 11:40:33 AM
|
its better not to use JavaScript when you deal with datalist object(work in server-side not in client-side using HTML & javascript).
-------------------------------------------- Mehdi.
|
|
Reply By:
|
bmains
|
Reply Date:
|
8/11/2004 7:34:00 AM
|
Hey,
Well, although the server side code is easier, it is still possible to use JavaScript. Because the datalist is similar to the datagrid, where when you select an item and it posts back, it is possible to use javascript, by changing the above code to:
var list = document.getElementById('DropDownList1'); for (var intI = 0; intI < list.options.count -1; intI++) { if (list.options[intI].value == '<%=DataList1.<whatever control or attribute returns the value>') { list.options[intI].selected = true; } }
But server-side would be the easiest, especially because you are posting back anyway due to the select.
Brian
|
|
Reply By:
|
azitanosrati
|
Reply Date:
|
8/14/2004 2:54:27 AM
|
quote: Originally posted by mehdi62b
or you have a DropDownList in your DataList(ItemTemplate) and want to change it's selectedindex ?
-------------------------------------------- Mehdi.
i have a dropdown list in my datalist(edittemplate) and iwant to change dropdown list value with my value means(i want pass selectedindex value in edit template ) please help me thanks.
|
|
Reply By:
|
azitanosrati
|
Reply Date:
|
8/14/2004 3:07:28 AM
|
quote: Originally posted by mehdi62b
or you have a DropDownList in your DataList(ItemTemplate) and want to change it's selectedindex ?
-------------------------------------------- Mehdi.
my dear mehdi hi i have a dropdown list in my datalist(edittemplate) and iwant to change dropdown list value with my value means(i want pass selectedindex value in edit template from itemtemplate ) please help me thanks
|
|
Reply By:
|
mehdi62b
|
Reply Date:
|
8/14/2004 10:44:32 AM
|
I think I got it what you mean somewhere like ItemCommand event write below code
private void DataList1_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
DataList1.EditItemIndex=e.Item.ItemIndex;//make your cell like EditTemplate
((DropDownList)e.Item.FindControl("DropDownList1")).SelectedIndex=newvalue;
//find your dropdownlist in your edititemtemplate and assign new value to it's SelectedIndex
}
-------------------------------------------- Mehdi.
|
|
Reply By:
|
azitanosrati
|
Reply Date:
|
8/16/2004 2:17:38 AM
|
quote: Originally posted by mehdi62b
I think I got it what you mean somewhere like ItemCommand event write below code
private void DataList1_ItemCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
DataList1.EditItemIndex=e.Item.ItemIndex;//make your cell like EditTemplate
((DropDownList)e.Item.FindControl("DropDownList1")).SelectedIndex=newvalue;
//find your dropdownlist in your edititemtemplate and assign new value to it's SelectedIndex
}
-------------------------------------------- Mehdi.
i performed this way but dropdownlist return null value and i dont khow what do i do
|
|
Reply By:
|
bmains
|
Reply Date:
|
8/16/2004 7:54:38 AM
|
Hey,
I think you have to reference the specific cell, as such:
((DropDownList)e.Item.Cells[<cellindex>].FindControl("DropDownList1")).SelectedIndex=newvalue;
Brian
|
|
Reply By:
|
mehdi62b
|
Reply Date:
|
8/16/2004 9:31:50 AM
|
Brian,I dont think it makes any sense dear Azita, if you tell us more about your scenario we could help you much more (I think your problem is due to binding your DropDownList and DataList) can u tell me when you bind your DropDownList(it should be after binding DataList)? also before Binding your DropDownList you should determine or change your SelectedIndex(through an integer static variable could be changed everywhere u need like ItemCommand event ) Hope this helps.
-------------------------------------------- Mehdi.
|
|
Reply By:
|
bmains
|
Reply Date:
|
8/16/2004 10:15:13 AM
|
Hello,
You are right, that is for a DataGrid. Sorry for any confusion.
Brian
|
|
Reply By:
|
larrelscott
|
Reply Date:
|
9/15/2004 3:56:14 PM
|
I am new to .NET and I am still having trouble with this the topic in this post. I am trying to set the value of the dropdownlist to match the database value when I move to edit mode in the datagrid. Some of this code may be unecessary but I'm not sure what would be. I have been trying everything to get this too work with a lot of different approaches being recommended by many different forums. Sorry if it is ugly. I am getting a message that says, "Object reference not set to an instance of an object."
Here is the code I have in the DataGrid1_EditCommand method. this.sqlConnection1.Open(); this.sqlDataAdapter1.Fill(recordValuesDataSet11); this.sqlConnection1.Close(); DataGrid1.EditItemIndex = e.Item.ItemIndex; this.DataGrid1.DataBind(); ((DropDownList)e.Item.Cells[6].FindControl ("statusddl")).SelectedIndex = e.Item.Cells[6].Text; Thanks in advance for any help you can give.
|
|
Reply By:
|
planoie
|
Reply Date:
|
9/16/2004 2:17:41 PM
|
Which line is that error occuring on?
I see the call to bind the datagrid, but I don't see anything that is setting the grid's datasource. I assume that you have some code somewhere that is setting up the grid's datasource that gets executed on page load. You need to call this code again when you re-bind the grid. After the page postback to change a row into edit mode, the only data that still exists is the data in the grid's viewstate and this is not enough to rebind the grid.
|
|
Reply By:
|
mehdi62b
|
Reply Date:
|
9/16/2004 6:21:52 PM
|
Hello, if you dont have any problem with your Connection object your problem is due to binding your grid,for preventing this error you should bind your DropDownList and DataGrid like below order, 1-first bind your datagrid(before it,set its datasource) 2-then bind again your DropDownList(before it,set its datasource) for example somewhere like ItemDataBound event could be a good place for binding DropDownList meanwhile,its better to use this line in your code,
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem ||
e.Item.ItemType == ListItemType.SelectedItem ||
e.Item.ItemType == ListItemType.EditItem)
{
((DropDownList)e.Item.FindControl ("statusddl")).SelectedIndex = anIntegerVariable;
}
you should put your find method in if-statement for avoiding that error. HtH.
-------------------------------------------- Mehdi.
|
|
Reply By:
|
jlh42581
|
Reply Date:
|
12/1/2006 1:25:47 PM
|
You guys are adding an extra step.
Use the .length property and you dont need to subtract one, because it knows, that an array starts at zero.
for(var i=0;i<avail_options.length;i++){ if(avail_options.options[i].value == current_season){ avail_options.options[i].selected = true; } }
|