Showing posts with label run. Show all posts
Showing posts with label run. Show all posts

Saturday, March 31, 2012

table run at server

I want to have an table like this
<table>
if session("userid") <> "" then
<tr><td><droplist></td></tr>
else
<tr><td><hiddeninput></td></tr>
end If

Anybody know how to solve that in .NET and codebehind?Hi,

HTML
=====


<TABLE id="Table1" border="0">
<TR>
<TD>
<asp:DropDownList id="DropDownList1" runat="server">
<asp:ListItem Value="BlahBlah">BlahBlah</asp:ListItem>
</asp:DropDownList>
</TD>
</TR>
</TABLE>

code-behind
===========


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

If Session("userid") <> "" Then
DropDownList1.Visible = True
Else
DropDownList1.Visible = False
End If

End Sub

HTH
Thanks, but i asked for the tablerow, cause I have labels and so on that don't is supposed to been shown either.

I solved it surfing around like. (My Row that is going to be hidden is Row #3.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Session("userid") <> "" Then
Table1.Rows(3).Visible = True
Else
Table1.Rows(3)..Visible = False
End If
End Sub

table that i add at run time, is in the end of page

I"m using this code to add at run time:


Dim maintable As New Table()

Dim row As New TableRow()
Dim cell As New TableCell()
Dim heights As New Unit("200px")
Dim place As New PlaceHolder()
Dim mycontrol As UserControl = LoadControl("smallmenu.ascx")

maintable.Height = heights
maintable.Width = heights

cell.Height = heights
cell.Text = "yaniv"
cell.Controls.Add(mycontrol)

row.Height = heights

row.Cells.Add(cell)

maintable.Rows.Add(row)
maintable.CssClass = "maintable"

Page.Controls.Add(maintable)

place.Controls.Add(mycontrol)

the problem is that the new table sits in the end of the (after the </html> tag) so i get an error massege:
"Control '_ctl2_Button1' of type 'Button' must be placed inside a form tag with runat=server.
"

how can solve that problem?You can use

<asp:table id="maintable" runat="server" width=... height=.. ... > </asp:Table
in your aspx page rather than dynamically creating a maintable in the codebehind.

Use the codebehind to just populate the table rows and columns.

This way you can place the <asp:table... ></asp:Table> wherever you want on the aspx page.

Hope this helps.
i don't want to use the aspx page, because i want this table to inherit to all my other pages...
I dont understand...

If you want to embed the table in all your aspx pages, you can still add an empty asp Table tag like

<asp:Table id="mytable" runat="server"... ...></asp:Table> to every aspx page where you want the table to be displayed.

Now you can still dynamically populate this table from code behind...
sorry to bother, i tried thatone, but the code behind dosen't recognize this table in i add it in the aspx page. i can't run the "maintable.controls.add" in this case. even if i use the dim maintable, and don't you the page.controls.add(maintable) there is no connection between them!
I had the same problem. I asked about it yesterday (only about a DDL).

My kloogy solution was to create a wrapper class:

Public Class ItemPair
Inherits DropDownList
Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
output.RenderBeginTag("form")
MyBase.Render(output)
End Sub
Protected Overrides Sub AddAttributesToRender(ByVal writer As HtmlTextWriter)
writer.RenderEndTag()
End Sub
End Class

You could probably cut and paste and change a couple words and be set.
I posted another asnwer that hasn't shown up yet but here is the link to my question which was very similar to yours:

http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=516165