• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1668
  • Last Modified:

Disabling a buttonfield in a gridview table

Dear experts,

I am designing a helpdesk website, on one of the pages I have a a gridview in the last column is a button field.
I have some SQL which updates the helpdesk record. But once the button has been click it should be disabled so the user can not update the record again.

So, is there a way to disable / remove the button in the gridview??

Thanks in advance
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int index = Convert.ToInt32(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Text);
        
        string usr = HttpContext.Current.User.Identity.Name.ToString().Substring(HttpContext.Current.User.Identity.Name.ToString().LastIndexOf("\\")+1);
        DateTime rightNow = DateTime.Now;
        
        string date = String.Format("{0:d}", rightNow);
        string time = String.Format("{0:T}", rightNow);
 
        helpDeskDB.InsertCommand = "UPDATE Emails SET [Date Dealt With] = '" + date + "', [Time Dealt With] = '" + time + "', [By] = '" + usr + "', [Compleate]='true' WHERE ID ='" + index + "';";
        helpDeskDB.Insert(); 
    }

Open in new window

0
keredB
Asked:
keredB
  • 5
  • 3
  • 3
  • +1
3 Solutions
 
sunithnairCommented:
You could find the button control using FindControl and then add a "disabled" attribute to disable button or set the Enabled=false property after finding the control
0
 
sunithnairCommented:
Or like this
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int index = Convert.ToInt32(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Text);
        
        string usr = HttpContext.Current.User.Identity.Name.ToString().Substring(HttpContext.Current.User.Identity.Name.ToString().LastIndexOf("\\")+1);
        DateTime rightNow = DateTime.Now;
        
        string date = String.Format("{0:d}", rightNow);
        string time = String.Format("{0:T}", rightNow);
 
        helpDeskDB.InsertCommand = "UPDATE Emails SET [Date Dealt With] = '" + date + "', [Time Dealt With] = '" + time + "', [By] = '" + usr + "', [Compleate]='true' WHERE ID ='" + index + "';";
        helpDeskDB.Insert(); 
  ((Button)sender).Enabled=false;
    }

Open in new window

0
 
sunithnairCommented:
Or like this
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int index = Convert.ToInt32(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Text);
        
        string usr = HttpContext.Current.User.Identity.Name.ToString().Substring(HttpContext.Current.User.Identity.Name.ToString().LastIndexOf("\\")+1);
        DateTime rightNow = DateTime.Now;
        
        string date = String.Format("{0:d}", rightNow);
        string time = String.Format("{0:T}", rightNow);
 
        helpDeskDB.InsertCommand = "UPDATE Emails SET [Date Dealt With] = '" + date + "', [Time Dealt With] = '" + time + "', [By] = '" + usr + "', [Compleate]='true' WHERE ID ='" + index + "';";
        helpDeskDB.Insert(); 
  ((Button)sender).Attributes.Add("disabled", "true");
    }

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Praveen VenuCommented:
Make the column as a template field
Add a button like below

<asp:Button ID="Button1" runat="server" Enabled='<%# Eval("Compleate")=="true"  
                        Text="Button" />


0
 
CakinciCommented:
You have to define a templatefield like in below example in your asp code and then create a method called isFieldUpdated(string flag) in the code behind which returns a boolean.

Your Aspx Code should look like this.


<asp:gridview id="gvProducts" runat="server" allowpaging="True" autogeneratecolumns="False"
	datakeynames="ProductID" datasourceid="sdsProducts" onrowcommand="gvProducts_RowCommand">
	<columns>
		<asp:templatefield>
			<itemtemplate>
				<asp:button id="btnUpdate" runat="server" commandname="update" enabled='<%# isFieldUpdated() %>'
					text="Update" />
			</itemtemplate>
		</asp:templatefield>
	</columns>
</asp:gridview>
<asp:sqldatasource id="sdsProducts" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
	selectcommand="any sql command">
	<updateparameters>
		<asp:parameter name="ProductID" />
	</updateparameters>
</asp:sqldatasource>

Open in new window

0
 
keredBAuthor Commented:
Thanks guys,

I have used an item template (Cakinci) and using the Eval() function (praveenvenu).
Although I changed the code to be,

Eval("Compleate")== true

As it was always returning false, this is because the 'Compleate' value in the DB is a boolean. But now
I am getting an error now, CS0019: Operator '==' cannot be applied to operands of type 'object' and 'bool'.

If I use Eval("Compleate") I get a "Specified cast is not valid" error.

P.S. I'm thinking the comparison should be != true, because if the helpdesk call is complete I don't want the button to be enabled.


0
 
Praveen VenuCommented:
((bool)Eval("Compleate"))!= true
0
 
sunithnairCommented:
Yes it should be != and try this
Convert.ToBoolean(Eval("Compleate"))!= true

Open in new window

0
 
keredBAuthor Commented:
Hi Guys,

I've figured it out, I'm using

<%# ((Boolean)(Eval("Compleate")) == false ? true:false)%>

Getting an error when I click the button. As follows,

"Input string was not in a correct format"


//ASP code //
 
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataSourceID="helpDeskDB" onrowcommand="GridView1_RowCommand">
            <Columns>
...
                <asp:templatefield>
                    <itemtemplate>
                            <asp:button id="btnComplate" runat="server" CommandName="Complete" Enabled='<%# ((Boolean)(Eval("Compleate")) == false ? true:false)%>'
                                      HeaderText="Compleated" ShowHeader="True" Text="Compleated" />
                    </itemtemplate>
            </asp:templatefield>
            </Columns>            
        </asp:GridView>
 
// C# Code Behind //
 
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int index = Convert.ToInt32(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Text);
        
        string usr = HttpContext.Current.User.Identity.Name.ToString().Substring(HttpContext.Current.User.Identity.Name.ToString().LastIndexOf("\\") + 1);
        DateTime rightNow = DateTime.Now;
        
        string date = String.Format("{0:d}", rightNow);
        string time = String.Format("{0:T}", rightNow);
 
        helpDeskDB.UpdateCommand = "UPDATE Emails SET [Date Dealt With] = '" + date + "', [Time Dealt With] = '" + time + "', [By] = '" + usr + "', [Compleate]='true' WHERE ID ='" + index + "';";
        helpDeskDB.Update();
 
        Control bf = GridView1.FindControl("ButtonField");
 
        bf.Visible = false;
    }

Open in new window

0
 
Praveen VenuCommented:
paranthesis issue

<%# ((Boolean)Eval("Compleate")) == false ? true:false %>
0
 
keredBAuthor Commented:
The Error lies with,

int index = Convert.ToInt32(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Text);

I'm guessing because I have changed the button from a ButtonField to a ItemTemplate Button.
0
 
sunithnairCommented:
Try adding a RowCreated event like this and set the CommandArgument of the button to to the row index
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        ((Button)e.Row.FindControl("btnComplate")).CommandArgument = e.Row.RowIndex.ToString();
    }

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now