Search code examples
c#asp.netgridviewitemtemplate

Forcing button to cause postback


I have a button which causes a popup to be created:

<ItemTemplate>
        <asp:Button ID="viewHoursButton" runat="server" Text="View Hours" OnClick="viewHoursButton_OnClick" />
        <ajaxToolkit:ModalPopupExtender ID="viewHoursPopup" runat="server"
                TargetControlID="viewHoursButton"
                PopupControlID="viewHoursPanel"
                CancelControlID="closeInfoPanelButton2"
                DropShadow="true">
                </ajaxToolkit:ModalPopupExtender>
    <asp:Panel ID="viewHoursPanel" runat="server" CssClass="infoPanel">
           //content of panel including gridview
    </asp:Panel>
</ItemTemplate>

The panel that pop's up has a gridview and when the button is pressed a SQL parameter is passed. :

protected void viewHoursButton_OnClick(object sender, EventArgs e)
{
    Button btn = sender as Button;
    GridViewRow row = btn.NamingContainer as GridViewRow;
    SqlDataSource6.SelectParameters["nonScrumStoryId"].DefaultValue = storyGridView.DataKeys[row.RowIndex].Values[0].ToString();
    var viewHoursGridView = storyGridView.FindControl("viewHoursGridView") as GridView;
    if (viewHoursGridView != null)
    {
        viewHoursGridView.DataBind();
    }
}

The issue is that the gridview isn't showing because there is no postback to the server. When you add a button to ajaxToolkit:ModalPopupExtender the postback is pevented. How do I get it back?


Solution

  • You can force postback with Javascript by attaching __doPostBack to an event.

    function doClick(sender, e) { 
    __doPostBack(sender,e); 
    }