textbox value not returned in a datagrid

A

Amadelle

Hi all and thanks in advance for your help,

I have a problem with capturing the changed value of a text box in a
datagrid. The datagrid is populated based on a dataset and I am using
template columns to show some of the columns. Few of the columns have
textboxes to make them available for editing. I am trying to update the
dataset all at once using one update button. Here is my code... (for
simplicity I have only listed one of the columns)

============================================================================
===
For the ASP.NET columns:

<asp:datagrid id="dgGED" runat="server" AutoGenerateColumns="False"
ItemStyle-BackColor="#E8E8E8"
HeaderStyle-BackColor="#D0D0D0" font-size="10pt" font-names="Arial"
gridlines="vertical"
Bordercolor="black">
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<B>EmpNumber</B>
</HeaderTemplate>
<ItemTemplate>
<center>
<asp:TextBox ID="txtEmpNum" Runat=server text='<%#
DataBinder.Eval(Container.DataItem, "EmpNumber") %>' size=5 style="border=0"</asp:TextBox></center>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>

============================================================================
===
The code behind C#:
//Iterate and apply changes.

string strEmpNumber = "";

foreach (DataGridItem item in dgGED.Items)

{

//Get value from grid.

strEmpNumber = (string)((TextBox) item.FindControl("txtEmpNum")).Text;

ds.Tables[0].Rows[item.ItemIndex]["EmpNumber"] = strEmpNumber;

}

============================================================================
===

Just a few more notes: If I change the value of the textbox to some new
text, the strEmpNumber still shows the old value of the textbox before
editing (basically the original value that comes from the dataset). I don't
get any errors and everything seems to run just fine. So I don't know what
I am missing that the new change is not captured in the

strEmpNumber = (string)((TextBox) item.FindControl("txtEmpNum")).Text;

statement?



Any help and suggestions are truely appreciated,

Thanks a bunch,

Amadelle
 
A

Amadelle

for those who are interested I found my problem.
I was making a mistake of regenrating the dataset in the PageLoad event
everytime - and bind it anew to the datagrid. So when the save button was
hit all the users input would basically disappear since the dataset/datagrid
would get regenerated from the datasource. So by adding a simple logic to
check post back or not, the problem was solved.

Thanks again,

Amadelle



Amadelle said:
Hi all and thanks in advance for your help,

I have a problem with capturing the changed value of a text box in a
datagrid. The datagrid is populated based on a dataset and I am using
template columns to show some of the columns. Few of the columns have
textboxes to make them available for editing. I am trying to update the
dataset all at once using one update button. Here is my code... (for
simplicity I have only listed one of the columns)

============================================================================
===
For the ASP.NET columns:

<asp:datagrid id="dgGED" runat="server" AutoGenerateColumns="False"
ItemStyle-BackColor="#E8E8E8"
HeaderStyle-BackColor="#D0D0D0" font-size="10pt" font-names="Arial"
gridlines="vertical"
Bordercolor="black">
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<B>EmpNumber</B>
</HeaderTemplate>
<ItemTemplate>
<center>
<asp:TextBox ID="txtEmpNum" Runat=server text='<%#
DataBinder.Eval(Container.DataItem, "EmpNumber") %>' size=5 style="border=0"
</asp:TextBox></center>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
============================================================================
===
The code behind C#:
//Iterate and apply changes.

string strEmpNumber = "";

foreach (DataGridItem item in dgGED.Items)

{

//Get value from grid.

strEmpNumber = (string)((TextBox) item.FindControl("txtEmpNum")).Text;

ds.Tables[0].Rows[item.ItemIndex]["EmpNumber"] = strEmpNumber;

}

============================================================================
===

Just a few more notes: If I change the value of the textbox to some new
text, the strEmpNumber still shows the old value of the textbox before
editing (basically the original value that comes from the dataset). I don't
get any errors and everything seems to run just fine. So I don't know what
I am missing that the new change is not captured in the

strEmpNumber = (string)((TextBox) item.FindControl("txtEmpNum")).Text;

statement?



Any help and suggestions are truely appreciated,

Thanks a bunch,

Amadelle
 

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,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top