我的网站上有一个日历控件(asp.net中的vb.net)(标准日历控件,但我操作它,现在它看起来像outlook日历)
事件作为动态按钮添加到日历中,每个按钮都有唯一ID ,即使在回发后也是如此。
这是我生成按钮并将其添加到日历中的适当单元格的代码:
Protected Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Calendar1.DayRender
Dim nextDate As DateTime
If Not dsHearings Is Nothing Then
For Each dr As DataRow In dsHearings.Tables(0).Rows
nextDate = CType(dr(6), DateTime)
If nextDate = e.Day.Date Then
e.Cell.BackColor = System.Drawing.Color.LightGoldenrodYellow
Dim btn As New Button
btn.Text = Left(dr(7).ToString, 5) & "-" & "جلسة في ملف" & dr(1) & " " & dr(2) & " (" & dr(8) & ")"
btn.CssClass = "CalendarHearingEvent"
btn.BackColor = Drawing.Color.Red
btn.ToolTip = "جلسة في ملف" & dr(1) & " " & dr(2) & " (" & dr(8) & ")"
btn.ID = "btnHearings" & dr(9).ToString
btn.UseSubmitBehavior = True
AddHandler btn.Click, AddressOf Me.HearingButton_Click
Dim lbl As New Label
lbl.Text = "<br>"
e.Cell.Controls.Add(lbl)
e.Cell.Controls.Add(btn)
End If
Next
End If
这是处理子:
Private Sub HearingButton_Click(sender As Object, e As EventArgs)
End Sub
一切都很完美,但点击事件没有解雇
请帮助
答案 0 :(得分:0)
我使用了LinkButton,并将link button的href属性设置为
lnkButton.Attributes(“ href”)= e.SelectUrl
代码
lnkButton = New LinkButton()
lnkButton.CommandArgument = objScheduleDetail.SelectedDate
lnkButton.Text = <Write your text here>
lnkButton.ID = "lnkView" ' you can make it unique as well
lnkButton.Attributes("href") = e.SelectUrl
e.Cell.Controls.Add(lnkButton)
现在,单击此链接按钮时,将触发Calendar.SelectionChanged事件。然后我使用Calendar1.SelectedDate来获取单击按钮的日期。
Protected Sub Calendar1_SelectionChanged(sender As Object, e As System.EventArgs) Handles Calendar1.SelectionChanged
Calendar1.SelectedDate ' date which is clicked.
End Sub
答案 1 :(得分:0)
感谢danish的回答,但我还需要传递参数。所以我用您的答案加上额外的代码来满足我的应用程序的需求。
问题是,当单击日历中的动态按钮时,它将回发日历事件而不是按钮单击事件,因此应用程序无法捕获该按钮。
为了绕过它,在我后面的代码中,我生成了一个如下的linkbutton:
Dim btn As New LinkButton
btn.Text = "Text On LinkButton"
btn.CssClass = "CalendarEvent"
btn.ToolTip = "What ever"
btn.ID = "LinkButton1"
Dim str As String = "return EventonMyClientClick(" + dr(0).ToString + ");"
btn.OnClientClick = str
e.Cell.Controls.Add(btn)
这将在日历单元格内生成一个链接按钮,其中OnClientClick指向HTML代码中的javascript函数,如下所示:
<asp:LinkButton ID="LinkButton1" style="display:none;" runat="server" onclick="LinkButton1_Click">LinkButton</asp:LinkButton>
<asp:HiddenField ID="hdEventSentId" runat="server" />
<script>
function EventonMyClientClick(val) {
document.getElementById('<%=hdEventSentId.ClientID%>').value = val;
var btn = document.getElementById('<%=LinkButton1.ClientID%>');
btn.click();
}
</script>
如您所见,动态链接按钮将我的参数(dr(0).ToString)传递给eventonmyclientclick函数。函数将获取参数,将其保存在hiddenfield中,然后对linkbutton1链接按钮执行click事件。
该页面回发并捕获linkbutton1 click事件,该事件调用链接按钮处理程序背后的代码,该按钮处理隐藏字段中保存的参数:
Protected Sub LinkButton1_Click(sender As Object, e As EventArgs)
Try
Dim EventId As String = hdEventSentId.Value
....
Catch ex As Exception
End Try
End Sub
我希望这将有助于其他人寻求相同问题的解决方案。