Reputation: 69
I am working on a form with a GridView, pulling data from an SQL Express database. The last column will be a Button that I want to be able to change based on what data is bound to that column.
The button will act as an "Enable/Disable" toggle. Basically, if a user is enabled, I want the button to say "Disable" and have the OnClientClick property point to a disable function; if the user is disabled, I want the button to read "Enable" and point to an enable function.
My google-fu is weak today, hoping you all can be some help. Thanks in advance.
Upvotes: 2
Views: 1132
Reputation: 26521
try this, i assume your Enabled/Disabled is a bit value in the DB
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" ID="btn" Text='<%# "1".Equals(Eval("Enabled").ToString()) ? "Enabled" : "Disabled" %>' OnClick='<%# "1".Equals(Eval("Enabled").ToString()) ? "DISABLE_METHOD" : "ENABLE_METHOD" %>' />
</ItemTemplate>
</asp:TemplateField>
Upvotes: 2
Reputation: 37819
Have the text "Enabled/Disabled" in the dataset that's being used to bind the grid.
Then, utilizing a template column, define the button as thus:
<asp:button text='<%# Eval("IsEnabled") %>' runat=server commandname='<%# Eval("IsEnabled") %>' commandargument ='<%# Eval("UserId") %>' id="myButton" />
Then, on the grid's RowCommand event, do this:
dim btn as button = e.item.findcontrol("myButton")
select case e.Command
case "Enable"
myDisableFunction(e.commandargument)
btn.Text = 'Disable'
btn.Cmmand = 'Disable'
case "Disable"
myEnableFunction(e.commandArgument)
btn.Text = "Enable"
btn.Commandname="Enable"
end case
Upvotes: 2
Reputation: 19772
Use the OnRowDatabound event (assuming .net 2.0 upwards) as your start point. You will then have access to the row data for the row and can change your button as required. I'll see if I can dig up a concrete example for you.
Upvotes: 1