user3036965
user3036965

Reputation: 155

Invoking postback using server generated javascript

I have a Calendar control laid out in asp.net like the following...

<asp:Calendar ID="Calendar1" EnableViewState="False" OnDayRender="Calendar_DayRender"    OnSelectionChanged="Cal_SelectionChanged"
                                                                         FirstDayOfWeek="Default" ShowNextPrevMonth="False" SelectionMode="Day" TitleFormat="MonthYear"
                                                                            runat="server"></asp:Calendar>

I then have some server side asp/vb.net code that append some text and javascript into a cell that has values which are found in the method further up.

Sub Calendar_DayRender(ByVal s As Object, ByVal e As DayRenderEventArgs)
e.Cell.Text = "<table " onclick=""javascript:__doPostBack('cal1','" & strID & "')""" &    " ><tr valign=""top""><td>" & e.Day.Date.Day & "</td></tr><tr><td align=""left"">" & strCount & "</td></tr></table>"

End Sub

I then have the following Event Handler which is bound to the calendar control.

Protected Sub Cal_SelectionChanged(ByVal s As Object, ByVal e As EventArgs)
    lblSelectedDate.Text = "Invoked Method"
End Sub

I am trying to invoke the above method from the forced javascript method. It worked fine until I changed the software and introduced Master Pages. Now it doesn't like it at all. I wondered if anyone had encountered this and what their approach was to resolving it.

Upvotes: 0

Views: 42

Answers (1)

Aristos
Aristos

Reputation: 66641

After you add the Master Page, the rendered id on the javascript side, has change and is not the same as the control id you use on code behind.

To fix that, use the Calendar1.ClientID code, as <%=Calendar1.ClientID%> if you rendered on page.

Or use it direct Calendar1.ClientID if you use it on code behind.

And change the "Calendar1" to Calendar1.ClientID

For example this line will probably be

e.Cell.Text = "<table " onclick=""javascript:__doPostBack('"
               & Calendar1.ClientID & "','" & strID & "')""" & ...

I hope that is clear what I am say.

Upvotes: 2

Related Questions