无法从后面的代码访问gridview内的超链接列

时间:2016-08-10 12:41:46

标签: c# asp.net gridview hyperlink

我想从后面的代码访问GridView的HyperLinkField列,但我无法这样做。

我的数据集中有列,但我仍然无法使用它。

我的HyperLinkField列名为Status

img

以下是我的尝试:

UltraWebGrid1.DataSource = ObjPriDsGrid;
UltraWebGrid1.DataBind();

string StrPriStatus = "";

for (int IntPriI = 0; IntPriI < UltraWebGrid1.Rows.Count; IntPriI++)
{
    if (UltraWebGrid1.Rows[IntPriI].Cells[6].Text.Trim() != null)
    {
        StrPriStatus = UltraWebGrid1.Rows[IntPriI].Cells[6].Text.Trim();
    }
    else
    {

    }
    if (StrPriStatus == "5")
    {
        UltraWebGrid1.Rows[IntPriI].Cells[8].Text = ""; // Not getting status column here
    }
}

这是我的GridView:

<asp:GridView ID="UltraWebGrid1" ShowHeader="true" runat="server" AutoGenerateColumns="false"
    DataKeyNames="mkey" OnRowDataBound="Grid_RowDataBound" Width="98%" Height="30%"
    PageSize="10" AllowPaging="true" OnPageIndexChanging="Grid1_PageIndexChanging"
    ShowFooter="true" CssClass="Grid">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <img alt="" style="cursor: pointer" src="../../../Images/plus.png" />
                <asp:Panel ID="pnlGrid" runat="server" Style="display: none">
                    <asp:GridView ID="Grid2" runat="server" AutoGenerateColumns="false" Width="600px"
                        CssClass="ChildGrid">
                        <Columns>
                            <asp:BoundField ItemStyle-Width="80px" DataField="RefMkey" HeaderText="Mkey" />
                            <asp:BoundField ItemStyle-Width="150px" DataField="CurrentUser" HeaderText="Current User" />
                            <asp:BoundField ItemStyle-Width="180px" DataField="Department" HeaderText="Current Department" />
                            <asp:BoundField ItemStyle-Width="100px" DataField="remarks" HeaderText="Remarks" />
                        </Columns>
                    </asp:GridView>
                </asp:Panel>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField ItemStyle-Width="0px" DataField="mkey" Visible="false" HeaderText="Mkey" />
        <asp:BoundField ItemStyle-Width="8%" DataField="Doc_No" HeaderText="IW/ No" />
        <asp:BoundField ItemStyle-Width="10%" DataField="Doc_Date" HeaderText="IW/ Date" />
        <asp:BoundField ItemStyle-Width="12%" DataField="DocType" HeaderText="Doc type" />
        <asp:BoundField ItemStyle-Width="15%" DataField="Party_Name" HeaderText="Party Name" />
        <asp:BoundField ItemStyle-Width="0" Visible="true" DataField="Status_Flag" HeaderText="Status" />
        <asp:BoundField ItemStyle-Width="10%" DataField="LastAction_datetime" HeaderText="Last Action Date" />
        <asp:BoundField ItemStyle-Width="10%" DataField="CurrStatus" HeaderText="Current Status" />
        <asp:BoundField ItemStyle-Width="10%" DataField="Type_desc" HeaderText="Resp Dept" />
        <asp:BoundField ItemStyle-Width="12%" DataField="UserName" HeaderText="Resp User" />
        <asp:BoundField ItemStyle-Width="8%" DataField="No_Of_Days" HeaderText="No of days" />
        <asp:HyperLinkField ItemStyle-Width="5%" DataNavigateUrlFields="Mkey, Status, Doc_No"
            DataNavigateUrlFormatString="~/Administration/Dispatch/Inward/FrmInwardNextAction.aspx?Inward_mkey={0}&Status={0}&IWNO={0}"
            HeaderText="Status" DataTextField="Status" Target="_blank" />
        <asp:HyperLinkField ItemStyle-Width="5%" DataNavigateUrlFields="Mkey, Status, Doc_No"
            HeaderText="View" DataTextField="View" Target="_blank" DataNavigateUrlFormatString="~/Administration/Dispatch/Inward/InwardDocDetails.aspx?Key={0}&Status={0}&IWNO={0}" />
    </Columns>
</asp:GridView>

2 个答案:

答案 0 :(得分:1)

HyperLinkField和CheckBoxField等列的行为与BoundField略有不同。它们不仅仅包含像BoundField这样的文本。你必须得到细胞内的控制。您可以使用Controls集合获取单元格的子控件。我们知道专门针对HyperLinkField,HyperLink将是该集合中的第一个控件。

HyperLink hyp = (HyperLink)UltraWebGrid1.Rows[IntPriI].Cells[12].Controls[0];

答案 1 :(得分:0)

你能这样尝试吗

if (StrPriStatus == "5")
{
   HyperLink HyperLinkObj = (HyperLink)UltraWebGrid1.Rows[IntPriI].Cells[12].Controls[0];
   HyperLinkObj.Text = "";
}