Accessing the value of a passed in argument to click event c#

别说谁变了你拦得住时间么 提交于 2019-12-25 07:37:19

问题


I have a gridview defined like this...

<asp:GridView runat="server" id="gvProjectLineage"
    ShowFooter="false"
    DataKeyNames="projectLineageID"
    AutoGenerateColumns="False"
    AllowPaging="false"
    AllowSorting="false"
    RowStyle-CssClass="shade" CellSpacing="1"
    AlternatingRowStyle-CssClass="unshade"
    BorderColor="Transparent"
    DataSourceID="gvProjectLineageDataSource"
    Width="100%">
    <HeaderStyle HorizontalAlign="Left" Font-Underline="true"></HeaderStyle>
        <Columns>
            <asp:BoundField ReadOnly="True" 
                HeaderText="Lineage ID"
                InsertVisible="false" 
                DataField="projectLineageID"
                HeaderStyle-CssClass="columnHeader" 
                ItemStyle-VerticalAlign="Top"
                Visible="false">
            </asp:BoundField>
            <asp:BoundField ReadOnly="True" 
                HeaderText="Relationship type"
                InsertVisible="false" 
                DataField="projectRelationshipType"
                HeaderStyle-CssClass="columnHeader" 
                ItemStyle-VerticalAlign="Top"
                Visible="true">
            </asp:BoundField>
            <asp:BoundField ReadOnly="True" 
                HeaderText="Related Project"
                InsertVisible="false" 
                DataField="projectTitle"
                HeaderStyle-CssClass="columnHeader" 
                ItemStyle-VerticalAlign="Top"
                Visible="true">
            </asp:BoundField>

            <asp:TemplateField ShowHeader="False" 
                HeaderStyle-Font-Underline="false" 
                ItemStyle-HorizontalAlign="Left" 
                FooterStyle-HorizontalAlign="Left">
                   <ItemTemplate>
                       <asp:LinkButton ID="btnDelete"
                           CommandArgument='<%# Eval("projectLineageID") %>'
                           CommandName="Delete" 
                           runat="server">
                           <img style="border:none;"
                             src="/content/images/icon/deleteIcon.png"
                             title="Delete this lineage" />
                       </asp:LinkButton>
                   </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField ShowHeader="False" 
                HeaderStyle-Font-Underline="false" 
                ItemStyle-HorizontalAlign="Left" 
                FooterStyle-HorizontalAlign="Left">
                   <ItemTemplate>
                       <asp:LinkButton ID="btnDeleteAll" 
                           CommandArgument='<%# Eval("projectLineageID") %>' 
                           runat="server" 
                           OnClick="btnDeleteAll_Click" >
                           <img style="border:none;"
                            src="/content/images/icon/deleteIcon.png"
                            title="Delete this lineage AND it's reciprocal lineage" />
                       </asp:LinkButton>
                   </ItemTemplate>
            </asp:TemplateField>

        </Columns>
        <EmptyDataTemplate>There are currently no relationships identified for this S&T project.</EmptyDataTemplate>
</asp:GridView>

The first "delete" button calls a simple delete query and it works fine to delete the selected row from the table. However, when I call the 2nd delete button ("DeleteAll"), I want to use the [projectLineageID] to find the related lineage records for a project and delete them all at once.

When I call the 2nd delete function "onclick" as follows, I get nothing in column [0].

protected void btnDeleteAll_Click(object sender, EventArgs e)
{
    //Delete lineage and reciprocal lineage for relationship types 1 (Transitioned from) and 2 (Transitioned to)
    util u = new util();
    GridViewRow grdrow = (GridViewRow)((LinkButton)sender).NamingContainer;
    string strRelID = grdrow.Cells[0].Text;
    int intRelID = System.Convert.ToInt32(grdrow.Cells[0].Text);
    string relType = grdrow.Cells[1].Text;
    string relProj = grdrow.Cells[2].Text;

    Response.Write ("Deleting relationship between " +  projectID  + " and the project in column 3  " + relID + "   " + relType + "  " + relProj + "  :");
}

How do I get the projectLineageID to pass it to the query? And, why can't I display it? Even if I try to "convert" it, I get an error telling me the "Input string was not in a correct format."

Thanks, Bob

来源:https://stackoverflow.com/questions/38111190/accessing-the-value-of-a-passed-in-argument-to-click-event-c-sharp

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