How to use RadDatePicker inside RadGrid? - asp.net

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

Related

Setting button invisible with event not working

I have a Visual Basic Project where I have a page(Parent.aspx) with a user control inside(ChildForm.ascx), the user control have a checkboxlist where if the selection is changed I cause a postback and send an event
Protected Sub checkBoxList_checked(ByVal sender As Object, ByVal e As EventArgs) Handles checkBoxList.SelectedIndexChanged
RaiseEvent MyEvent(sender, e)
End Sub
to make a button invisible on the Parent view
Protected Sub ChildForm_MyEvent(ByVal sender As Object, ByVal e As EventArgs) Handles ChildForm.MyEvent
If condition Then
btnSave.Visible = False
Else
btnSave.Visible= True
End If
End Sub
When I debug I see the breakpoint hitting all the lines of code and the condition apply as intended, but the button is not hidden, I dont know why if is setting the correct value it never refreshes the Parent page to show the changes, even if it does cause a postback.
Please help
Check your page load events (load, prerender, prerenderComplete) to make sure you are not setting the visibility property in there. if you are, you may need to use
"If not IsPostBack"

How to find selected GridView row in page_load event?

I have a page with GridView. The GridView has select button. Normally I use GridView's selected index changed event to do all kinds of operations when user clicks select button. But now I want to do some operations in Page_Load event based on grid view's selected row. Since the Selected_Index_changed event occurs after Page_Load how do I know following things in page load event.
I checked the asp lifecycle and this other question but I dont know how to do this.
How about using a QueryString to transmit which row was selected and then in the Page_Load event get the QueryString parameters? This is an example.
Protected Sub LinkButton1_Command(sender As Object, e As CommandEventArgs)
Dim UserId As Integer = e.CommandArgument 'Here goes whatever value you're trying to pass
Response.Redirect("~/OtherPage.aspx?UserId=" _
& UserId)
End Sub
This is in the OtherPage.aspx
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
UserId = Request.QueryString("UserId")
'Your code
end sub

RowDataBound DropDownList

I have a RowDataBound event handler that looks like this:
Public Sub CustomersGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GVHistoricNames.RowDataBound 'RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim hyperlinkUSNHyperlink As HyperLink = CType(e.Row.FindControl("USNHyperlink"), HyperLink)
Dim ddl As DropDownList = CType(e.Row.FindControl("ddlUsercode"), DropDownList)
If ddl.SelectedValue = "" Then 'labLastUserCode.Text = "" Then
hyperlinkUSNHyperlink.NavigateUrl = ""
End If
End If
End Sub
...and a RowCreated event handler that looks like this:
Public Sub CustomersGridView_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GVHistoricNames.RowCreated 'RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim ddl As DropDownList = CType(e.Row.FindControl("ddlUsercode"), DropDownList)
ddl.Items.Add("")
ddl.Items.Add(strUserName)
End If
End Sub
...and a RowUpdating event handler that looks like this:
Protected Sub GVHistoricNames_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GVClearcoreHistoricNames.RowUpdating
Try
Dim ddl As DropDownList = CType(GVHistoricNames.Rows(e.RowIndex).FindControl("ddlUsercode"), DropDownList)
SQLHistoricNames.UpdateParameters("UserCode").DefaultValue = ddl.SelectedValue
Catch ex As Exception
Finally
End Try
End Sub
Please see line three of the RowUpdating event handler. The value of the SelectedValue property is never correct because the RowDataBound event handler is called after the RowUpdating event handler. How do I access SelectedValue? I want to set it as an update parameter.
One of the way could be to look into the actual request data. For example, in GVHistoricNames_RowUpdating code, use
Dim ddl As DropDownList = CType(GVHistoricNames.Rows(e.RowIndex).FindControl("ddlUsercode"), DropDownList)
SQLHistoricNames.UpdateParameters("UserCode").DefaultValue = Request(ddl.UiniqueID)
I often use such work-arounds when the control value is needed before post data could be loaded into control (or when controls are added/bound dynamically at a later event).
EDIT
ASP.NET uses Control.UniqueId to represent name property of corresponding html element. It (as well as ClientID) typically gets constructed by appending control's id to parent's (parent that is naming container) unique id, hence you get different unique ids (and client ids) for multiple drop-down lists in the grid (because each row acts as a naming container)
As far as your problem goes, you are probably creating drop-down list in design time template while you are loading your list items in row created. However, before row-created event is fired, the drop-down list would have been already added to page control tree and its POST events would have been already processed. In such case, there would be no items in the drop-down list at that time to set the selection. Hence the issue.

button add event click event

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

ASP.NET, VB: how to access controls inside a FormView from the code behind?

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.

Resources