Solved

Output Text from Textbox - e.Item.FindControl

Posted on 2004-09-23
13
402 Views
Last Modified: 2008-03-03
Hi, I'm trying to get the text from my textbox so I can update my DataTable but I can't even print out the text--yet there are no errors.  How do I get the textbox text?

void UpdateQty(Object Sender, DataGridCommandEventArgs e)
{
         tblCart=(DataTable)Session["dgCart"];
             
         string myText=((TextBox)e.Item.FindControl("txtQty")).Text;
         Response.Write(myText); //--------------NO TEXT PRINTS OUT!---------

         tblCart.Rows[e.Item.ItemIndex]["Quantity"]=myText;
 
         Session["dgCart"]=tblCart;

        BindData();
}
<asp:TemplateColumn HeaderText="Qty">
      <ItemTemplate>
      <asp:TextBox ID="txtQty"  Text='<%#DataBinder.Eval(Container.DataItem,"Quantity")%>' runat="server"/>
      </ItemTemplate>
</asp:TemplateColumn>
0
Comment
Question by:champ_010
  • 6
  • 3
  • 2
  • +2
13 Comments
 
LVL 15

Expert Comment

by:praneetha
ID: 12137284
when r u calling this(from where)

void UpdateQty(Object Sender, DataGridCommandEventArgs e)

void updateqty()
{
foreach(datagriditem dg in datagrid1.items) // loop theu all the rows
{
 string myText=((TextBox)e.Item.FindControl("txtQty")).Text;
 Response.write(nyText);
}
}
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12137569
string myText = ((TextBox)e.Item.Cells(3).Controls(0)).Text;

try using the ordinal instead

Regards,

Aeros
0
 
LVL 1

Author Comment

by:champ_010
ID: 12137813
Hi I still get no output even when using the ordinal but I don't get errors. In fact, when I press the button for the OnUpdateCommand it just clears the textboxes so there isn't even the original number inside.

Sorry I didn't show the button:

<asp:DataGrid ID="cartGrid"
    AutoGenerateColumns="false"
    OnDeleteCommand="DeleteItem"
    OnUpdateCommand="UpdateQty"
    runat="server">

    <Columns>
       <asp:TemplateColumn HeaderText="Qty">
      <ItemTemplate>
      <asp:TextBox ID="txtQty" Text='<%#DataBinder.Eval(Container.DataItem,"Quantity")%>' runat="server"/>
      </ItemTemplate>
     </asp:TemplateColumn>

    <asp:ButtonColumn ButtonType="LinkButton"  CommandName="Update" Text="Update Qty"/>

   <asp:ButtonColumn ButtonType="LinkButton"  CommandName="Delete" Text="Remove"/>

</Columns>
</asp:DataGrid>
0
 
LVL 1

Author Comment

by:champ_010
ID: 12137840

Just to clarify:

string myText = ((TextBox)e.Item.Cells(3).Controls(0)).Text;

e.Item.Cells(3) -- is the third column of my datagrid?
.Contorls(0) -- is the first WebControl?

(I have more columns but didn't include them in code above to keep it simple).
0
 
LVL 5

Expert Comment

by:tomasX2
ID: 12138532
e.Item.Cells(3) would be the 4th column. e.Item.Cells(2) is the third - zero based.
0
 
LVL 1

Author Comment

by:champ_010
ID: 12138637
Oopps yes, but that wasn't what was wrong...

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Expert Comment

by:praneetha
ID: 12138679
update command

fres

private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
            {
            so here you shoul dhave

this.updateqty(e);
            }

is that how u have it
0
 
LVL 10

Accepted Solution

by:
jnhorst earned 100 total points
ID: 12138683
Think in terms of what HTML the various web controls send to the browser.  A datagrid, for example, will send a <table> with rows and cells.  In your ItemTemplate, you are showing the data in a textbox.  So in HTML, there will be a <td> tag that corrsponds to the column, inside each <tr>  tag that corresponds to a row.  If you put a label in your ItemTemplate, that label will render as a <span> tag inside the <td></td> tag.  So when you do e.Item.Cells(3).Controls(...) you are referring to whatever controls are embedded in the column.  In your case, since you have named your textbox, you would be better off using e.Item.Cells(3).FindControl("txtQty"), which you cast to a TextBox class and get the text.

John
0
 
LVL 1

Author Comment

by:champ_010
ID: 12149293

O.k. that makes sense John.  So I'm still back at square one:

string myText=((TextBox)e.Item.Cells[3].FindControl("txtQty")).Text;
Request.Write(myText);

Still doesn't print the text, but doesn't give any errors either.

I'm trying to get whatever number the user types into the textbox as the new quantity in my shopping cart. I'm only guessing that this is how I should do it.  I can't find any good examples of this....
0
 
LVL 10

Expert Comment

by:jnhorst
ID: 12149376
I think your UpdateQty routine is not wired up right.  View the web form in design view.  Show the Properties of the data grid.  At the top of the Properties window is a button with a yellow bolt on it.  Click this button to show the events for the grid.  If there is nothing in the UpdateCommand event here, you should be able to show your UpdateQty routine in a dropdown in the Property entry for the event.  You can also look for this in Windows Generated Code:

this.grdThe Grid.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.UpdateQty);

which is where the routine is wired to the event.

John

0
 
LVL 1

Author Comment

by:champ_010
ID: 12149698
John,

I'm not using VisualStudios--just hand coding inline for now so I can learn.  You're probalby right about my update routine not being wired right.  I don't need an Edit routine to use the Update right?

Can I set something with the OnTextChanged?

I think the problem may be that when I press the button and go to the Button_Clicked method, it's already pass through the Page_Load and the tblCart in Session gets rebind to the DataGrid so the old values replace the new ones before it gets to the update.  Not sure...

(BTW, I'm going to sleep...back tommorrow am....)
0
 
LVL 10

Expert Comment

by:jnhorst
ID: 12151147
You may be right about Page_Load().  Make sure you are bindind the grid like this:

If Not Page.IsPostBack Then
     theGrid.DataBind()
End If

John
0
 
LVL 1

Author Comment

by:champ_010
ID: 12183933

What was the problem?  I had two <form> tags on the same page--but it took me this long to figure out because one of them wasn't apparent when looking at my code--it was in the user control I attached to the page for my nav bar.

Accepted John's answer--I really like your breakdown of what I am looking for--a web control within a DataGrid cell.  I need to learn to visualize things this way. Thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Log4Net custom Appender not being fired. 5 74
C# GridRow get Old/New Value 1 55
Calling stored proc in EDMX 11 23
error on the asp.net page 1 33
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now