Disable button server control under GridView when one of the Column in Gridview Total is Zero

Hi

web page(aspx) consist of a GridView with two columns displayed from Database.

GridView has two columns 1)Employees_Department and 2)Total_Employees. In Employees_Department column employee department name is retrieved and displayed as a Hyperlink when Total_Employees column value is not Zero(This is achieved using row_databound event code shown below). Clicking or selecting particular Employees_Department row will open data for that Employees_Department in a new window. In Total_Employees column of the GridView, total number of employees in that(row) department displayed.

GridView is working fine, dispalying expected data , clicking particular Employees_Department row(Hyperlink) display data in a new window.

GridView RowDataBound code in C# 

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
                {

                             if (e.Row.RowType == DataControlRowType.DataRow)
                                     {

                                          //Get the value from DataItem

                                        string name = DataBinder.Eval(e.Row.DataItem, "name").ToString();
 

                                      // Get the control from the row by the control ID

                                    HyperLink EmployeeDepartment = (HyperLink)e.Row.FindControl("Employees_Department");
 

                                     // Get Presence column control from the row by the control ID

                                    HyperLink Totalemp = (HyperLink)e.Row.FindControl("Total_Employees");
 

                                                 if (Total_Employees.Text != "0")
                                                       {

                                                         Employees_Department.NavigateUrl = "~\\anotherpage.aspx?Name=" + name;                                                          
                                                       }

                                     }

            }

 

Under GridView, there is a button Server control that will Open a window ON BUTTON CLICK and displays data for all Employees_Department column(or for all records in GridView). This requirement has been achieved as well using the code below:

 

// Below code opens a window and displays data for all Employees_Department column in Gridview
// i.e., Session data.

Button1.Attributes.Add("onclick","window.open('AllEmpWebform.aspx');return false;");
Like to know how to disable the button(on click), when Total_Employees column total is Zero,
In other words when Total of Total_Employees column is Zero or all rows value in Total_Employees column is Zero , button under GridView should be disabled(clicking on button shouldn't do anything).  

Regards,


-- "Mark As Answer" if my reply helped you --
0
sukumarraju
1/13/2008 8:30:51 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

2 Replies
1223 Views

Similar Articles

[PageSpeed] 46

I would suggest making sure you have converted the column in the gridview to a template.  Then, in the button, you can have an attribute like:

enabled = '<%# IsTotalEmployeesZero(Eval("Total_Employees")) %> ...

then, put the method in your codebehind

protected bool IsTotalEmployeesZero(int numEmployees)

{

  if (numEmployees == 0)

   return false;

else

 return true;

Or something like this.


Peter Kellner
http://73rdstreet.com and blogging at
http://PeterKellner.net
MVP, ASP.NET
0
pkellner
1/13/2008 11:21:47 PM

Hi,

Based on my understanding, you want to set the Button to disabled if the Total value of Total_Employees column is zero. This Button is in out of GridView. If I have misunderstood you, please feel free to let me know.

In your case, we can set the Button to disable in the GridView1_RowDataBound event, where we can count the total value of Total_Employees column. For example:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //Total value of Total_Employees column
        int TotalValue = 0;

        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            //Get the value from Total_Employees
            int value = Convert.ToInt32( DataBinder.Eval(e.Row.DataItem, "Total_Employees"));

            // Count every row's value
            TotalValue += value;
            
        }

        
        if (TotalValue < 1)
        {
            //Total value is zero.
            Button1.Enabled = false;
        }
        else
        {
            //Total Value is larger than zero.
            Button1.Attributes.Add("onclick", "window.open('AllEmpWebform.aspx');return false;");Button1.Enabled = true;

        }
        
        

    }
 

 


I hope this helps. 

 


Thomas Sun
Microsoft Online Community Support

Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
0
Thomas
1/15/2008 2:22:41 AM
Reply:

Similar Artilces:

gridview control in another gridview control
Hi ia have a questioni have a gridview control which contains button in one colum.now my question is i need another gridview to be placed in the  first gidviewso that when i click on the button the second gridview will populate the data.now my question is how to place second gridview in first gridview.or is there any approach like first grid itself acting as parent and child.please help me  Hi sirikalavalapalli:                I think nested gridview will help you. Check this link and it will show you how...

Help Req: !!! :- On Disabling Controls Present inside a Gridview control
Hi All, We have A requirement Where we have disable controls inside a grid.These controls can be textbox, DropDownlist (in TemplateField), ComandField present inside the grid.We have to call a generic method for doing the same .(since it can be done for multiple grid.) PFB the code snippet we use. Which disables all the controls inside the page.The bolded part of the code is used to loop inside a grid view.(Not Working.)Is this ideal way to deal with the requirement.Or can someone suggest be a better way.        public void DisableMe(Page p1, st...

Gridview control embedded in another gridview control
I am writing an ambulance crew scheduling application on the web and I need some help completing the last part.  I have an access database that has three tables: user table, shift table (describing the shifts available), and a schedule table (that stores shift by shift_id and user by user_id).  I have a gridview control that will display the available shifts but I cannot get the embedded gridview control that will display the user that is signed up for each shift to display because I cannot figure out how to use the OnRowDataBound method.  I need a simple VB example of how to...

using javascript getting value from textbox when edit button is clicked and both controls are present in gridview control
Hi All using javascript I want to get value  from textbox when edit button is clicked and both controls that is texbox and button are present in gridview control .Alok Hi aloksinha83, Please see this post: http://forums.asp.net/t/1069245.aspx Thanks,Qin Dian TangMicrosoft Online Community SupportPlease remember to mark the replies as answers if they help and unmark them if they provide no help....

There is a Gridview Control in the tmplate field of another GridView Control.
    For instance,a gridview displays the information of the  students in a class.A table is recording the information except their mark.There is another table recording the mark of the students whose foreign key is the primary key of the previous table.   string strSQLconnection=".................."; SqlConnection sqlCononection=new SqlConnection(strSQLconnection); SqlCommand sqlCommand=new SqlCommand("select * from stuendent where classID=1",sqlConnection); SqlConnection.Open(); SqlDataReader reader=sqlCommand.ExecuteReader(); GridVie...

Gridview column and control settings. Disable buttons, checkbox based on row value
I'm using C# Hello, I have a gridview with a checkbox in the first column along with a button in the last column. Based on a rows value (on load), I want to make the checkbox grey or not clickable and then enable the button in the last column for that row. If the value is different I want the checkbox clickable, but I want to hide or disable (make it grey) the button in the last column. I'm using the RowDataBound method and have access to the row as it loads. But do not know how to change the checkbox to grey or disabled (same for the button). Does anybody know how to disable the check...

Binding GridView Columns with controls outside the GridView
HiI already posted this issue but couldn't get the proper resolution so making it more clear;Problem is my controls are outside the grid. Like ive text boxes, for First Name and Last Name and dropdown for country. User can fill these fields for entering a new record and also update the existing by selecting it from the grid.Like user selects a row from the grid and its contents will be displayed on the textboxes and drop downs that are out side the grid.Hope i could explain it better now.  RegardsMohsin Shahzad You could use a DetailsView or FormView control that is linked to...

How can I control the width of a column in the GridView control.
From searching I see mention of ways to do it before the controls are bound. Is there a simpler way? If not, could someone explain to me how I need to go about it?Here is the code: <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"        DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display."        OnSelectedIndexChanged="GridView1_SelectedIndexChanged">   &nbs...

Trying to use the FAQ " Sorting and paging in the GridView control when not using data source controls "Sorting and paging in the GridView control when not using..."
and I am not getting the results I had hoped for.  I keep getting a "Compiler Error Message: CS0123: No overload for 'AdhocJobGridView_PageIndexChanged' matches delegate 'System.EventHandler'".   Here is my event handling... protected void AdhocJobGridView_PageIndexChanged(object sender, GridViewPageEventArgs e){ try { AdhocJobGridView.DataSource = wsBaseInfoArray; AdhocJobGridView.PageIndex = e.NewPageIndex; AdhocJobGridView.DataBind(); }catch (Exception ex) {throw ex; }   Here is my aspx... <asp:GridView ID="AdhocJobGridView...

How to add a Web server or HTML server controls (such as CheckBox) to the template in GridView and bind the controls to a data source.
How to add a Web server or HTML server controls (such as CheckBox) to the template in GridView and bind the controls to a data source. Normally, We can add a Web server or HTML server controls to the template in GridView control,just like the Code 1I hope to do the same thing by programming (just like the Code 2), but I can ony add the BoundField,I don't know how to add a CheckBox control to the template in GridView , could you help me? //--------------------------------------------Code 1------------------------------------------------------------<asp:GridView AutoGenerateColumns="False...

How to manipulate images and combe multiple database columns into one column in a GridView control
Hi Folks,I'm hoping someone can help me out with something.I am trying to put together a photo album as a learning exercise and have a GridView control with two columns linked to an Access Data Source. In the left hand column (called Image) I want to display the photo and in the right hand column (called Details) I want to provide details such as Date, Location, People and Comments, Camera Type etc. Each of these is columns is taken from the database I have set up.However, I have am having some problems:1: At the moment I have been able to get the image to display (using an image column ...

Getting the control id of a control inside gridview control
 uhmm.. a little bit confusing regarding my subject because i dont know how to say it...by the way my problem is how can i get the id of a control (e.g. textbox) inside the gridview... and the textbox is in the templatefield... is there any way i can get its id??  hoping i could get a reply as soon as possible... Regards,MhaeyPlease remember to click “Mark as Answer” on the post that helps you.. =) What do you want's to do by getting the id of the Text box? Do you want's to find particular Textbox with certain id ? Kamran ShahidSr. Software Engineer(MCP,MCAD.net,MC...

OnBubbleEvent with Button (child control) in GridView (parent control)
I have a button (created at runtime) inside the gridview.  I'm having a hard time telling the grid view that row 1 (the row which was selected when the button is clicked) is selected. Here's my code so far:  protected void ContentsGrid_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { counter++; //Display Imagebutton for View in column 1 ImageButton imgView = new ImageButton(); imgView.ID = "imgView" + c...

How to find the control in the Second GridView which is nested in one Gridview ?
Hi,  i have check box in the second gridview.here i wanna check wheather it is checked or not, if more than two boxes are checked i wanna give an alert that it is not possible to select more than three.. The code below is working when i was usign single grid view, can anybody please tell me where i went wrong.........     Code Snippet protected void CheckBox1_CheckedChanged1(object sender, EventArgs e)    {        int i=0, n=0;        foreach (GridViewRow row in GridView1.Rows)...

Web resources about - Disable button server control under GridView when one of the Column in Gridview Total is Zero - asp.net.presentation-controls

GridView SelectedItem Binding Gotcha in WinRT
Last night when I was doing some WinRT hacking I was trying to setup databinding to the selected item in the GridView control and I ran into ...

CNW Group
Home Services Beyond the Wire About CNW Financials Webcasts Events My CNW All News Releases Advanced Search Gridview Latest News Releases All ...

CNW Group
... Services Beyond the Wire About CNW my CNW CNW Access CNW Directories Financials Webcasts Events my CNW All News Releases Advanced Search Gridview ...

CNW Group
Home Services Beyond the Wire About CNW Financials Webcasts Events My CNW All News Releases Add a Filter Gridview Latest News Releases All Today ...

New CmdLets of Powershell 2.0
New CmdLets of Powershell 2.0

Price Drop: Softbox
... objects- create product reflection- changing the background with something more interestingUse it as:- softbox- honeycomb softbox- gridview ...

Profile.aspx: ASP Alliance
.NET,ASP,ASP.NET,SQL,XML,HTML,ADO,ADO.NET,JavaScript,JScript,CSharp,VBScript and VB tutorials from AspAlliance.

Quick Answers - CodeProject
Questions - Free source code and tutorials for Software developers and Architects.; Updated: 25 Oct 2012

DotNetKicks.com - Upcoming stories
DotNetKicks.com is a community based news site edited by our members. It specialises in .NET development techniques, technologies and tools including ...

View Features - ExtendASP
Call 847-681-9827 or Request a Demo View Features Testimonials Pricing About Us Coming Soon ExtendASP exposes every feature, more than any other ...

Resources last updated: 2/4/2016 10:39:31 PM