Link to home
Start Free TrialLog in
Avatar of bmutch
bmutch

asked on

Click on text in a dotnet asp datagrid cell and run server event.

Currently I have a company org. chart displayed in an asp.net datagrid, and a button in the first cell. If user clicks button all the employees (rows) beneath (and under authority of) that person are displayed to show the command chain, click button again and the branch is collapsed. I want to accomplish the same thing by clicking on the employees name ( the column position that displays the employees name is dynamic of couse depending on where the employee is in the command chain). It would just be nice if the user did not have to locate the correct button (row) in the first column that is associated with the name they are looking at, so I want them to be able to click the name instead. How can I best accomplish this.   (VS 2008)

thanks.
Avatar of sgomzin
sgomzin
Flag of United States of America image

Hi bmutch,
You should use HyperLinkField.  Display employee name using DataTextField. Create dynamic link using DataNavigateUrlFormatString field. The data field for navigation  to your details should be defined in DataNavigateUrlFields.
Example:
<asp:HyperLinkField DataNavigateUrlFields="EMPLOYEE_ID"
DataNavigateUrlFormatString="ShowEmployeeDetails.aspx?EmployeeID={0}"
DataTextField="EMPLOYEE_NAME" > 
Avatar of Allister_Reid
Allister_Reid

Hi,
have you thought about using a ButtonField rather than BoundField here?


Regards
Avatar of bmutch

ASKER

sgomzin: I've used the hyperlink before to open a new browser window, but do you have any details (code) as to how I could click on it and have it behave like a grid button would, ie cause a postback and run the itemcommand method?

Allister_Reid: thought about using buttonfield but would prefer the text.
Hi Allister_Reid,
I guess that's what you are looking for: use TemplateField with LinkButton. Set Text to the employee name, and commandArgument - to the employee ID, and create OnClick event. So when user clicks on the employee name you will receive the employee ID in in the event argument in server side event handler.
 

            <asp:TemplateField HeaderText="TemplateField">
            <ItemTemplate>
            <asp:LinkButton runat="server" ID="DetailsLinkButton"
            Text='<%# Eval("EMPLOYEE_NAME") %>' CommandName="DetailsLinkButton"
            CommandArgument='<%# Eval("EMPLOYEE_ID") %>' OnClick="DetailsLinkButton_Click" />
            </ItemTemplate>
            </asp:TemplateField>
 
    public void DetailsLinkButton_Click(Object sender, EventArgs e)
    {
        if (sender is LinkButton)
        {
            int EMPLOYEEID = Convert.ToInt32((sender as LinkButton).CommandArgument);
        }
    }

Open in new window

Sorry bmutch, the last comment was addressed to you
I'm not sure there's a need to go for a template field here, have you tried a ButtonField and changing the type to Link?
Avatar of bmutch

ASKER

The buttoncolumn is working well. But I do have a question on it. I would like to only have the buttons "clickable" if they actually have people working for them. Is there a way to somehow "disable" the link on the buttoncolumn?

thanks.
Yeah, you can set the Enabled property of the button to false.  You may be able to do this when you are populating the text?
Avatar of bmutch

ASKER

ok, cool. Is there anyway to avoid the "grayed-out" look of the disabled button though, I tried this but It didn't work:

btn.ForeColor = Color.Green
thanks.
ASKER CERTIFIED SOLUTION
Avatar of Allister_Reid
Allister_Reid

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bmutch

ASKER

very good, thanks.
Avatar of bmutch

ASKER

excellent