Here's a picture of the gridview I'm working with: http://i.imgur.com/kFUTQ.png
Here's my issue:
I have a DropDownList (not shown in the picture) that has the same number of items as the Fraction DropDownList (shown in image). Each of the indices match with each other (ex: index 4 of the other field is the same as index 4 of the Fraction DropDownList), so when submitting the form, they have to have both of these match properly.
What I want to do is check and see if they do indeed match. I've tried:
this.gvRecords.Rows[0].Cells[2].Text
&
(((DropDownList)this.gvReserveRecords.Controls[0].Controls[0]
.FindControl("gvcbnFraction")).SelectedIndex
Neither have worked. They are just returning an empty string.
Here's some info that might help:
I am using ASP.NET Web Forms
The GridView is initially empty, but once the information is filled in, the 'Insert' LinkButton is clicked and the data is added to a database, and the GridView then calls DataBind().
I'm trying to compare the value of Fraction to a field outside of the GridView, if that matters at all.
Let me know if you need any other information!
You should be able to simply reference your DropDown control by its ID, in this case - gvcbnFraction. So, gvcbnFraction.SelectedIndex should give the integer index and gvcbnFraction.SelectedValue should give the string value.
If you aren't able to reference the drop down control in your code behind, then you are missing something that should be there. Possibly one of these:
Your designer.cs file doesn't include protected
global::System.Web.UI.WebControls.DropDownList gvcbnFraction;
Your .aspx file doesn't include CodeBehind="MySuperAwesomePage.aspx.cs"
Inherits="MyWebApp._Default" or something similar to that
Something else I just don't know about
If you are using Visual Studio, you can try dragging a control from the toolbox and see if you can reference it. Doing this will automatically update your designer.cs file, eliminating #1
Related
I'm in the process of creating a front-end Windows Forms application, which will have multiple uses. One of those uses will be to allow the end-user to manage the contents of a database table.
At the moment, my form contains a DataGridView object which uses a DataTable as its source. That datatable is populated from a stored procedure which selects every row from the table based on criteria specified by the end user. I want to put code into the DataGridView which will look at the row that's been selected by the user, and will populate a series of text boxes with the details from that row.
I want to avoid hard-coding a text box for each column in the DataGridView, since it's highly possible that in the future I might add columns to the underlying stored procedure. Therefore, my plan is that the first time the user selects a row, the application would dynamically create a text box for each column, size that text box appropriately (both height and width) and add the appropriate labels.
So far, the code I've written creates a brand-new DataTable, and adds columns to store the following information about each column in the DataGridView:
The actual name of the DataGridView column;
The name that I wish to give to the associated label;
The text that I wish to assign to the associated label;
The top and left positions of the associated label;
The name that I wish to give to the associated text box;
The value that I wish to display in the associated text box;
So far when the user initially clicks on a row, everything is working well: the text boxes get created in a vertical column, with the associated label to the left of each text box and the appropriate value displayed in each one. Each text box has its width dynamically set based on the contents of that text box so that everything can easily be seen.
The difficulty comes when the user changes from one row of the DataGridView to another. The code that runs to create each control looks like this:
if (pnlManageJobManagerOutcomes.Controls.Contains(txtCurrent))
{
pnlManageJobManagerOutcomes.Controls.Remove(txtCurrent);
}
if (!pnlManageJobManagerOutcomes.Controls.Contains(txtCurrent))
{
pnlManageJobManagerOutcomes.Controls.Add(txtCurrent);
}
When the code first runs, all seems to be fine. However, if it gets fired a second time (at which point it should remove and re-create the control) it doesn't seem to detect the prior existence of the control and simply creates a second copy of it in the form.
For information, the form I'm building has multiple uses which are dictated by a series of buttons. Therefore, I'm creating the controls for this particular use inside a Panel (pnlManageJobManagerOutcomes). I'm completely stumped as to why the code doesn't spot the existence of those controls during subsequent executions.
TIA
I've figured out a way to do it, which may seem to be clunky but which seems to work.
Since it doesn't appear that I can check to see whether the Panel I'm working with Contains the field name, I've written a method which will loop through the controls in that Panel and return a count of the number of instances of that control in the Panel's Controls array. If the method returns a 0, this means the Control doesn't exist in that Panel and I can go ahead and create it.
My Gridview contains many template fields which includes label box, image button, button, file upload controls. when I go edit row in gridview, and give new value by changing the old value, then I go to click the update button, there was no error will be thrown. When I put break point and debug line by line, it only takes old value not a new value.
The code was given below:
I am binding data in a DataTable to a GridView.
The result is a simple table with 5 columns and 3 rows.
What I want to achieve is:
If an user clicks on a cell in the GridView, the selected cell should become editable (with a textbox). In that textbox the user should have to possibility to enter new value and save it to the database.
What I've achieved so far is:
I followed this tutorial: http://www.dotnetlogix.com/Article/aspnet/80/How-to-make-GridView-Individual-Cells-Selectable.html
I managed to make the cells clickable, when clicking the cell the background color becomes red:
With that tutorial I also managed to get the ColumnIndex and RowIndex.
So I was able to do: GridView.SetEditRow(RowIndex);
Then my result became:
This is almost what I want, but not 100%. The SetEditRow edits the whole row, but I need to edit the clicked cell which is based on a row and column index.
Can anyone help me further please?
I've followed some other tutorials as well, but without result.
I tried this a few years ago and ran into a few issues, I don't recall all of them off the top of my head right now. What I ended up doing was using a repeater instead of a gridview to build a table. Since I'm writing the HTML I have more control over things. The table contained both a dig tag for the static value and a text input for the edit value. I used jquery to show and hide when needed. I then used ajax to post the changes back. This was a lot of work, but it ended up working out ok. Hope this helps.
When using javascript/ajax to repopulate the options in ComboBox B based on the selection in ComboBox A, selecting an item in B with an index > original set of indexes causes the error:
"'...' has a SelectedIndex which is invalid because it does not exist in the list of items.
Parameter name: value "
Evidently comboboxes still link back to their original datasource regardless of what javascript does, so there are issues with it thinking item 1 is x when it's really y, or item 2 doesnt exist. At least that's what I'm getting out of similar posts. What i'm not getting is a solution, tho...
If your ComboBox B was originally populated by databinding, your ViewState is going to reflect that, and your ComboBox B is going to be repopulated from the information in ViewState on postback. So your SelectedIndex of greater than the original number of items is going to confuse the heck out of it.
(The contents of ComboBoxes don't post back: just the selected item. So it's not going to know anything about what you did client-side.)
Possible options include: using an UpdatePanel to update your combo boxes without refreshing the whole page, or making your ComboBox B an ordinary HTML select, populating it exclusively from the client side (through Ajax?) and fetching its value from the Forms collection on the server side after postback.
I have a TextBox entry field where the user will enter a integer value. And then there is a "Create" button, which when clicked upon must generate a Table with 2 columns :
"Name" and "Email" being the column headers.
I want each row to have a textbox in each of these columns.
All of this has to happen after the button is clicked. I have discovered that if you dynamically add a control in ASP.NET(I am using C#) then the controls are lost during postback. And I don't know how to prevent that from happening.
Can somebody please give me some ideas regarding how to go about adding rows dynamically to a table (I tried using the asp.net Server side table control but ran into the "lost-during-postback" problem - can I try with something else like a gridview ? but afaik a GV will not work without data bound to it )
Point to note is that my table has textboxes for user entry and it is not for showing data ..rather it is for accepting data from the user which will be later used to persist details to the database.
Dynamic Controls
That's involved. Here's an interesting article on the issue of dynamic controls.
I think normally if you create dynamic controls then you're responsible for recreating them on postback; however the article contains a way to use the Init event if it's copacetic with your app.
Edit:
or Regular ASP.NET Controls
You can use data bound ASP.NET controls like: DataList, Repeater, GridView, etc. (You can put text boxes and all other kinds of controls into them for repeating in each row - or "item") and bind values to it. If you need to perform some processing on the user input first, then generate an internal Array, List, Dictionary, DataTable etc. (your choice) and bind that data as a data source to the ASP.NET control of choice.
aspnetControl.DataSource = myDataTable;
aspnetControl.DataBind();
or
aspnetControl.DataSourceId = "name_of_control";
There are various ways to assign a data source.
This way you won't run into the same problems as with dynamic control creation; however there is a lot to learn to facilitate this way too.