Saturday, March 31, 2012

Text box looping question.

I can do this in regular asp, but i'm a little confused in .net.

There are 5 records in my table. companyid and companyname fields.

If I want to display all five records but as editable textboxes. So each textbox has the companyid from table and companyname as the text value.

Then if they hit save -- it loops through and saves all values in textboxes to the table based on the id.

Do I do put the textboxes inside a repeater?Or do I put the textboxes inside a datalist and do it that way. I'm not sure about the saving procedure.
I think you can use a repeater,

try this (this should definitely work :)):


<asp:Repeater id="myRptr" runat="server">
<ItemTemplate>
<input type=text id='<%# DataBinder.Eval(Container.DataItem, "companyid ") %>'
value='<%# DataBinder.Eval(Container.DataItem, "companyname") %>'>
</ItemTemplate>
</asp:Repeater>

It didn't like that ('<%# DataBinder.Eval(Container, "DataItem.company_id") %>' is not a valid identifier.) so I tried:

<asp:TextBox id="txtName" runat="server" text='<%# DataBinder.Eval(Container, "DataItem.company_name") %>'></asp:TextBox>
<asp:Label ID="lblName" Runat="server" text=<%# DataBinder.Eval(Container, "DataItem.company_id") %>></asp:Label
Then in codebehind
Dim j As RepeaterItem
For Each j In rptName.Items
Response.Write(DirectCast(j.FindControl("txtName"), TextBox).Text & "<br>")
Response.Write(DirectCast(j.FindControl("lblName"), Label).Text & "<br>")
Next

That gives me the companyname and id. Then I can just use update statement in the [for...next] and update.
I tried this way instead and it seems to work.

<asp:TextBox id="txtName" runat="server" text='<%# DataBinder.Eval(Container, "DataItem.company_name") %>'></asp:TextBox>
<asp:Label ID="lblName" Runat="server" text=<%# DataBinder.Eval(Container, "DataItem.company_id") %>></asp:Label
Dim j As RepeaterItem
For Each j In rptName.Items
Response.Write(DirectCast(j.FindControl("txtName"), TextBox).Text & "<br>")
Response.Write(DirectCast(j.FindControl("lblName"), Label).Text & "<br>")
Next
Then I just use update statement inside [for ... next]
You can use either... Repeater is a lighter control though...

All the form data will be post back (there is no looping concept)

You can read a very good MSDN article on control choice here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/aspnet-whenusedatawebcontrols.asp

Good luck :)
I used:


<asp:TextBox id="txtName" runat="server" text='<%# DataBinder.Eval(Container, "DataItem.company_name") %>'></asp:TextBox>
<asp:Label ID="lblName" Runat="server" text=<%# DataBinder.Eval(Container, "DataItem.company_id") %>></asp:Label>

Behind file


Dim j As RepeaterItem
For Each j In rptName.Items
Response.Write(DirectCast(j.FindControl("txtName"), TextBox).Text & "<br>")
Response.Write(DirectCast(j.FindControl("lblName"), Label).Text & "<br>")
Next

I dont know why you have to do this...

I tested this code piece before i posted it and it works fine with me I do not have to use the DirectCast and all that...

Simple '<%# DataBinder.Eval(Container, "DataItem.company_id") %>' works !!!

Did you databind the repeater to a datasource in the Page_Load ?
What does your [for ...next] loop look like?
I tried it with <asp:textbox not with a regular input box like you have. Sorry. So it will work the way you said.
I call this after I hit save to get the values:

Dim j As RepeaterItem
For Each j In rptName.Items
Response.Write(DirectCast(j.FindControl("txtName"), TextBox).Text & "<br>")
Response.Write(DirectCast(j.FindControl("lblName"), Label).Text & "<br>")
Next

How would you get the values using your way?
Oh yes... about the code behind... you will have to do the FindControl...

I am not sure why you are using 'DirectCast(j.FindControl("txtName")... ' what if you use
this.FindControl("txtName")

I might be wrong... cause I am not sure why you need the DirectCast function...

Let me know

0 comments:

Post a Comment