0

I have created an SPGridView control in my visual web part. Inside SPGridView, there is an SPHyperLink control that stores some URL. When accessing this value using SPGridView SelectedRow.Cells[3].Text, i am getting an empty value.

SPGridView Control

<SharePoint:SPGridView ID="ShowNode" runat="server" AutoGenerateColumns="false" 
        AllowSorting="true" OnRowDataBound="ShowNode_RowDataBound" OnSelectedIndexChanged="ShowNode_RowIndexChanged" OnSorting="ShowNode_Sort" OnRowCommand="ShowNode_RowClick">
<Columns>
<asp:ImageField HeaderText="Icon"  DataImageUrlField="Img" HeaderStyle-HorizontalAlign="Left"></asp:ImageField>
<SharePoint:SPBoundField HeaderText="Name" DataField="Title" HeaderStyle-HorizontalAlign="Left" SortExpression="Name"/>
<SharePoint:SPBoundField HeaderText="Type" DataField="Type" HeaderStyle-HorizontalAlign="Left" SortExpression="Type" />
<asp:HyperLinkField  DataTextField="Link" HeaderText="Link" Visible="false" HeaderStyle-HorizontalAlign="Left"/>
</Columns>
</SharePoint:SPGridView>

Row Click event

protected void ShowNode_RowIndexChanged(object sender, EventArgs e)
        {
            int ind = ShowNode.SelectedRow.RowIndex;
            string link = ShowNode.SelectedRow.Cells[3].Text;


        }
  • At which event you are trying to access the value? On OnSelectedIndexChanged? – Aanchal Sep 22 '14 at 8:13
  • @Aanchal Yes. This is the event - OnSelectedIndexChanged="ShowNode_RowIndexChanged" – Thomas Mathew Sep 22 '14 at 8:30
  • when should it be fired? – Gwny Sep 22 '14 at 11:14
  • @Gwny It triggers when a GridView row item is clicked. – Thomas Mathew Sep 22 '14 at 11:19
0

Try to remove "Visible=false" property from hyperlink field and then check, it should not return empty value then.

I am getting value on my side. Try to debug it and see in window "Shift+F9" Navigate there as this will help you to find value of a particular control.

  • Thanks. But i am getting empty value for other controls too. So visibility is not a factor. – Thomas Mathew Sep 22 '14 at 7:13
0

I was able to get the values using SPGridView DataKey attribute.

GridView Control

<SharePoint:SPGridView ID="ShowNode" runat="server" AutoGenerateColumns="false" AllowSorting="true" ShowFooter="true"  ShowHeader="true" DataSourceID="" DataKeyNames="Link,Type" OnRowDataBound="ShowNode_RowDataBound" OnSelectedIndexChanged="ShowNode_RowIndexChanged" OnSorting="ShowNode_Sort"  EnableViewState="true" >

<Columns>

<asp:ImageField HeaderText="Icon"  DataImageUrlField="Img" HeaderStyle-HorizontalAlign="Left">

</asp:ImageField><SharePoint:SPBoundField HeaderText="Name" DataField="Name" HeaderStyle-HorizontalAlign="Left" SortExpression="Name"/>

<SharePoint:SPBoundField HeaderText="Type" DataField="Type" HeaderStyle-HorizontalAlign="Left"  />

<asp:HyperLinkField  DataTextField="Link" HeaderText="Link" Visible="false" HeaderStyle-HorizontalAlign="Left"/>

</Columns> 

</SharePoint:SPGridView>

Code behind

string u= ShowNode.DataKeys[ShowNode.SelectedRow.RowIndex]["Link"].ToString(); 

string t = ShowNode.DataKeys[ShowNode.SelectedRow.RowIndex]["Type"].ToString();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.