ASP.NET: Gridview Showing a button on a row, when a row cell vlaue = false in the Bound row from the database
I have a GridView 开发者_运维知识库binded to a datasource, i was wondering of how i can check a row cell value (bool) from the database row being bound , and then show a button on the rows where the cell value equals to false..
i am using OndataBound Event to retrieve the Gridview-row being bound, i take the ID show, run another procedure against the database to find the value of the cell from the corresponding Database row.. but i cant figure out how to add the button..
also is there any other ways to handle this scenario?
Solution 1: Create a button with an ID where you want it in the gridview, with an attribute visible=false. Whenever you want to show the button, retrieve it (currentGridRow.FindControl("chosen button ID")
) and set it's visible attribute to true.
put your button in a templatefield, like that:
<asp:TemplateField HeaderText="foobar" >
<ItemTemplate>
<asp:ImageButton ID="plusbutton" CssClass="cplusButton" ToolTip="plusButton" OnClick="buttonAdd_Click" runat="server" Visible = "false"/>
</ItemTemplate>
</asp:TemplateField>
Solution 2: dynamically create the button (Button b = new Button; currentGridRow.Cell[].Controls.Add(b);
), but it's a pain to deal with the viewstate and the events handler. Don't go that way.
精彩评论