How to bind data to linkbutton without interfering

2019-09-08 08:29发布

问题:

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.

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.

回答1:

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
    }
}