anpalmer Posted October 6, 2004 Posted October 6, 2004 Hi, Does anyone know if it is possible to bind data directly to a label? I want to retrieve some information from an SQL database and bind this to a label (writing in vb by the way) so the information can be used to control things within the website. I have a series of user controls that I want only to be visible if a certain value (retrieved from the sql database) is equal to a certain value. This is essential to the site as it will prevent people from attempting to do things they should not be able to do. I have all the if statements ready within my application logic to control what I have described, the only thing I am missing is the data. I have tried binding to a datalist with a label embedded in it, this works fine but then the label becomes inaccessible to any thing else within the webpage. I have also tried creating a global variable, but again the problem is assigning the value retrieved from the database to the global variable. Any ideas anyone? :-\ Quote
eramgarden Posted October 7, 2004 Posted October 7, 2004 Do you have to use a label? can you use a textbox, then make the textbox readonly with no border? Quote
anpalmer Posted October 7, 2004 Author Posted October 7, 2004 I can use either, I only picked Label as it was the first thing that came to my head, a textbox would be fine, as I would set the visibility property to "False" anyway. Do you know how to bind to a textbox? If you did that would be fantastic. Cheers Ash Quote
eramgarden Posted October 8, 2004 Posted October 8, 2004 can you do something like this? <asp:TextBox id="ext1" Width="80px" Runat="server" MaxLength=8 Text='<%# Container.DataItem("phone1_ext")%>' > </asp:TextBox> [/code? Quote
anpalmer Posted October 9, 2004 Author Posted October 9, 2004 Thanks for the reply, I will give it a try and let you know Quote
anpalmer Posted October 9, 2004 Author Posted October 9, 2004 I have tried your method in several ways and unfortunately I still have the same problem. I followed your code and had: <asp:TextBox id="ext1" Width="80px" Runat="server" MaxLength=8 Text='<%# Container.DataItem("expression")%>' > But to get this to work I had to have it within <asp:repeater> tags. As soon as it is inside the repeater tag the page displays the data within the text box, but when I then try and assign the text box to a label or a variable: Dim VariableX As Integer / or Dim VariableX As String VariableX = ext1.Text I get: Object reference not set to an instance of an object. I am quite stuck on this one, and even though I have got round the initial problem by a long-winded and inefficient method of filling another dataset with data and counting the number of results and making conditional statements dependant on the result, I still want to find out how to bind data to a label and then extract that data and use it throughout my applications. I am sure it can be done, I just can't quite get my head around it yet. Any suggestions? Ash Quote
eramgarden Posted October 10, 2004 Posted October 10, 2004 Yeah, if it's inside a repeater, you cant reference it like that.. This is how I did it when with a repeater; Dim DisplayNotesValue As Label = CType(rpAct22.Items(i).FindControl("lblDisplayNotes"), WebControls.Label) with rpAct22 being the name of the repeater. lblDisplayNotes name of the label. Or for textbox: Dim InternalNotesBox As TextBox = CType(rpAct22.Items(i).FindControl("txtInternalNotes"), WebControls.TextBox) Is this what you want to do? Quote
eramgarden Posted October 10, 2004 Posted October 10, 2004 I forgot, i have those inside: For i = 0 To rpAct22.Items.Count - 1 'code i posted earlier next Quote
anpalmer Posted October 12, 2004 Author Posted October 12, 2004 Sorry have been offline for a few days, I will try this and let you know Thanks :) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.