I have a button within a formview control on my page.
Because the button is contained within the formview, my code-behind can't see it.
So I did this:
Dim btnSave As Button = CType(fvCourse.FindControl("btnSave"), Button)
And then I added an event handler like this:
AddHandler btnSave.Click, AddressOf btnSave_Click
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Response.write("hey!")
End Sub
The problem is, I don't think it's working because I never see the "hey!" on my page.
Am I missing something?
Thanks
I don't know about missing something, but I reckon you could do it a simpler way since you're using VB. Give your button a command name and command argument first:
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
CommandArgument="1" CommandName="yes" />
These can be anything - typically you use the command name to determine which button a user clicked on, and the command argument to show the record id.
In your code-behind, attach a macro to the ItemCommand event of the FormView (which fires when something happens within it):
Protected Sub FormView1_ItemCommand(sender As Object, e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles FormView1.ItemCommand
Select Case e.CommandName.ToLower
Case "yes"
'test
Label2.Text = "You chose " & e.CommandArgument.ToString
End Select
End Sub Protected Sub FormView1_ItemCommand(sender As Object, e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles FormView1.ItemCommand
Select Case e.CommandName.ToLower
Case "yes"
'test
Label2.Text = "You chose " & e.CommandArgument.ToString
End Select
End Sub
And in VB, that's all you need to do!
You should use the ItemCreated event of the FormView for such things. If the Button is in the ItemTemplate you need to check for the FormViewMode.ReadOnly, for EditItemTemplate you need to use Edit:
Private Sub fvCourse_ItemCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles fvCourse.ItemCreated
Select Case fvCourse.CurrentMode
Case FormViewMode.Edit
Dim btnSave As Button = DirectCast(fvCourse.FindControl("btnSave"), Button)
AddHandler btnSave.Click, AddressOf btnSave_Click
End Select
End Sub
Related
Wasn't able to find anything about this situation:
I have two RadDatePicker inside RadGrid for start and end date change
<telerik:RadDatePicker ID="rdpStartDate" Skin="Library" EnableEmbeddedSkins="false" CommandName="StartDateChange" runat="server" />
By itself, they work fine, but now I have a situation when I need to call method when their value has been changed (CommandName was added for this)
I know how to do this outside RadGrid, basically:
Protected Sub rdpStartDateChanged(ByVal sender As Object, ByVal e As Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs) Handles rdpStartDate.SelectedDateChanged
...
...
End Sub
But I wasn't able to do this inside RadGrid, because nothings seems to trigger it.
I tried to catch my command with this (works for buttons at least):
Protected Sub rgLibraryItemCommand(ByVal sender As Object, ByVal e As GridCommandEventArgs) Handles rgLibrary.ItemCommand
But, no, it doesn't see CommandName="StartDateChange"
What I need to do to be able to catch those Date change events if RadDatePicker
is placed inside RadGrid?
You need to identify the control inside the grid which triggers the action. I'm not sure which is the way for Rad to do that but it should be similar this:
Private Sub DataGridView1_ButtonClick(sender As DataGridView, e As DataGridViewCellEventArgs) _
Handles DataGridView1.CellButtonClick
'TODO - Button Clicked - Execute Code Here
End Sub
So you need to find the events for the DataGridViewRow for Rad and substitute them with the ones cell clicking events. This example should get you started.
Subscribing to an event on a control inside a RadGrid does not work the same way, because there could be multiple copies of the control or even none at all, depending on how many records are in the Data Source. Therefore, in order to subscribe to events on these controls, you have to do it manually after the data is bound, either in the ItemCreated event or ItemDataBound event.
Protected Sub rgLibraryItemCreated(ByVal sender as Object, ByVal e As GridItemEventArgs) Handles rgLibrary.ItemCreated
If TypeOf e.Item Is GridDataItem Then
Dim item As GridDataItem = e.Item
Dim rdpStartDate As RadDatePicker = item.FindControl("rdpStartDate")
AddHandler rdpStartDate.SelectedDateChanged, AddressOf rdpStartDateChanged
End If
End Sub
I have a ImageButton instance variable in my custom control I am making. I am a little shaky on how to simply set up it's event handler for it's click event. Coming from C#, I am a little confused as this is in VB.NET.
Snip:
Dim ctrlCloseImage As ImageButton = New ImageButton()
With ctrlCloseImage
.ID = "imgClose"
.ImageUrl = "~/Web/Images/close.gif"
.ToolTip = "Close"
.CssClass = "popup_close_img"
'This is where I left off
AddHandler ctrlCloseImage.Click, AddressOf testSub(ctrlCloseImage, ??)
End With
Me._ctrlCloseImage = ctrlCloseImage
And Then:
Protected Sub testSub(ByVal Sender As Object, ByVal e As ImageClickEventArgs)
Me.Page.Response.Write("Yay, you clicked the image button!")
End Sub
Thanks for any help, and if I am missing anything important code-snip wise please let me know. Again, I am wanting this to fire server side, I am not interested in firing off a javascript function here.
You just have to add the delegate:
AddHandler ctrlCloseImage.Click, AddressOf testSub
You cannot pass arguments since the event is not triggered here.
However, if you don't need to create a control dynamically (normally that's unnecessary) you could either register the event on the aspx or with the Handles clause(as opposed to C#):
Protected Sub testSub(ByVal Sender As Object, ByVal e As ImageClickEventArgs) _
Handles ctrlCloseImage.Click
Me.Page.Response.Write("Yay, you clicked the image button!")
End Sub
You need to assign the click event a handler :
AddHandler ctrlCloseImage.Click, AddressOf testSub
This will assign the Click control the handler of address testSub
This should do the trick (just put the name of the method afyer AddressOf):
AddHandler ctrlCloseImage.Click, AddressOf testSub
It have a ListView with LayoutTemplate, ItemTemplate and EditTemplate. The List view only ever shows one item.
The ItemTemplate as an EditButton with CommandName = 'Edit' which when clicked triggers...
Protected Sub ListView1_ItemEditing(ByVal sender As Object, ByVal e As ListViewEditEventArgs) Handles ListView1.ItemEditing
ListView1.EditIndex = e.NewEditIndex
'Create SQL and load result in datatable and bind to listview
LoadData(Session("SID"))
End Sub
The EditTemplate has an UpdateButton with CommandName 'Update' which when clicked triggers...
Protected Sub ListView1_Command(ByVal sender As Object, ByVal e As ListViewCommandEventArgs) Handles ListView1.ItemCommand
If e.CommandName = "Update" Then
'Use StringBuilder to build up an UPDATE TSql script
SqlStr = sb.ToString
'Execute Update
ExecuteSQLScript(SqlStr)
End If
The above works great. My question is how do I now get back to ItemTemplate View. I know that I must use the ItemUpdating method similar to the way the above ItemEditing method worked, but I've ran out of ideas... Any help appreciated. All I have at the moment is...
Protected Sub ListView1_Updating(ByVal sender As Object, ByVal e As ListViewUpdateEventArgs) Handles ListView1.ItemUpdating
'ListView1.ItemIndex = e.ItemIndex 'This does not work
LoadData(Session("SID"))
End Sub
Thanks
Set the EditIndex to -1.
ListView1.EditIndex = -1
Is there a Panel or any container with CommandName, CommandArguments instead of using Buttons (LinkButton, ImageButton, ...)?
I want to add a column in a GridView to make selection for the row, the whole cell's rectangle instead of a Select link.
You can make (almost) anything implement CommandName and CommandArguments by implementing the IButtonControl interface. You will probably also want to implement the IPostBackEventHandler interface.
This article covers in detail exactly what you are asking about, generally: making a Panel into a command control. It's not entirely trivial.
But making a table row clickable is a lot easier, though, esp. using jQuery. See this article. You just bind an event to the row, and go from there. In this example, they're redirecting to the url for a link in the row on a click event. You could just as easily do something else like call __doPostBack to cause an async postback and run arbitrary server code, e.g.
Here is another way how you can make a GridView-Cell as Sort-Column(see in RowCreated, the rest is sample-data):
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
If Not IsPostBack Then
BindGridView()
End If
End Sub
Private Sub BindGridView()
Dim source As New List(Of String)(New String() {"1. row", "2. row", "3. row", "4. row", "5. row"})
Me.GridView1.DataSource = source
Me.GridView1.DataBind()
End Sub
Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
Dim cell As New TableCell
If e.Row.RowType = DataControlRowType.DataRow Then
cell.Text = "select"
cell.ToolTip = "click to select row"
cell.Attributes("onmouseover") = "this.style.cursor='pointer';"
cell.Attributes("onclick") = ClientScript.GetPostBackClientHyperlink(DirectCast(sender, GridView), "Select$" & e.Row.RowIndex)
End If
e.Row.Cells.Add(cell)
End Sub
Edit: if you get a "Invalid postback or callback argument"-Exception from ASP.Net, you can either set EnableEventValidation="False" or add following to your page's codebehind:
Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
For Each row As GridViewRow In GridView1.Rows
If row.RowType = DataControlRowType.DataRow Then
ClientScript.RegisterForEventValidation(GridView1.UniqueID, "Select$" & row.RowIndex)
End If
Next
MyBase.Render(writer)
End Sub
If you need C#, copy all into: http://www.developerfusion.com/tools/convert/vb-to-csharp/
I have a checkbox and a panel inside of a FormView control, and I need to access them from the code behind in order to use the checkbox to determine whether or not the panel is visible. This is the code that I originally used, but since I put the controls inside of the FormView, it no longer works.
Protected Sub checkGenEd_CheckedChanged(ByVal sender As Object, _
ByVal e As System.EventArgs)
If checkGenEd.Checked = True Then
panelOutcome.Visible = True
Else
panelOutcome.Visible = False
End If
End Sub
I've started to figure this out based on other questions I looked up on here, but all of them were in C# instead of VB, so this is as far as I got:
Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
If FormView1.CurrentMode = FormViewMode.Edit Then
End If
End Sub
So yeah I'm not sure exactly how to finish it. I'm sorry, this might be pretty basic, but I'm new at this and any help would be appreciated!
EDIT: here's my code now:
Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
If FormView1.CurrentMode = FormViewMode.Edit Then
CheckBox checkGenEd = formview1.FindControl("checkGenEd");
Panel panelOutcome = formview1.FindControl("panelOutcome");
End If
End Sub
It's also saying that checkGenEd and panelOutcome are not declared.
EDIT: I changed my code to this but it still doesn't work:
Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
If FormView1.CurrentMode = FormViewMode.Edit Then
Dim checkGenEd As CheckBox = FormView1.FindControl("checkGenEd")
Dim panelOutcome As Panel = FormView1.FindControl("panelOutcome")
If checkGenEd.Checked = True Then
panelOutcome.Visible = True
Else
panelOutcome.Visible = False
End If
End If
End Sub
There aren't any errors anymore, but nothing happens when I click the checkbox. I think there needs to be some kind of event to trigger it but I don't know how you can put an event handler inside of an event handler.
With FormView, you have to use find control, as in:
CheckBox checkGenEd = (CheckBox)formview1.FindControl("checkGenEd");
Panel panelOutcome = (Panel)formview1.FindControl("panelOutcome");
You cannot reference a control directly by ID.
HTH.
In VB you need use Directcast
Dim chk As Checkbox = DirectCast(Me.FormView1.FindControl("checkgen"), Checkbox)
FormView has its own event framework. A normal control within a FormView won't generate the postback events you are looking for. I initially made the same mistake. I wanted, like you, to generate some kind of postback that could be intercepted at the server end. Once we get back to the server we can look at the values in checkboxes etc depending on whatever business rules apply. This is what I did.
First of all put all relevant controls within an
<EditItemTemplate>
section within the FormView. (There are other Template tags that may be more appropriate). To generate the postback have a button (for example) like the one below. (This has to be within the EditItemTemplate section as well):
<asp:linkbutton id="UpdateButton"
text="Update"
commandname="Update"
runat="server"/>
You can intercept this at the server with the FormView event ItemCommand. For example:
Private Sub FormView1_ItemCommand(sender As Object, e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles FormView1.ItemCommand
'your code here
End Sub
Once you are back at the server you can then start looking at the various controls to see what they hold, using findControl if necessary. The button command shown above is an example so you might want to use another control.