tags:

views:

328

answers:

3

Hello,

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.

+1  A: 

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.

Jon P
+2  A: 

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>
roman m
you'll still need to provide the User's ID to the command argument for this situation. and then rebind the entire grid after processing.
Stephen Wrighton
+2  A: 

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
Stephen Wrighton
+1- i bet IsEnabled is a bit in the DB, you might wanna redo your CommandName logic
roman m
@rm - I specified returning TEXT from the dataset for this particular reason. Changing the bit to a string can be accomplished in SQL via: CASE When IsEnabled= 1 THEN 'Disable' ELSE 'Enable' END AS IsEnabled.This will also be faster than an IF statement in code.
Stephen Wrighton