Unable to retrieve textbox value from datagrid

J

John Dalberg

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
 
J

Jos

John said:
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

Any line breaks or spaces between <ItemTemplate> and
<asp:TextBox> are converted into a Literal control by ASP.NET.

For the empty values in the textboxes, is it possible
that the boxes are empty to begin with, you fill them
in, and in your code, they are still empty?
In that case, you should not use databinding on postback.

In Page_Load, use this:

If Not IsPostBack Then
' databinding here
End If
 
J

John Dalberg

John said:
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

Any line breaks or spaces between <ItemTemplate> and
<asp:TextBox> are converted into a Literal control by ASP.NET.

For the empty values in the textboxes, is it possible
that the boxes are empty to begin with, you fill them
in, and in your code, they are still empty?
In that case, you should not use databinding on postback.

In Page_Load, use this:

If Not IsPostBack Then
' databinding here
End If

Your reply lit a lightbulb over my head. I had an autopostback ddl and the
page_load, which gets fired before the save click event, was loading the
data from the database and therefore erasing the user inputs. Thanks.

John Dalberg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top