Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make invisible a datagrid item 1

Status
Not open for further replies.

wsmall73

Programmer
May 23, 2002
261
0
0
US
Based on level of the user I would like to disable or make invisible a hyperlink to another page...

<ItemTemplate>
<TABLE><TR><TD>
<asp:HyperLink ID=&quot;mylink&quot; ageUrl=&quot;images/arrow_right.gif&quot; NavigateUrl='<%#&quot;ReviewIt.aspx?task_no=&quot;+ DataBinder.Eval(Container.DataItem,&quot;task_no&quot;)+&quot;&location_no=&quot;+DataBinder.Eval(Container.DataItem,&quot;location_no&quot;)%>' Target=&quot;_blank&quot; Runat=&quot;server&quot; Width=&quot;37&quot; />
</TD></TR></TABLE>
</ItemTemplate>

I have tried to walk the object model with little luck.

HyperLink myhyper = new HyperLink();
myhyper=(HyperLink) DataGrid1.Controls[0].Controls[1].Controls[0].Controls[1];
myhyper.Visible=false;

Can anyone point me in the right direction... and should I code this in the itemcreated event or can it really be called from anywhere...
thanks in advance.... -wayno
 
You'll need to do it in the itemDataBound event in the datagrid like so:

((HyperLink)e.Item.FindControl(&quot;mylink&quot;)).Visible = False;

Here's a great article on the datagrid. I'd highly suggest reading it in its entirety. But specifically, check out the details on the onItemDataBound event, and how to use the syntax that I've described above:

thread855-447401

:)
paul
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
Thanks for pointing me to that article... I had actually touched on it a few months back when they were on article 9 and your right it is a great piece on the datagrid....

I have also quickly realized that you are the man in this house... as this is the 2nd thing that you have assisted me with in recent weeks. Thanks for taking time out to assist those of us on the short bus... =)
 
I tried your solution as well as all of these (stealing shamelessly from the article)
DataGrid1.Items[e.Item.ItemIndex].Cells[0].FindControl(&quot;mylink&quot;).Visible=false;
DataGrid1.Items[e.Item.ItemIndex].Cells[1].FindControl(&quot;mylink&quot;).Visible=false;
DataGrid1.Items[e.Item.ItemIndex].Cells[2].FindControl(&quot;mylink&quot;).Visible=false;

e.Item.FindControl(&quot;mylink&quot;).Visible=false;

without figuring out what is that I am doing I keep getting the error that the index is out of range... or System.NullReferenceException: Object reference not set to an instance of an object.


any ideas??
 
What context are you trying to use those statements? Can I see your method declaration and any pertinent surrounding code?

-paul
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
We've completed this functionality by putting the element you would like conditionally hidden in a named asp table, then showing or hiding the table.

I've also used the CType function to get at a detail datagrid in a cell to show or hide it as follows:
CType(e.Item.Cells(3).Controls(3), DataGrid).visible = not CType(e.Item.Cells(3).Controls(3), DataGrid).visible
Possibly you could convert yours to a hyperlink.

It should still be done in the itemDataBound event

Terry
 
I am using this in the itemdatabound event....

private void DataGrid1_ItemDataBound(object sender,System.Web.UI.WebControls.DataGridItemEventArgs e)
{
((HyperLink)e.Item.FindControl(&quot;mylink&quot;)).Visible = False;

}

the event handler...looks like...
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);

the pertinent client code is above...(previous post)

below is essentially the rest of the code behind server code.

if (!IsPostBack)
{ BindGrid();}

private void BindGrid(int location_no)
{
//Create the SQL Command Object....
SqlCommand myCommand = new SqlCommand(&quot;sp_getTasks&quot;, myConnection);

//Indicate the command is a Stored Procedure...
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.Add(&quot;@location_no&quot;,SqlDbType.Int).Value=location_no;

// Create SqlDataAdapter...
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);

// Fill DataSet...
myAdapter.Fill(myDataSet,&quot;Tasks&quot;);

DataGrid1.DataSource=myDataSet;
DataGrid1.DataMember=&quot;Tasks&quot;;
DataGrid1.DataBind();

}
 
Maybe you haven't switched on the item type. You may be trying to find that control in the header, which is the first time that event is called. Try modifying your code to this:

switch(e.Item.ItemType){
case ListItemType.Header:
//this is where the code would bomb
break;
case ListItemType.Item:
//your code should work fine here
break;
case ListItemType.AlternatingItem:
//your code should also work fine here
break;
}

That's all I see. It has to be it. Everything else looks pretty solid.

hope it works it out for you.

-paul
penny1.gif
penny1.gif

The answer to getting answered -- faq855-2992
 
Yeah that did it... you have no idea how much I appreciate all your help with this. It was seriously beginning to give me greys.. =)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top