showing images dependent on check box value - asp.net

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

Related

How can I access dynamically created checkboxes and determine checked state?

I am trying to access 3 checkboxes on a page that were created dynamically from database info on PageLoad.
I can see these checkboxes in the web Page Source with ID's equal to T1, T2 and T3. On viewing the page the boxes are checked or unchecked appropriately according to database table values.
I am trying to determine the checked or unchecked states of these checkboxes and then writing it to the database on a button click. All of that is fine.
My problem is the actual "accessing" of those checkboxes/their associated ID's. I have extensively researched and tried everything yet nothing seems to work. I'm sure I'm missing some little nuance.
Here's my code (sorry the indenting is screwed up):
Dim RecCountT As Integer = TNotify.Rows.Count
For i = 0 to RecCountT - 1
Dim strSQLNotifyT as String = "UPDATE dbo._PinCodes SET TextOnOff = #TextOnOff WHERE EmployeeID = #EmployeeID"
Dim myCommand as New SqlCommand(strSQLNotifyT, Conn)
Dim CheckBoxID
CheckBoxID = "T" & i
If CheckBoxID.Checked = True Then
myCommand.Parameters.AddWithValue("#TextOnOff", "on")
ElseIf CheckBoxID.Checked = False Then
myCommand.Parameters.AddWithValue("#TextOnOff", "off")
End If
myCommand.Parameters.AddWithValue("#EmployeeID", TNotify.Rows(i)("EmployeeID").ToString())
Conn.Open()
myCommand.ExecuteNonQuery()
Conn.Close()
Next
Since I do not see the checkbox creation code and assuming the checkBox is created successully and added to the Controls collection, when you create the CheckBox, add:
yourCheckBox.ClientIDMode = ClientIDMode.Static
If this does not work, edit your post with more relevant code to replicate the issue.

Can't get a checkbox value inside a gridviewrow controls

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

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

telerik grid edit with validators seems overlapped

i have a telerik grid for asp.net ajax with AllowAutomaticxxxx properties set to true and EditMode="EditForms".
The mastertableview is for Divisions and detail table is for functions. Currently am able to insert new division and function and also edit division and function. i am using required field validator for both divisions and functions. now am facing a problem. the validator appears as overlapped when opening one edit function and one insert division.i mean when they are opened at the same time, and when i edited the function and clicked the update button, the validator for division name appears!!
1 more thing i have used ajaxmanager for the grid and i am not sure whether this is the issue.
i solved this issue by setting validationgroup for each edit and delete like this in the itemCreated event of the grid
If TypeOf e.Item Is Telerik.Web.UI.GridEditableItem AndAlso e.Item.IsInEditMode Then
If "Divisions".Equals(e.Item.OwnerTableView.Name) Then
Dim rvDivisionName = DirectCast(e.Item.FindControl("rvDivisionName"), RequiredFieldValidator)
If TypeOf e.Item Is Telerik.Web.UI.GridEditFormInsertItem Then
rvDivisionName.ValidationGroup = "addDivisionRowValidation"
TryCast(TryCast(e.Item, Telerik.Web.UI.GridEditFormItem).FindControl("PerformInsertButton"), LinkButton).ValidationGroup = "addDivisionRowValidation"
Else
rvDivisionName.ValidationGroup = "editDivisionRowValidation"
TryCast(TryCast(e.Item, Telerik.Web.UI.GridEditFormItem).FindControl("UpdateButton"), LinkButton).ValidationGroup = "editDivisionRowValidation"
End If
ElseIf "Functions".Equals(e.Item.OwnerTableView.Name) Then
Dim rvFunctionName = DirectCast(e.Item.FindControl("rvFunctionName"), RequiredFieldValidator)
Dim rvFunctionRoleName = DirectCast(e.Item.FindControl("rvFunctionRoleName"), RequiredFieldValidator)
If TypeOf e.Item Is Telerik.Web.UI.GridEditFormInsertItem Then
rvFunctionName.ValidationGroup = "addFunctionRowValidation"
rvFunctionRoleName.ValidationGroup = "addFunctionRowValidation"
TryCast(TryCast(e.Item, Telerik.Web.UI.GridEditFormItem).FindControl("PerformInsertButton"), LinkButton).ValidationGroup = "addFunctionRowValidation"
Else
rvFunctionName.ValidationGroup = "editFunctionRowValidation"
rvFunctionRoleName.ValidationGroup = "editFunctionRowValidation"
TryCast(TryCast(e.Item, Telerik.Web.UI.GridEditFormItem).FindControl("UpdateButton"), LinkButton).ValidationGroup = "editFunctionRowValidation"
End If
End If
End If

Programatically created ASP.NET TextBox retains Text value after PostBack even if Control is cleared

I have a drop down menu, and based on which item is selected, I call a web service and then dynamically create some text boxes.
The first time I drop down the menu and select an item, it works perfectly, and the text boxes are created and populated dynamically. However, the next time I drop down the menu (after the first postback), and select something different... after the second postback, the original values remain in the textboxes.
I am clearing all of the text boxes out of the placeholder, then re-creating them, and then setting a NEW value, how can they retain the OLD values... especially if I controls.clear them from the page?
Note: The second time they are being created, the textbox IDs DO end up being the same. Could that have something to do with it? This duplicate ID functionality will need to be supported.
My code, called from Page_Load, is as follows: (edited to add more code)
Private Sub RefreshEntity()
Dim XmlRecords As New XmlDocument
Dim XmlRecordsNode As XmlNode
Dim EntityType As String = EntityTypes.SelectedValue
Dim Entity As String = RecordValue.Value
Dim FieldName As String
Dim FieldValue As String
FieldPlaceHolder.Controls.Clear()
If RecordList.SelectedValue <> "Select..." Then
Try
XmlRecordsNode = LoginInfo.SharePointConnectWebService.GetMetaData(LoginInfo.WSUser, LoginInfo.WSPass, _
EntityType, Entity)
XmlRecords.LoadXml(XmlRecordsNode.OuterXml)
Catch ex As Exception
ConfirmLabel.Text = "<b>Error:</b><br>" & ex.Message.ToString
Return
End Try
Else
SetProperties.Visible = False
Return
End If
For Each OneNode As XmlNode In XmlRecords.SelectNodes("Fields").Item(0).ChildNodes
FieldName = OneNode.Name
FieldValue = OneNode.InnerText
Dim newLabel As Label = New Label()
newLabel.Text = FieldName & ": "
Dim newTextBox As TextBox = New TextBox()
newTextBox.ID = "Field-" & FieldName
newTextBox.Text = FieldValue
Dim newLine As Label = New Label()
newLine.Text = "<br><br>"
FieldPlaceHolder.Controls.Add(newLabel)
FieldPlaceHolder.Controls.Add(newTextBox)
FieldPlaceHolder.Controls.Add(newLine)
Next
SetProperties.Visible = True
End Sub
And the RecordValue.Value is a hidden field that gets populated in every Page_Load:
RecordValue.Value = RecordList.SelectedValue
Where RecordList is my DropDown menu.
This is likely due to ViewState or the Posted values clobbering your values.
Once a control is dynamically added to the controls collection it needs to catch up with all the page life cycle events that have already fired. In the case of a post back this means that the ViewState and/or the posted form value will clobber the .text property on the TextBox based on the order you're adding the dynamic controls to the controls collection and setting the .text property.
To fix this you can disable ViewState by setting the .EnableViewState property to false on the dynamically generate controls also add the controls to the controls collection before you set any properties on them.
For Each OneNode As XmlNode In XmlRecords.SelectNodes("Fields").Item(0).ChildNodes
FieldName = OneNode.Name
FieldValue = OneNode.InnerText
Dim newLabel As Label = New Label()
Dim newTextBox As TextBox = New TextBox()
Dim newLine As Label = New Label()
newTextBox.ID = "Field-" & FieldName
newLabel.EnableViewState = False
newTextBox.EnableViewState = False
newLine.EnableViewState = False
FieldPlaceHolder.Controls.Add(newLabel)
FieldPlaceHolder.Controls.Add(newTextBox)
FieldPlaceHolder.Controls.Add(newLine)
newLabel.Text = FieldName & ": "
newTextBox.Text = FieldValue
newLine.Text = "<br><br>"
Next
You're not storing the value in a Session variable and then putting it back into the text box later in your code?

Resources