Make reference to a Button in a repeater? - asp.net

I have a form with a button control in a repeater, and I am trying to reference it in my code so that I can disable and enable it where appropriate but it isn't recognized.
I tried something that looked like this:
Sub btnClickPrev(ByVal sender As Object, ByVal e As EventArgs)
'Get the reference of the clicked button.
Dim button As Button = CType(sender, Button)
'Get the command argument
Dim commandArgument As String = button.CommandArgument
'Get the Repeater Item reference
Dim item As RepeaterItem = CType(button.NamingContainer, RepeaterItem)
'Get the repeater item index
Dim index As Integer = item.ItemIndex
item.Enabled = false
End Sub
But item.enabled didn't work like btnPrev.enabled=false would
And this is what I have in my button form control:
asp:Button ID="btnPrev" Text="Previous" OnClick="btnClickPrev" CommandArgument = '1' runat="server"
Any help would be appreciated. thank you

Problem is, RepeaterItem doesn't have any Enable property that you are trying to change. Instead, loop through all controls inside RepeaterItem and disable them like this:
Sub btnClickPrev(ByVal sender As Object, ByVal e As EventArgs)
'Get the reference of the clicked button.
Dim button As Button = CType(sender, Button)
'Get the Repeater Item reference
Dim item As RepeaterItem = CType(button.NamingContainer, RepeaterItem)
For Each con In item.Controls
'Disable button
If TypeOf con Is Button Then
CType(con, Button).Enabled = False
End If
'Disable other controls if you want
If TypeOf con Is CheckBox Then
CType(con, CheckBox).Enabled = False
End If
Next
'item.Enabled = False 'Wouldn't work
End Sub

Related

OnClick function on dynamic LinkButton?

I'm creating dynamic LinkButton where I assign to it dynamically an ID now I would be able to write an onClick function where I get that ID but after trying to make something like that I had no result:
table.Append("<td><asp:LinkButton runat=""server"" class=""edit btn btn-sm btn-default"" ID=" & reader.GetString("id") & " OnClient=""Delete_User""><i class=""fa fa-trash-o"" aria-hidden=""true""></asp:LinkButton></i></a>")
While here is VB.NET code
Sub Delete_User(sender As Object, e As EventArgs)
Dim clickedBtn As LinkButton = CType(sender, LinkButton)
MsgBox(clickedBtn.ID)
End Sub
The LinkButton is a server control and you cannot render it with same way as you render plain HTML tags from code-behind, it will not work as expected. You need to provide HtmlTableCell instance and bind LinkButton control from there, assumed table is defined as HtmlTable:
' this is just a sample event
Protected Sub SomeEventHandler(sender As Object, e As EventArgs)
Dim table As HtmlTable = New HtmlTable()
Dim row As HtmlTableRow = New HtmlTableRow()
' set the data reader from database here
Dim cell As HtmlTableCell = New HtmlTableCell()
Dim linkbtn As LinkButton = New LinkButton()
linkbtn.ID = reader.GetString("id") ' setting control ID
linkbtn.Text = "Delete User" ' setting link text
linkbtn.CssClass = "edit btn btn-sm btn-default"
AddHandler lnkbutton.Click, AddressOf Delete_User ' assign event handler
' add the control to table cell ('td' element)
cell.Controls.Add(linkbtn)
row.Cells.Add(cell)
table.Rows.Add(row)
' other stuff
End Sub
Note that you should provide server-side click event with Click property which associated with OnClick event, not OnClient nor OnClientClick.
Additional note:
MsgBox cannot be used in ASP.NET because it belongs to WinForms, you need to provide JS alert() function to show message box with RegisterStartupScript or RegisterClientScriptBlock:
Sub Delete_User(sender As Object, e As EventArgs)
Dim clickedBtn As LinkButton = CType(sender, LinkButton)
Dim message As String = "alert('" & clickedBtn.ID & "');"
Page.ClientScript.RegisterClientScriptBlock(Me.[GetType](), "MsgBox", message, True)
End Sub
Related issues:
How to add linkbutton control to table cell dynamically
Creating a link button programmatically
How to add dynamically created buttons to a dynamically created table?

GridView SelectedIndexChanged - return item that was clicked

I have an ASP.NET DataGrid that returns three clickable columns at the end. When running SelectedIndexChanged how can I return the value of the item that was clicked?
Thanks
Private Sub DG_SelectedIndexChanged(sender As Object, e As EventArgs)
Try
Dim DG As GridView = CType(sender, GridView)
Dim vID As Integer = DG.SelectedRow.Cells(0).Text
'Need to determine which item was clicked here
Catch ex As Exception
End Try
End Sub
In the end I found the only way was to add an onclick event to the LinkButton
AG_Link.Text = "View Agenda"
AG_Link.Attributes.Add("onclick", "returnDocumentType('Agenda');")
Add some JavaScript to update a hidden textbox, then pick up the result
Private Sub DG_SelectedIndexChanged(sender As Object, e As EventArgs)
Try
Dim DG As GridView = CType(sender, GridView)
Dim vID As Integer = DG.SelectedRow.Cells(0).Text
Dim DocumentType As String = DocumentTypeTB.Text
Catch ex As Exception
End Try
End Sub
Bit of a long way round, but it works :-)

Access Individual Row Template in GridView

I have a ListBox (named ListBoxProperty) and button (btnDeselectAll) in a TemplateField on a gridview control that shows on every row of the gridview. The code shown below is the code behind the button control in the TemplateField. This code works on every ListBox on every row in the gridview. I would like it to only affect the ListBox in the gridview row from which it was clicked. I've tried a few things with failure so I didn't include my failings - just the portion that almost gives me what I want. Couldn't include a pic alas my reputation is weak.
Protected Sub btnDeselectAll_Click(sender As Object, e As System.EventArgs)
For Each Row As GridViewRow In GridView1.Rows
Dim myListBox As ListBox = _
CType(Row.FindControl("ListBoxProperty"), ListBox)
For Each selectedItem As ListItem In myListBox.Items
selectedItem.Selected = False
Next
Next
End Sub
The NamingContainer of the button is the GridViewRow, there you are.
Protected Sub btnDeselectAll_Click(sender As Object, e As System.EventArgs)
Dim btn = DirectCast(sender, Button)
Dim row = DirectCast(btn.NamingContainer, GridViewRow)
Dim myListBox = DirectCast(row.FindControl("ListBoxProperty"), ListBox)
For Each selectedItem As ListItem In myListBox.Items
selectedItem.Selected = False
Next
End Sub

Programmatically update a bound checkbox in a GridView using an asp:ButtonField in ASP.NET 2005

The GridView is bound to an Access Table, but I want to update the checkbox using the asp:ButtonField. The best I've been able to do is replace the checkbox with a string value. Below is the code I have now:
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "check" Then
'Enable Editing on the GridView
GridView1.EditIndex = GridView1.SelectedIndex
'Assign value to the row selected
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
'Assign selected gridview row
Dim selectedRow As GridViewRow = GridView1.Rows(index)
'Assign checkbox cell as table cell
Dim selectedCell As TableCell = selectedRow.Cells(0)
'Assign a value to the checkbox, **but instead is replacing the checkbox**
selectedCell.Text = "True"
End If
End Sub
I think you need to use .FindControl().
The GridViewCell is not the same thing as a CheckBox control inside it. You code should probably look more like:
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
If e.CommandName = "check" Then
'Enable Editing on the GridView
GridView1.EditIndex = GridView1.SelectedIndex
'Assign value to the row selected
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
'Assign selected gridview row
Dim checkBox As CheckBox = GridView1.Rows(index).FindControl("checkBoxIdGoesHere")
checkBox.Checked = true 'or false or whatever based on your command.
End If
End Sub
I've had my own share of problems accessing controls from within GridViews. I solved something similar about a month ago. Assuming that your checkbox control is the only thing in that cell, you could try something like this:
'Assign a value to the checkbox
CType(GridView1.Rows(index).Cells(0).Controls(0), CheckBox).Checked = True
Try that and see if it works. If not, post back and we can take another look.

whats wrong in this code?

whats wrong in this code
i have a Imagebutton2 in gridview whose commandname is xxx and i have added modalpopup extendar panel2 with ImageButton2 i want when image button 2 will be clicked then the modalpopup will display and retrieve the values from selected gridview row to the literal3 control of panel 1 which acts as a modalpopup control for gridview ?
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Dim myrow As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
If e.CommandName = "xxx" Then
Dim lab5 As Label = DirectCast(myrow.FindControl("Label5"), Label)
Dim lit3 As Literal = Me.Panel2.FindControl("Literal3")
lit3.Text = lab5.Text
End If
End Sub
Many things can fail:
e.CommandArgument may not be an integer
e.CommandSource may not be of Control type
the NamingContainer may not be of GridViewRow type
myRow can be null, at least when the code is ran
the "Label5" control may not be found, or may not be of Label type, when the code is ran
the "Literal3" control may not be found (null ref) or may not be of Literal type, when the code is ran
...

Resources