473,323 Members | 1,547 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

C#: SelectedIndex Returns -1

42
This function fires when the DataGrid roleList is updated by clicking 'Save' on a given row. Successful operation involves finding a DropDownList, getting its SelectedIndex, and printing it on a label:

Expand|Select|Wrap|Line Numbers
  1. public void roleList_Update(Object sender, DataGridCommandEventArgs e)
  2.         {
  3.             // VARIABLES --------------
  4.             DropDownList ddlCompany = (DropDownList) e.Item.FindControl("ddlCompanyEdit");
  5.  
  6.             string detailValue = "";
  7.             // ------------------------
  8.  
  9.             if (ddlCompany != null)
  10.                 Label1.Text = ddlCompany.SelectedIndex.ToString();
  11.             else
  12.                 Label1.Text = "crap!";
  13.  
  14.             roleList.EditItemIndex = -1;
  15.             DoBind();
  16.         }
  17.  
-1 is always printed regardless of whatever I chose.
Jan 11 '08 #1
5 3551
Plater
7,872 Expert 4TB
If you update the contents of the dropdown, the selected index always gets reset (to -1).
Are you updating the contents of the dropdown list before checking the index?
If this is a webapplication and you bind the data for the control in page_load(), it will get fired before the event does.
Jan 11 '08 #2
spamguy
42
If you update the contents of the dropdown, the selected index always gets reset (to -1).
Are you updating the contents of the dropdown list before checking the index?
If this is a webapplication and you bind the data for the control in page_load(), it will get fired before the event does.
OK, but if I remove data binding from the (!IsPostBack) block, every time I execute an action inside DataGrid (edit, delete, etc.), the data won't be updated. Clicking 'Edit' will produce duplicate rows, and deleted rows will still show up.
Jan 11 '08 #3
Plater
7,872 Expert 4TB
You could move the data binding on a post back to their respective eventhandler functions (edit update, whatever) so that your index is not lost.
Jan 11 '08 #4
spamguy
42
Hmmm, I haven't found the proper way to bind whilst keeping the DDL index intact. Occasional tests return values other than -1, but they're always the old, default values and not the newly selected values.

This configuration of binds achieves the latter. It's probably missing several binds to achieve 100% functionality (e.g., cancelling is missing one), but I'm just focusing on getting the DDL to respond:

Expand|Select|Wrap|Line Numbers
  1. private void Page_Load(object sender, System.EventArgs e)
  2. {
  3.         if (!IsPostBack)
  4.     {
  5.         }
  6. }
  7.  
  8. protected void roleList_On_Load(object sender, EventArgs e)
  9. {
  10.     DoBind();
  11. }
  12.  
  13. protected void roleList_OnItemDataBound(object sender, DataGridItemEventArgs e)
  14. {
  15. }
  16.  
  17. public void roleList_Edit(Object sender, DataGridCommandEventArgs e)
  18. {
  19.     roleList.EditItemIndex = e.Item.ItemIndex;
  20.  
  21.     DoBind();
  22. }
  23.  
  24. public void roleList_Cancel(Object sender, DataGridCommandEventArgs e)
  25. {
  26.     roleList.EditItemIndex = -1;
  27. }
  28.  
  29. public void roleList_Update(Object sender, DataGridCommandEventArgs e)
  30. {
  31.     // VARIABLES --------------
  32.     DropDownList ddlCompany = (DropDownList) e.Item.FindControl("ddlCompanyEdit");
  33.     // ------------------------
  34.  
  35.     if (ddlCompany != null)
  36.         Label1.Text = ddlCompany.SelectedIndex.ToString();
  37.         else
  38.         Label1.Text = "crap";
  39.  
  40.         roleList.EditItemIndex = -1;
  41. }
  42.  
  43. private void DoBind()
  44. {
  45.     roleListSQLAdapter.SelectCommand.Parameters["@ID"].Value = cid;
  46.     roleListSQLAdapter.Fill(dataSet11, "TableX");
  47.     roleList.DataBind();
  48. }
Jan 11 '08 #5
Plater
7,872 Expert 4TB
Hmm, I guess you will just have to play with it a bit more, maybe figure out what order events are fired in and then place code accordingly.
Jan 11 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Russell Campbell | last post by:
Posting again, since my first attempt never appeared: In the onClick method of a listbox, I am attempting to retrieve the selectedIndex property. The listbox is set up to allow multiple...
0
by: KavvY | last post by:
Hello, In the following code ddlJustified.DataSource = jTable; ddlJustified.DataTextField = "Just_Description"; ddlJustified.DataValueField = "Just_Description"; ddlJustified.SelectedIndex...
1
by: thomaz | last post by:
The Combobox SelectedIndex method doesn´t work in windows 98. If no one itens selected this method return ZERO (0) but the correct is (-1). According to the visual Studio help if not exist itens...
1
by: Nathan Sokalski | last post by:
I have a problem that is driving me crazy. I have a User Control composed of three DropDownLists that will be used to select a date. I have everything working except for one thing. When I select a...
9
by: Atchoum | last post by:
I have a combobox populated with objects from a class derived from FileInfo. When I try to assign a value to selected index, I get a "Argument length must be greater of equal to 0". What is this...
5
by: john.halet | last post by:
This line of code had been working with out issue, now its throwing errors. In my case I have three items in the ComboBox. If I try to change the selected index it throw the error. Earler in...
7
by: Jim Carlock | last post by:
Does a SELECT element (listbox) need to be inside a FORM element? The code I'm playing with: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"...
4
by: afetsch | last post by:
It's been quite awhile since I've written anything in JavaScript, so please pardon my ignorance if I'm missing the obvious. The following code works fine in Firefox, but not in IE (unless I include...
2
by: tuxedo | last post by:
At risk of asking a stupid question, I'd like to return the displayed text-string of the selectedIndex contained in an options menu, and so I tried the following one-liner: ...
4
by: in da club | last post by:
I have a datagrid and a dropdown in my page. I want to fill dropdown with my pagenumbers. When a user change the dropdown selected index i wnt to display the result on label1. Anytime i change the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.