Link to home
Start Free TrialLog in
Avatar of champ_010
champ_010

asked on

Problem DataBinding to <Asp:TextBox>

I am having trouble seeing the textbox when I bind data to it to create the ID dynamically:

<asp:TextBox ID="<%#DataBinder.Eval(Container.DataItem,"DetailID")%>" runat="server"  OnTextChanged="TextChanged" />

Doesn't show a textbox where user can type something. it just shows a space where the textbox should appear. If I type: <input type="text" id="<%#DataBinder.Eval(Container.DataItem,"DetailID")%>"> the textbox does show with the correct ID but I can't use OnTextChanged.

I've experimented just typing in a string value into <asp:TextBox ID="testID"> which produces textboxes but an error will show if I typed <asp:TextBox ID="1">.  

What to do?
Avatar of boulder_bum
boulder_bum

Can you just set the ID in the code-behind instead of with the markup syntax (or even create the whole TextBox on the fly in the code-behind)?
Avatar of champ_010

ASKER

Can you show me how to do either of the suggested?  Currently my TextBox is in a DataRepeater with the id coming from the database. If the TextBoxes can be created on the fly that sounds like it would work for me too--maybe even better.  
 while (dr3.Read())
  {
  dID=dr3[0].ToString();
  dName=dr3[1].ToString();
  } while (dr3.NextResult());

dID would be the ID of the TextBox.
champ,

All you need to do is name the textbox with a latin (english) character before the number.

For example, "5" can produce unexpected results, but "t5" will work fine.

To retrieve the number later, just use Convert.ToInt32( textbox.ID.SubString( 1 )).
So:

dID= "t" + dr3[0].ToString();
ASKER CERTIFIED SOLUTION
Avatar of boulder_bum
boulder_bum

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Thanks,

I don't know how to use the ItemDataBound EventHandler and find it a bit confusing right now so I tried shovavnik's example with dID= "t" + dr3[0].ToString();

 SqlDataReader dr3=cmd3.ExecuteReader();
 dr3.Read();
 
 while (dr3.Read())
  {
  dID= "t" + dr3[0].ToString();
 
 myDataReader.DataSource=dr3;
 myDataReader.DataBind();
 }
 dr3.Close();

...but wasn't sure where or how to put dID into the textbox:

<%#DataBinder.Eval(Container.DataItem,"DetailName")%>

is obviously wrong because I get an error.  So I tried type the "t" in before the DataBinder.Eval to get the latin english character:

<asp:TextBox ID="t<%#DataBinder.Eval(Container.DataItem,"DetailName")%>" runat="server"/>

When I view source I see:  

<asp:Textbox ID="t1" runat="server">
<asp:TextBox ID="t2" runat="server"> etc.  

The ID seems to be as it should but the textboxes still don't show up.  There seems to be a problem with using DataBinder.Eval--the <asp:Textboxes> are not being converted to <input type="text">.  

When I type in fixed IDs:

<asp:TextBox ID="t1" runat="server"/>
<asp:TextBox ID="t2" runat="server"/>

the textboxes do show up and the <asp:TextBox> gets converted to <input type="text">

So I guess I need to get rid of using DataBinder.Eval.  Can I create textboxes inside the while loop and then display them in the form somehow?:

while (dr3.Read())
  {
  dID= "t" + dr3[0].ToString();
 
 myDataReader.DataSource=dr3;
 myDataReader.DataBind();
 }
sorry, I'm cutting and pasting wrong I meant

<asp:TextBox ID="t<%#DataBinder.Eval(Container.DataItem,"detailID")%>" runat="server"/>
Hey, I think I got the textbox problem solved.  
The reason why I used a Panel was because the textboxes don't show until the subcategoryID has been chosen from a dropdownlist. Seems to work well here.

Now I just have to loop through them somehow to retrieve the ID again and input the entered text for INSERT.


---- Solution:-------

 while (dr3.Read())
  {
   Label tLabel=new Label();
   tLabel.Text="<br>"+dr3[1].ToString()+"<br>";
   tLabel.CssClass="h4_666666";
   
   TextBox tBox=new TextBox();
   tBox.ID=dr3[0].ToString();
   tBox.CssClass="textfield_grey";
   tBox.Columns=35;
   
   form1.Controls.Add(tLabel);
   form1.Controls.Add(tBox);
   myPanel.Controls.Add(tLabel);
   myPanel.Controls.Add(tBox);
 }

I've asked Community Suport to award half the points (100) to boulder_bum for giving me a hint about doing it in the code behind on the fly.  I was hoping you would show me how to do it--as requested in my follow up comment, but you suggested something else instead which I found a little too advanced for me. I finally figured out how to do it in the code the following day and have posted it in my latest comment.  

If anyone thinks it is unfair for me to award partial points and think I should still award full points to boulder_bum for the suggestion without follow up example as requested, please let me know and I will reconsider.

Thanks.