: I have created a dataGrid on a web form using VB.Net. Here is what I am trying to do...
: The first column in the dataGrid I have created an ItemTemplate. What I would like for it to do is either display a textbox or a label. Basically, if there is a name in the recordset I pulled from my stored procedure, print it out. If not, I want a textbox to display with the text Vacant Slot. I am getting it to either print the name or the words "Vacant Slot" but I can't get it to show up in a textbox. Any suggestions?? Thanks in advance.
: this is from my html page.....
: <asp:TemplateColumn SortExpression="Name" HeaderText="Name">
: <ItemTemplate> <%# RenderNameField(Databinder.Eval (Container, "DataItem.vacancy_flag"),Databinder.Eval
: (Container, "DataItem.Name")) %>
: this is from my codebehind page......
: Function RenderNameField(ByVal sVacantFlag As String, ByVal sText As
: String) As String
: If Not Convert.ToBoolean(Byte.Parse(sVacantFlag)) Then
: Return "<asp:Label runat=server Text=""" & sText & """>" & sText & "</asp:Label>"
: Return "<asp:TextBox runat=server ID=txtName width=10px>Vacant Slot</asp:TextBox>"
: End If
: End Function
It wont work like that. You are trying to dynamically build the string for the control, however, by the time that sting prints, the control rendering engine has long executed. It's kind of like the chicken before the egg scenario.
Your best bet is to place the two controls in the Item Template and set the visibility of either conntrol to false, depending on your scenario ...
myTextBox.Visible = "False"