开发者

Setting Visibility of Button Control in GridView Header

I have a gridview that displays entries from a data table. I am giving users the ability to select a subset of the data in the table by having a textbox and search button in the grid view header. The search button fires the gridview row command, and changes the underlying sqlDataSource's select command, and adds the text value from the text box as a parameter.

This works smoothly.

Also, I have a "Show All" button in the header, that clears out the select parameters, so all entries in the table are shown. Again, this works perfectly.

What is NOT working is controlling the visibility of the "Show All" button control. Below is the html markup for the data grid header template:

<HeaderTemplate>
    <asp:Button ID="btnShowAll" runat="server" CausesValidation="False" CommandName="ShowAll" Text="Show All" />
    <asp:Button ID="btnSearch" runat="server" CausesValidation="True" CommandName="Search" Text="Search" ValidationGroup="vldSearch" /><br />
    <asp:TextBox ID="txtSearchName" runat="server"></asp:TextBox>&nbsp;
    <asp:RequiredFieldValidator ID="vldSearchName" runat="server" ErrorMessage="You have to provide an attorney name to search for." Text="*" ControlToValidate="txtSearchName" ValidationGroup="vldSearch" ForeColor="White"></asp:RequiredFieldValidator>
</HeaderTemplate>

In the Row Command event ha开发者_如何转开发ndler, here is how I am setting the visibility of the button:

If Not Me.dgAttorneys.HeaderRow Is Nothing Then
    Dim btnShowAll As Button = Me.dgAttorneys.HeaderRow.FindControl("btnShowAll")
    btnShowAll.Visible = Me.sqlAttorneys.SelectParameters.Count > 0
    Trace.Write("Show all status is " & btnShowAll.Visible.ToString)
End If

The trace statement is showing the correct visible status - if the "show all" button is clicked, I do a SelectParameters.Clear() on the sqlAttorneys sqlDataSource.

Is my problem due to a misunderstanding of how the "FindControl" method works - I had assumed my new btnShowAll that I define is actually a reference to the "physical" control on the aspx page, so any changes I make to my local object is reflected in the control on the page.

If this is not the case, what is the best way to get a reference to the button control in the header row of the grid view?


I managed to get the button behavior to work - it was all to do with where in the overall process I was setting the button visibility. I moved that code block (setting the button visibility based on the presence of a search parameter) to the DataBound event for the data grid, and the button's visibility was set as it should be.

I suspect this is because during the overall data binding process, based on the state of the overall grid view and each grid row, the appropriate template object is used to render each row. Thus, any changes made to the button's visible property were being overridden during the data binding process. By shifting my code to set the visibility until after the data binding was complete, then it took effect.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜