How to bind data to linkbutton without interfering with other boundfields?
Asked Answered
S

1

0

There are few gridviews in the page. First gridview is editable, second is not. First gridview has a column that has a hyperlink (linkbutton). The second table should be loaded based on the click event of above hyper link. It is like expecting the hyperlink to behave like a button. Can this be done?

In order even try, I am not sure where to start. I added a hyperlink to the first gridview column. But what to set for the NavigationURL is doubtful to me.

There are only handful of events in a hyperlink control. enter image description here

None of that seems to do what I need. Any tutorial is valuable.

Update: Gird markup code

<div id="schedule">
    <asp:GridView ID="gvSchedule" 
        runat="server" AutoGenerateColumns="false" 
        CssClass="Grid" 
        DataKeyNames="Employee ID, WOY, Location" 
        ShowHeader="false" 
        RowStyle-CssClass="rowstyle"
        onrowdatabound="gvSchedule_RowDataBound">
    <Columns>
 <asp:BoundField HeaderText="Staff" 
     DataField="E_Name" SortExpression="E_Name"  
     ItemStyle-Width="113" />
    </Columns>

    </asp:GridView>
    <br />
</div>

Tried out a linkbutton as per this solution here

<asp:TemplateField HeaderText="StaffClick" SortExpression="STOCK NO" ItemStyle-Width="50">
        <ItemTemplate>
           <asp:LinkButton ID="lblStaff" runat="server">Click</asp:LinkButton>
        </ItemTemplate>
        <HeaderStyle BackColor="Black" ForeColor="White" HorizontalAlign="Left"/>
        <ItemStyle HorizontalAlign="Left" />
        </asp:TemplateField>

After using this code, now none of the fields with checkboxes are loaded with rowbound event.

Senskell answered 12/5, 2015 at 10:48 Comment(3)
It really depends how you've started the GridView. Please post the grid markup code here and I will give you more directions.Alecto
If you need an anchor to act like a button, use LinkButton.Alecto
@AdrianIftode added the markup. It's not much. Just plain Asp.Net and a css. How to use the linkbutton in a way that it binds the data?Senskell
C
0

Put a LinkButton in your GridView, and use the "RowCommand" action to set what's supposed to happen. As soon as the link gets clicked, an Event called RowCommand is dispatched. You can handle whatever needs to happen server-side.

CommandArgument can be anything you want to pass to the function on the server side.

Here's an example:

 <asp:Button ID="btnViewmore"  
        CommandArgument="<%# ((GridViewRow) Container).RowIndex %>
        " CommandName="More" runat="server" Text="View More" />

And on the server side you could do this (C#):

protected void gridMembersList_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "More")
    {
        int index = Convert.ToInt32(e.CommandArgument.ToString());
        // do something with your command and commandargument, like for instance, update the second gridview
    }
}  
Coil answered 12/5, 2015 at 11:31 Comment(3)
What other bound fields? You only have the LinkButton. The markup for that seems fine though.Coil
In the markup I only showed the linkbutton. There are alots of controls : template fields, boundfields. It's quite intuitive when OP says there are boundfields.Senskell
What I meant was that I don't see anything in your code that could be giving you the problems. Can you post more code?Coil

© 2022 - 2024 — McMap. All rights reserved.