• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

Setting .text value for a label/literal on Page_Load inside a datagrid template?

Hello, I'd like to put some labels inside a datagrid template and have the .text property of the labels assigned in the Page_Load method. So far I have some code but it's not quite right. Is this possible? Can anyone please help me find the solution?

Here's what I've got so far.

  Public Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)

            e.Item.FindControl("ltlPhone").Text = xPathQuery("//search/searchresultsphone", "profile.xml")
  End Sub

---------

<ItemTemplate>
    <asp:literal id="ltlPhone" runat="server" />
</ItemTemplate>

-------------

I know i'm approaching this wrong, but I'm having trouble searching for an answer. Thank you for your time.
0
sneidig
Asked:
sneidig
  • 5
  • 4
2 Solutions
 
raterusCommented:
It would be done in the ItemDataBound event the DataGrid fires, not page.load
0
 
sneidigAuthor Commented:
Thanks for your response. That makes sense.

When I try to compile I get a 'Text' is not a member of System.Web.UI.Control error.
e.Item.FindControl("ltlPhone").Text = xPathQuery("//search/searchresultsphone", "profile.xml")

This sort of makes sense to me. I tried creating a literal object, assigning it's text property a value, and then assiging it to e.Item.FindControl("phone") like this,

        Dim lit As Literal
        lit.Text = "this is a string"
        e.Item.FindControl("ltlPhone") = lit

But that isn't exactly working either. Can you offer any more guidance, or point me in the right direction?

Thanks.
0
 
raterusCommented:
Right, your first problem is something known as "late-binding", you can look up a better explanation than I'd provide, but the best fix is to create an object exactly like what you want, try this and it will work.

Dim lit as Literal = e.Item.FindControl("litPhone")
lit.Text = "Something"
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
laotzi2000Commented:
CType(e.Item.FindControl("ltlPhone"), Literal).Text = xPathQuery("//search/searchresultsphone", "profile.xml")
0
 
raterusCommented:
Well...If you really want the fastest, one line wonder to do the job, try this:
DirectCast(e.Item.Controls(0), Literal).Text = xPathQuery("//search/searchresultsphone", "profile.xml")

You take a performance hit with CType and FindControl so this is the most direct solution, although ugly!  You may need to play with the e.item.controls(0) to get the approprate index.
0
 
sneidigAuthor Commented:
OK, I see that I need to specify that the control is a literal before I can access the text property.

Though I'm still getting an object reference not set to an instance of object error.

Could it be that I need to alter the control in html  from <asp:literal id="ltlPhone" runat="server" /> to something else?
0
 
sneidigAuthor Commented:
The CType solution offered by laotzi2000 is what returns the "object reference not set to instance..." error.

The DirectCast solution from raterus returns a "specified cast not valid" error. That literal is the only control on in the template right now, so I'm pretty sure the index is 0. Plus if I change the index value 1, it tells me the index is out of range. So I'm pretty sure it is 0.

If that helps you guys out at all. Thank you, this has been helpful.
0
 
raterusCommented:
try this in ItemDataBound, this says to only run the code for actual items, not any headers or footers.

If e.item.itemtype <> listitemtype.header orelse e.item.itemtype <> listitemtype.footer then
  'put stuff you currently have in ItemDataBound here.
end if
0
 
raterusCommented:
sorry, this is correct

If e.item.itemtype <> listitemtype.header AndAlso e.item.itemtype <> listitemtype.footer then
  'put stuff you currently have in ItemDataBound here.
end if
0
 
sneidigAuthor Commented:
Wow, that works.

This is what I ended up with:
If e.item.itemtype <> listitemtype.header AndAlso e.item.itemtype <> listitemtype.footer then    
        DirectCast(e.Item.FindControl("ltlPhone"), Literal).Text = xPathQuery("//adobe/search/searchresultsphone", "profile.xml")
End If

I may put some more effort into figuring out if I can get the e.Item.Controls(0) solution to work, considering I see how knowing where the control would be faster than having to find it. But for now it's throwing errors.

Thank you both for you time and help. I'm going to split up the points.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now