How to bind data to linkbutton without interfering with other boundfields?

柔情痞子 提交于 2019-12-25 03:00:49

问题


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


来源:https://stackoverflow.com/questions/30188620/how-to-bind-data-to-linkbutton-without-interfering-with-other-boundfields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!