Can't get a checkbox value inside a gridviewrow controls - asp.net

i programatically populate a gridview with checkbox like this
Dim checkbox As New CheckBox()
checkbox.Checked = True
checkbox.ID = String.Format("chkChecked{0}", i)
MyRow.Cells(i).Controls.Add(checkbox)
later on i tried to access them with this
Dim chk As CheckBox = DirectCast( Row.FindControl(String.Format("chkChecked{0}", i)),CheckBox)
over the row but it does'nt work, any idea of what is going on? thank in advance

I use this code to access CheckBox control inside CellContentClick or CellClick events of the DataGridView :
Dim CheckBox As DataGridViewCheckBoxCell = CType(grdDataGridView.CurrentCell, DataGridViewCheckBoxCell)
If you want you can replace grdDataGridView.CurrentCell with grdDataGridView(columnIndex,rowIndex).
At the end I use EditedFormattedValue to check the value of the CheckBox.
If (CheckBox.EditedFormattedValue) Then
....
End If
Let me know if you need more help.
Regrads,
Boris

Related

Gridview FindControl inside SelectedIndexChanged event

I created a TemplateField in my ASP GridView, and now I want to write a small logic for a checkbox in the gridview. I am trying the FindControl code to no success, I've used these combinations...
Dim chkChosen As CheckBox =
'GridView1.Rows(e.RowIndex).FindControl("Checkbox1")
'DirectCast(GridView1.Rows(e.RowIndex).FindControl("Checkbox1"), CheckBox).Value
'chkChosen = (CheckBox)row.FindControl("Checkbox1")
I commented them as I've used a combination of those three to no success. They all give me the same error... "RowIndex is not a Member of SystemArg...". All this is under a SelectedIndexChanged protected sub.
This should work in your case:
Dim chkChosen As CheckBox = CType(GridView1.SelectedRow.FindControl("Checkbox1"), CheckBox)
You Can Get By Gridview Selected Row Index..lyk this
GridViewRow row = GridView1.Rows[GridView1.SelectedIndex];
cHeckbox chk=row.FindControl("chk");

Creating a New control of the same type as another control that is already declared

I'm using a custom template class to generate lines with my repeater control, i'd like to be able to specify the controls in this repeater dynamically from the code behind my aspx page.
In the code behind I've added controls to a list like this:
Dim lstControls As New List(Of Control)
lstControls.Add(New TextBox)
lstControls.Add(New Label)
lstControls.Add(New CheckBox)
lstControls.Add(New DropDownList)
lstControls.Add(New CheckBox)
Then i use this line to add the controls to my template
rptrSummary.ItemTemplate = New myTemplate(ListItemType.Item, , lstControls)
From the instantiateIn sub i'm doing something like this:
Dim ph As New PlaceHolder
For i = 0 To lstControls.Count - 1
ph.Controls.Add(lstControls(i))
Next
This doesn't work properly and following .databind() of my repeater control the controls i specify only appear on the final row. I think this is because i've only declared the controls as NEW once, so i only have one rows worth.
tldr?/ conclusion:
How can i generate new controls of the same type as controls from my list? Something like:
Dim newControl as new Control = type(lstControl(0))
(this obviously doesn't work)
I've found the answer, here are some examples in case anyone else is stuck (i may also change the title so it's more similar to likely search criteria):
dim egTextbox as new textbox
dim egLabel as new label
dim newObject1 as Object = Activator.CreateInstance(egTextbox.GetType)
dim newObject2 as Object = Activator.CreateInstance(egLabel.GetType)
newObject1 is now a textbox
newObject2 is now a label

How to select the Checked rows in GridviewRow and display it in next page in VB.net

I am trying to get the rows from the gridviewrow and display it in the detail view in next page. I am using Asp.net with VB
Simply iterate the collection and check the status. You can then put the selected rows into a session object or something else to pass on to the next page.
For Each Row As GridViewRow In MyGrid.Rows
Dim SelectCheck As CheckBox = DirectCast(Row.FindControl("chkSelectForDetail"), CheckBox)
If SelectCheck.Checked
' Add your logic here to save the data or passing on to the detail page.
End If
Next

showing images dependent on check box value

I am using a DataGrid, in VB, I have a check box. If it is checked I turn image.visible to true. This is done within the ItemDataBound method. How can I get it so when the check box status is changed, the DataGrid is rebound and uses the if statement to decide whether to show the images or not?
my code in ItemDataBound is:
Dim img As Image = e.Item.FindControl("pImage")
'Find the current Part
Dim qJob As qJob = CType(e.Item.DataItem, qJob)
Dim currentJob As New Job(qJob.JobCode)
img.ImageUrl = "~/ImageShow.aspx?JobID=" + JobID.ToString + "&Height=300&Width=300"
'Find the control and whether that part has an image, if there is an image, display the preview button
If currentJob.JObImage IsNot Nothing AndAlso checkShowImages.Checked = True Then
img.Visible = True
End If
When the checkbox status is changed how can I update the datagrid?
thanks

accessing HtmlTable inside a placeHolder

I'm working with a website written in aspx.net over vb.
I have a placeHolder, and I create a table of names inside this PlaceHolder, each name has an HtmlInputCheckBox next to it.
Im doing this in the aspx.vb file, when the page is uploading.
Then, when the user wants to send mail, he presses a button and than I need to access the checkboxes, and I'm having problems with this, the Sub doesn't know the checkBox object.
I would love for some help,
Thank you!
I understand that you're creating those checkboxes dynamically?
In such case, store them as global member of the class, most simple way is to have List of them:
List<HtmlInputCheckBox> arrCheckboxes = new List<HtmlInputCheckBox>();
...
...
HtmlInputCheckBox myCheckbox = new HtmlInputCheckBox();
arrCheckboxes.Add(myCheckbox);
...
This is C# but should be easy to translate to VB - anyhow having this, you can access the List and it should work.
Worst case as "last resort" you can simply iterate the whole Request.Form collection and look for Keys with name matching to the checkbox name.
Put this in the procedure...
Dim chkValue1 As New CheckBox
Dim chkValue2 As New CheckBox
'Find the Checkbox Controls in the PlaceHolder and cast them to the checkboxes we just made.
chkValue1 = CType(YourPlaceHolder.FindControl("Checkbox1ControlId"), CheckBox)
chkValue2 = CType(YourPlaceHolder.FindControl("Checkbox2ControlId"), CheckBox)
'Now you can do this...
Dim bolIsValue1Checked As Boolean = chkValue1.Checked

Resources