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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rendering controls 1

Status
Not open for further replies.

tembalena

Programmer
Apr 17, 2001
37
ZA
I'm creating an html table using stringbuilder
eg.
Dim sTable as New StringBuilder
sTable.Append(&quot;<table><tr><td>&quot;)
sTable.Append(&quot;</td></tr></table>&quot;)

I need to put an <asp:textbox></asp:textbox> control in this table's cell. How do I do this?

The reason why I'm using stringbuilder is to set the text attribute of a label = stable.tostring in order to output the table in an exact spot.

Am I completely off the mark here in trying to output html and controls?

Help would be greatly appreciated.
Thanks.
 
Use a Table control to build your table and a panel control to put the table in. Just remember that this is all object oriented and I am sure you'll have no trouble understanging. The following code creates a table with one row and two columns with a text box in the second column and a title in the first.


Table myTable = new Table();
TableRow myRow;
TableCell myCell;
TextBox myBox = new TextBox();

//create instances of the row and 1st cell
myRow = new TableRow();
myCell = new TableCell();

//set the properties for the 1st cell
myCell.Text = &quot;Title&quot;;
myCell.ID = &quot;TitleCell&quot;;

//add the cell into the row
myRow.Cells.Add(myCell);

//create and instance of the 2nd cell
myCell = new TableCell();

//set the properties for the text box and 2nd cell
myBox.ID = &quot;txtTest&quot;;
myBox.Text = &quot;This is my text box&quot;;
myCell.ID = &quot;Cell2&quot;;

//add the text box to the cell
myCell.Controls.Add(myBox);

//add the cell to the row
myRow.Cells.Add(myCell);

//add the row to the table
myTable.Rows.Add(myRow);

//add the table to the panel on the page
pnlHolder.Controls.Add(myTable);


If you want gridlines simply set the border width property of the table before you add it to the panel control.

Fairly simply, if you need to access the text box later on, it won't exist in a variable. You'll need to get it from the controls of the cell it is in then reference it by variable. If you need help with this as well post and I'll see what I can do.

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Thanks so much Mark, that's exactly what I was trying to do. I do find it tough to get my head around the object oriented way of thinking.

If you would let me know how to access the control's value from the cell, as I'm going to have many of the same controls repeated in different tables and will need to figure out which control of which table has what value.

Thanks again - it must be so great to have this all so figured...
 
Glad I could help.

Just try to remember that everything is an object with properties and methods. If you took and Orange it has the properties of color and shape. A method for the orange could be peel.

With objects they may have properties that are in themselves objects with their own properties and methods. In the case of the Table it has a property of a collection of Rows. The rows themselves have a method called Add which accepts a parameter of type cell.

Hope I cleared things up a bit and didn't muddy them instead. Just remember that most controls(objects) in .NET have a &quot;controls&quot; property. You can put other controls into this property. Like I showed using the Cell and the Panel.

Sorry to ramble on so long I just hope that I am clearing it up enough so that you can understand how I am now going to get the control and access it.

I started to write a recusive function for you that would find the text box in the page but it seems microsoft already has one. Very simple really, you just need to add a few checks so your code won't crash.


TextBox mytextbox;
Control temp = FindControl(&quot;txtTest&quot;);

if (temp is TextBox)
{
mytextbox = (TextBox) temp;
mytextbox.Text = &quot;Found it&quot;;
}
else
{
//this will occur if the control isn't found
}


The above code finds the control that has the ID of &quot;txtTest&quot;. It then casts that generic control into a textbox after which you can access it like a textbox that you created at design time rather than at run time. Just remember to name the textbox Id's uniquely.

Once again sorry for the speech but I am hoping it helped you understand the OO way a bit more.

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
I really appreciate the time you take to explain things to the simpleton like me. Thanks, it does make sense, clears it up, and willdefinitely help in what I'm trying to do. I'm sure I'll be back with more though, so until then, again, thank you.
 
Hi, me again.
I've got a web usercontrol and wish to use it a few times on a page.

I realise I can't drag it onto my page and use:
Protected WithEvents myControl as MyUserControl
because I need it to be named differently for each instance of it, right?

So how would I create/access this usercontrol repeatedly, giving it a different name each time in order to access it's properties which will differ per instance. (not sure if I'm using the correct terminology so hope not confusing)

Thanks
 
I've finally figured the previous question out, but if you could help me with the following:

I've the following code that dynamically creates controls per record found (while sreader.read).

Now that I've got my controls on the page, I'm struggling to access their properties. Any ideas?

Dim holderTable As New Table()

Dim myRow As TableRow
myRow = New TableRow()

While sReader.Read

'New cell to table per record
Dim myCell As TableCell
myCell = New TableCell()

iMarkup = sReader(&quot;markup&quot;)
iPrice = sReader(&quot;price&quot;)

'Create user control
Dim PartPricing1 As PartPricing = CType(Page.LoadControl(&quot;~/Controls/PartPricing.ascx&quot;), PartPricing)

PartPricing1.ID = &quot;partPricing_&quot; & CStr(iPartsPricingCount)

'Populate usercontrol properties
PartPricing1.Price = iPrice
PartPricing1.MarkupPercentage = iMarkup

'Add user control to the cell
myCell.Controls.Add(PartPricing1)
myCell.ID = &quot;cell_&quot; & iPartsPricingCount

'Add the cell to the row
myRow.Cells.Add(myCell)

End While

'Add the row to the table
holderTable.Rows.Add(myRow)
 
Not sure what exactly the problem your having is but I may have a few suggestions. I am guessing that the properties you are setting you've defined yourself. Make sure that you actually define the properties as public. If your unsure of this checkout
Another thing to remember is that unless you specifically do something to save the data it will not persist over post backs. One thing to do is to save data in the viewstate like the MS controls do. Another option is session variables (I don't recommend this). Or if persisting your data is not required just leave it be and reset the properties when you need to.

If you still can't get it to work, I'll need to see the code you have for your user control.

Hope that helps.
If I have been helpful please mark the thread as a helpful one so that others in the forum will know that they can make use of it.

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Yes please, more help if you can...

What I'm doing loop through records and create a usercontrol per loop. The usercontrol is a table containing textboxes and labels.
The textbox values (usercontrol properties) will be changed by the user, after which they click a button. I need to update those values in the user control as well as save them to the dbase.
I have no idea how to get each control's property values once they've been updated, and keep the controls visible (viewstate as you suggest).

So here follows the code. Thanks Mark.

'Usercontrol PartPricing.ascx.vb
'----
Public MustInherit Class PartPricing
Inherits System.Web.UI.UserControl
Protected WithEvents txtMarkup As System.Web.UI.WebControls.TextBox
Protected WithEvents txtPrice As System.Web.UI.WebControls.TextBox

#Region &quot; Web Form Designer Generated Code &quot;

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

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

End Sub

Public Property Price() As Decimal
Get
Return txtPrice.Text
End Get
Set(ByVal Value As Decimal)
Try
txtPrice.Text = Value
Catch
End Try
End Set
End Property

Public Property MarkupPercentage() As Decimal
Get
Return txtMarkup.Text
End Get
Set(ByVal Value As Decimal)
Try
txtMarkup.Text = Value
Catch
End Try
End Set
End Property

End Class

'----
.aspx page
'----
Public Class TestPricing
Inherits System.Web.UI.Page

Protected WithEvents btnUpdate As System.Web.UI.WebControls.Button
Protected WithEvents panelPricing As System.Web.UI.WebControls.Panel

Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
InitializeComponent()
End Sub

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

GetPartPricingDetails()

End Sub

Private Sub GetPartPricingDetails()

Dim iPartsPricingCount As Integer

'Create a holding table to hold pricing parts tables
Dim holderTable As New Table()

Dim myRow As TableRow
'Create instances of the row
myRow = New TableRow()

'Get part pricing details
Dim sCustomer As New Customers()
Dim sReader As SqlDataReader
sReader = sCustomer.GetQuotePartPricingDetails(CInt(lblPartDetailsID.Text), lblPartID.Text)

Dim iMarkup, iPrice

While sReader.Read

'Create a new table cell
Dim myCell As TableCell
myCell = New TableCell()

iMarkup = sReader(&quot;percentage_markup&quot;)
iPrice = sReader(&quot;price&quot;)

Dim PartPricing1 As PartPricing = CType(Page.LoadControl(&quot;~/Controls/PartPricing.ascx&quot;), PartPricing)

PartPricing1.ID = &quot;partPricing_&quot; & CStr(iPartsPricingCount)
PartPricing1.Price = iPrice
PartPricing1.MarkupPercentage = iMarkup

'Add partpricing user control to the cell
myCell.Controls.Add(PartPricing1)
myCell.ID = &quot;cell_&quot; & iPartsPricingCount

'Add the cell to the row
myRow.Cells.Add(myCell)

iPartsPricingCount += 1

End While

'Add the row to the table
holderTable.Rows.Add(myRow)

panelPricing.Controls.Add(holderTable)

End Sub
End Class
 
K this one may take me a bit cause I am fairly busy today but I'll get back to you as soon as I can

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
So do you have a button for each user control or just one on the page?

Cause if it's just one then I ask what your user control is doing and if it needs to be a user control (though it should work they way you have it I think). If there is a button for each control then put the database save code into the event handler for the buttons.

Awaiting your reply

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
I have one button. A user control is loaded per record, it holds pricing details for a part and there could be a few different pricing formulas for one part, therefore a few usercontrols for one part. The user changes the data in the textboxes of the user controls and clicks the one button to update the records.
Should I rather be creating a table per record instead of using user controls?
 
I've just tried to dynamically create a table per record (instead of a user control which holds the table), but the same happens when I click a button, the entire table disappears even though I've got enableviewstate enabled on the table. Help! What should I do now?
 
Well, finally it's sorted. I had to create the dynamic controls in the Page_Init(). And it's working!
 
glad you've got it going.

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top