Mike91
Mike91

Reputation: 540

Access asp.net gridview edit control

I would like to access a calendar control through c# for when a gridview row is edited. However, I am unsure of how to access this through C# behind.

I would like to do the following:

cal_SelectionChanged(object sender, EventArgs e)
{
date = cal.SelectedDate

blah blah...
}

However, as the control is in the gridview edit template, I cannot find a way to access this to C#, compared to if the calendar was just an element on a normal asp.net page.

Thanks

Upvotes: 1

Views: 1269

Answers (1)

freefaller
freefaller

Reputation: 19953

As you appear to be aware, controls within Template based controls (such as <asp:Repeaters>) cannot be accessed directly as you would a control placed directly into a page / usercontrol / masterpage. It is possible to find it using the "FindControl" on the specific item within the control, but there should be an easier way...

In this case you should be able to set a SelectionChanged event handler on the calendar control to the function in your code-behind. (This is assuming you're using the inbuilt Calendar control) Something like...

<asp:Calendar runat="server" OnSelectionChanged="cal_SelectionChanged"> ... </asp:Calendar>

When an event such as this is called, the object that caused the event (in this case the calendar control) is passed into the function through the sender parameter.

This means that instead of trying to work out which calendar was clicked, you simply can do this...

cal_SelectionChanged(object sender, EventArgs e)
{
  Calendar cal = (Calendar)sender;
  date = cal.SelectedDate;
  blah blah...
}

Upvotes: 1

Related Questions