473,383 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Unable to retrieve textbox value from datagrid

I have a datagrid plus a button to save the contents of the grid to the
database. My code gets blanks in the textbox field. I am not sure why.
Also I don't know why dgi.Cells[1].Controls[0] & dgi.Cells[1].Controls[2]
are Literal Controls. The html shows only a textbox in the second table
cell.

This is the code and below is the grid.

Code:
private void btnSave_Click(object sender, System.EventArgs e)
{
string sName;
int iCPID;
foreach(DataGridItem dgi in this.dgRegister.Items)
{
if(dgi.ItemType.ToString() == "Item" || dgi.ItemType.ToString() ==
"AlternatingItem")
{

TableCell tc = (TableCell)dgi.Cells[2];
iCPID = Convert.ToInt32(tc.Text);
TextBox tb = (TextBox)dgi.Cells[1].Controls[1];
sName = tb.Text; <-- always blank!
cProfitCash.UpdateDatabase(iCPID, sName );
}
}
Grid:
<asp:datagrid id=dgRegister runat="server" AutoGenerateColumns="False">
<AlternatingItemStyle CssClass="DataGridAltItem">
</AlternatingItemStyle>

<ItemStyle CssClass="DataGridItem">
</ItemStyle>

<HeaderStyle CssClass="grid-header">
</HeaderStyle>

<Columns>
<asp:BoundColumn DataField="CPCode" ReadOnly="True"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Name">
<HeaderStyle Font-Bold="True">
</HeaderStyle>

<ItemTemplate>

<asp:TextBox id="EmpName" Text='<%# DataBinder.Eval(Container.DataItem,
"EmpName") %>' Width="194px" runat="server"></asp:textbox>

</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn Visible="False" DataField="CPID"></asp:BoundColumn>
</Columns>
</asp:datagrid>

Jon Dalberg
Nov 18 '05 #1
1 2053
My guess is because you are rebinding the datagrid on postback, wrap your
databinding code in a if (!Page.IsPostBack){ }

[0] and [2] show up as literals because you have spaces/newlines.

You should consider two other things:
Change -->
if(dgi.ItemType.ToString() == "Item" || dgi.ItemType.ToString()
=="AlternatingItem") {

to -->
if(dgi.ItemType == ListItemType.Item || dgi.ItemType ==
ListItemType.AlternatingItem) {

Why ToString() an enum for comparison purposes? its much slower and error
prone.
And, instead of accessing controls via indexes like
dgi.Cells[1].Controls[0], consider doing a FindControl() instead...it might
be slower but (a) its considerably more readable and less error prone.

For example, replace -->
TextBox tb = (TextBox)dgi.Cells[1].Controls[1];

with -->
TextBox tb = (TextBox)dgi.FindControl("EmpName");

Karl

"John Dalberg" <jo*****@hotmail.com> wrote in message
news:hu******************************@40tude.net.. .
I have a datagrid plus a button to save the contents of the grid to the
database. My code gets blanks in the textbox field. I am not sure why.
Also I don't know why dgi.Cells[1].Controls[0] & dgi.Cells[1].Controls[2]
are Literal Controls. The html shows only a textbox in the second table
cell.

This is the code and below is the grid.

Code:
private void btnSave_Click(object sender, System.EventArgs e)
{
string sName;
int iCPID;
foreach(DataGridItem dgi in this.dgRegister.Items)
{
if(dgi.ItemType.ToString() == "Item" || dgi.ItemType.ToString() ==
"AlternatingItem")
{

TableCell tc = (TableCell)dgi.Cells[2];
iCPID = Convert.ToInt32(tc.Text);
TextBox tb = (TextBox)dgi.Cells[1].Controls[1];
sName = tb.Text; <-- always blank!
cProfitCash.UpdateDatabase(iCPID, sName );
}
}
Grid:
<asp:datagrid id=dgRegister runat="server" AutoGenerateColumns="False">
<AlternatingItemStyle CssClass="DataGridAltItem">
</AlternatingItemStyle>

<ItemStyle CssClass="DataGridItem">
</ItemStyle>

<HeaderStyle CssClass="grid-header">
</HeaderStyle>

<Columns>
<asp:BoundColumn DataField="CPCode" ReadOnly="True"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Name">
<HeaderStyle Font-Bold="True">
</HeaderStyle>

<ItemTemplate>

<asp:TextBox id="EmpName" Text='<%# DataBinder.Eval(Container.DataItem, "EmpName") %>' Width="194px" runat="server"></asp:textbox>

</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn Visible="False" DataField="CPID"></asp:BoundColumn>
</Columns>
</asp:datagrid>

Jon Dalberg

Nov 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mark Brown | last post by:
I'm new to ASP.NET and I'm trying to create an aspx page that retrieves info from a database, let's the user make a change and write the change back. It reads from the database just fine. The...
1
by: Bryan | last post by:
Hello: I need to retrieve the value of a textbox control in a asp.net datagrid using Javascript. Can anybody help out. I believe the control clientID needs to be passed client side so the...
1
by: moid | last post by:
im not able to get textbox value from the datagrid that is in the usercontrol. usercontrol is created dynamically on the page. i want to delete some values if textbox in the datagrid is 0 ...
7
by: I am Sam | last post by:
I have a DataGrid that is passing information to a stored procedure properly but the parameters aren't being casted properly. I was woundering if anyone can tell me how I should properly cast the...
4
by: René Kabis | last post by:
People, I am at my wit's end. I am using the exact code from http://aspnet.4guysfromrolla.com/articles/071002-1.aspx And yet, the code does not manage to update the database. When I go to...
2
by: simon | last post by:
hello, new to vb.net, have a few questions about DataGrid. I have a dataGrid that is working pulling a dataset back from a stored proc and binding to the datagrid for display the datagrid's...
1
by: whidbey | last post by:
Hello friends, I am whidbey, new to thescripts and dot net as well.I am working over Online Shopping Cart,web application.I design a page (webform5.aspx) where user search books then select the books...
0
by: whidbey | last post by:
Hello friends, I am whidbey, new to thescripts and dot net as well.I am working over Online Shopping Cart,web application.I design a page (webform5.aspx) where user search books then select the books...
2
by: Steve Hershoff | last post by:
Hi everyone, I have a DataGrid with several TemplateColumns. One of these columns has an EditItemTemplate that contains an ASP.Net DropDownList. I'm catching this DropDownList's...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.