I am kind of new to C# and Asp.Net, so this question might sound repetitive but I am not able to find a solution for this particular problem.
I have two buttons on my HTML page and a single class in .cs file. On one of the button clicks, I create a table programmatically (dynamically).
The table contains some checkboxes which are also created dynamically.
Table creation is one of the last tasks that I perform. Before that, I read several files and extract data from them to create the table. After the table is drawn, the user can select one or more checkboxes.
Now, how on second button click, can I know that which of the checkboxes were checked before the page reload? Currently I have made all these checkboxes member variables of the the only class that I have in the .cs file.
I tried adding checkbox event handler through C# code. But the handler is not getting called when the checkbox is checked. I don't want to set the 'autopostback' property of the checkbox to true since if thats set true, the page reloads after checking one of the checkboxes. User should be able to select multiple checkboxes.
Add your checkboxes dynamically and set a unique name for each checkbox. Checkboxes are only posted back to the server if the checkbox is checked, so you can test to see if it is checked by checking Request.Form to see if the name exists. For example, lets say you named your check boxes chk_[0-9] (i.e. chk_0, chk_1 etc till 9), you could check if they ticked by doing:
for(int i=0; i < 10; i++)
{
string chk_name = "chk_" + i.ToString();
if (Request.Form[chk_name] != null)
{
//checkbox is checked
}
else
{
//checkbox is not checked
}
}
Related
I'm loading a list of databases from my app.config, and I want to check my datagridview checkboxes on these databases. When I debug the method it assigns a true value, but it doesn't change on UI and the value of checkbox is still false.
The method runs before showing my Windows Forms, after InitializeComponent(). If I click on the button, that calls the same Method after Windows Forms is loaded it works as a charm. Not automatically though.
I'm doing this because I want to recreate my datagridview with all checks every time I launch the application, and it's been really bugging me. I tried refreshing and updating dataGridView, commiting updates, ending updates before I assign a value, I also tried setting property "TrueValue" on checkbox column to true, but it didn't help.
private void CheckDatabasesFromConfig()
{
for (int i = 0; i < SQLDatabases.RowCount; i++)
{
if (listOfDatabasesToBackup.Contains(SQLDatabases.Rows[i].Cells["Database Name"].Value))
{
SQLDatabases.Rows[i].Cells["Backup"].Value = true;
}
}
}
I want to open the application and see checkboxes checked according to this list on my datagridview, without clicking anything.
"Database Name" in Cells["Database Name"] is not a valid identifier so "if" expression is always false.
correct your column name defined in dataGridView.
I managed to solve the problem - I'm sorry for misinformation. I had the datagridview in second page of the tabcontrol, and since I didn't open it, the controls didn't load.
It helped to perform a simple tabPage2.show() before calling my method.
I have a Winform App where I want a user to be able to enter some stock corrections against a list of products which will in turn create a database record for each of those corrections.
Using a combobox, I got it so each item would have to be selected in turn. It works great, but is not very user friendly. Ideally, I'd like a list of products with an empty column to enter the corrections, click submit and ya done.
I looked at using a datagrdiview. I can generate the list of products, but I've no idea on how to create the 'entry column'.
Any suggestions on the path to take?
Cheers,
Numb
EDIT
Here is a mock up of what I would like to do to make it as clear as possible without showing code...
If you want use a ”Submit” button to save a new stock correction, then
approach will be:
-Create a datagridview with products names and empty cell for stock correction
-After ”Submit” button was clicked read ”Correction” columns values, if value exists update database…
Here how you can do it:
Create a datagridview in designer dgvProductCorrections,
then create with designer a two columns in this datagridview
dgvProductCorrection_Product and dgvProductCorrection_Correction.
Put this(or your) columns names in Column .Name property
Then, I assume that you have a class Product where exists a property .Name,
you will need to put a name of this property(in my example will be Name) in .DataProperty of column
Above can be done with designer or in code(Constructor)
Adding a list products List<Product> lstProducts; in datagridview will be as:
dgvProductCorrections.DataSource = lstProducts; //(this may be in Form_Load)
Rememeber before adding a list set a datagridview property AutoGenerateColumns to False
dgvProductCorrections.AutoGenerateColumns = false;
in Button_Click event handler of your Submit button put a code where you loopin through a all rows and reading a correction values. After you can update a database with those values
//Code in Button_Click
{
foreach(DataGridVewRow dgvr in dgvProductCorrections.Rows)
{
Decimal fCorrection;
//Check if value exists and it can be used. Add own other checks
if(dgvr.Cells(this. dgvProductCorrection_Correction.Name).Value != null && Decimal.TryParse(dgvr.Cells(this.dgvProductCorrection_Correction.Name).Value.ToString(), fCorrection) = True)
{
//Here you can put a update code, or save a correction in list and then update all by one update call
}
}
}
suppose I have 5 check boxes. Based on number of check box checked I have to add image and hyper-link to images. (i.e if I have checked 2 check box I have to load only 2 image and link to those 2 loaded images, in-case of 3 check box checked, 3 images needs to be loaded and link to those 3 loaded images).
Is there any possible way that I can do that in asp.net using c#?
If you want to display images or hide right after you select or deselect a check box you need to set the AutoPostBack attribute of the checkboxes controls (or checkboxlist control) to true.
And in the respective OnSelectedIndexChanged event (or the Button1_Click event if that's the case) you simply change the images Visible property to true or false depeding on the options selected.
Also, if you want the user experience to be smoother you may embedd your images markup inside an UpdatePanel and call:
UpdatePanel1.Update();
Got it?
It depends on whether the CheckBoxes are in a CheckBoxList, but assuming they're not put the CheckBoxes in some type of parent container, i.e. a PlaceHolder or a Panel, and you can retrieve the CheckBoxes like this:
foreach (CheckBox chk in PlaceHolder1.Controls.OfType<CheckBox>())
{
if (chk.Checked)
{
//create/load image and hyperlink
}
}
I am dynamically adding a textbox to certain rows (one column only) of a gridview. I add the controls with this insdie of a test condition (works fine):
TextBox txtASIN = new TextBox();
txtASIN.ID = "TxtASIN" + e.Row.RowIndex;
e.Row.Cells[4].Controls.Add(txtASIN);
int i = e.Row.Cells[4].Controls.Count; //TEST: This returns 1 correctly
I want the user to be able to enter values into one or more of these textboxes and then update the database with a single button click (not one click for each row). The problem I'm having is how to access those values on a button click event. I did a simple test this way to try to see the value in the second row but get null in temp1 (I am certain there is a value entered in that textbox):
protected void btnUpdate1_Click(object sender, EventArgs e)
{
TextBox temp = (TextBox)GridView2.Rows[1].FindControl("txt1");
string temp1 = temp.Text;
int i = GridView2.Row.Cells[4].Controls.Count; //TEST: This returns 0 incorrectly }
Once I can make this work, I can iterate through the rows and do what I need to do with the values. I don't know if the text entered in the textbox is actually readable without a postback but I'm otherwise stumped. Open to better suggestions on how to do this.
Thanks.
EDIT: Here is where I am now. I can see the textboxes in my column fine. I put a break in on a button that attempts to read them and this is what I'm seeing. If I check GridView2.Rows[0].Controls.Count, I get 8, which is the correect number of columns. If I check GridVeiw2.Rows[0].Cells[4].Controls.Count, I get 0, which is wrong because the textbox is there. I can get a Count of 1 right after I dynamically create it but not when I perform a subsequent button click.
Can anyone explain this? I feel if I can get past this holdup, I can get the rest done.
Thanks again.
You need to assign an ID to the TextBox controls and then access them by that ID in FindControl(). Also, make sure you're adding the controls in the Page's Init() method of the life-cycle. That way it gets added to ViewState.
TextBox txt1 = new TextBox();
txt1.ID = "txt1";
e.Row.Cells[4].Controls.Add(txt1);
EDIT: I just remembered another possible solution. Instead of programatically creating the TextBox controls in the code-behind just create a TemplateField in the GridView.
Add Textbox TemplateField Column To GridView Programmatically
I would try a different approach, putting the text box in the html markup of the page and then control the visible or readonly property of it on the ItemDataBound event. That way, the control will always be there and you don't have to worry about the lifecycle stuff.
I really didnt know what to put to the title so sorry about that. I have 2 colums where i list files from 2 folders. Now what i would like to do is to give a user ability to rearrange the files by clicking on one file in one column and then on the other file in other column. The application merges these two files together.
Just onother option would be just use datagrid and its drag and drop functionality (one column is static and one rearrangable with drag and drop or something) but doing it this way isnt really what i would like..
So, all options are welcome..
EDIT:
Using WinForms, it doesnt have to be gridview, just i couldnt think of anything else..
I would just create two DataGridView's (or ListView if you prefer), the first listing the files in the folder A and the second listing the files in the folder B.
Then allow to select just one row at a time in both grids (MultiSelect = false, SelectionMode = FullRowSelect) and add a button called "Merge Selected", that simply merges the file selected in the first grid with the one selected in the second grid.
Use 2 list boxes linked to each folder so that the use can scroll up/down to select the file they need
Have button "Merge Files" which gets enabled when an item from both listboxes are selected.
When user clicks on "Merge Files" have a confirmation box to make sure its not clicked my mistake.
I'll assume you're talking about DataGridView, as DataGrid has been deprecated.
In the designer, make sure SelectionMode on the DataGridView is set to CellSelect. Then in properties --> events, double-click the SelectionChanged event to create a new method which handles that event.
Add this code to the method:
private DataGridViewCell _lastCellSelected = null;
private void dataGridView_SelectionChanged(object sender, EventArgs e)
{
if(dataGridView.SelectedCells.Count == 0)
{
_lastCellSelected = null;
return;
}
DataGridViewCell selectedCell = dataGridView.SelectedCells[0];
if(_lastCellSelected == null || selectedCell.ColumnIndex == _lastCellSelected.ColumnIndex)
{
//User clicked first cell
_lastCellSelected = selectedCell;
}
else
{
//User has clicked two cells from different columns
string filename1 = _lastCellSelected.Value;
string filename2 = selectedCell.Value;
//TODO: "Merge" files here
_lastCellSelected = null;
}
}