views:

268

answers:

2

In a DataGrid, when text in a textbox changes I want to add the value of another field in that row to an array.

public void txtTitle_TextChanged(object sender, EventArgs e)
{
    TextBox titleBox = (TextBox)sender;
    DataGridItem myItem = (DataGridItem)titleBox.Parent.Parent;
    string test = DataBinder.Eval(myItem.DataItem, "prod_id").ToString();
}

However myItem.DataItem evaluates as null. I was expecting it to evaluate as DataRowView?

+1  A: 

You can get the TextChanged event to fire if you do the following:

<asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="False" 
    onitemdatabound="DataGrid1_ItemDataBound">
    <Columns>
        <asp:TemplateColumn HeaderText="Test">
            <ItemTemplate>
                <asp:TextBox OnTextChanged="txtBox_TextChanged" ID="TextBox1" runat="server" AutoPostBack="True"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateColumn>
        <asp:BoundColumn DataField="Name" HeaderText="Test 1"></asp:BoundColumn>
    </Columns>
</asp:DataGrid>

You will notice that i have the following properties set: AutoPostBack="True" I have also manually added the OnTextChanged="txtBox_TextChanged" to the text box as well.

In my code behind i have:

protected void txtBox_TextChanged(object sender, EventArgs e)
{
    TextBox txtBox = (TextBox)sender;
    Label1.Text = txtBox.Text;
}

The only way the event will fire is when you lose focus on the text box after typing.

Key points to consider: This will cause a post back, so Ajax might be a good way to keep the user experience nice. You will need to make sure you wrap your DataBind() in a if (!IsPostBack)

Hope this helps!

Jason Heine
I have no problem getting the event to fire, (it is called exactly as you describe).My problem is in referencing the row that the 'sender' textbox is on.
fearoffours
A: 

Effectively, I solved this by adding an autonumber column to the table, and using the value of this to determine the row's positino in the table, then using the value of this to affect the appropriate row in the datagrid. I'm now merely changing the color of the row rather than adding values in that row to an array, as stated in the original question.

public void txtPrice_TextChanged(object sender, EventArgs e)
{
    TextBox txtPrice = (TextBox)sender;
    DataGridItem myItem = (DataGridItem)txtPrice.Parent.Parent;
    markRows(myItem, true);
}

public void markRows(DataGridItem myItem, bool toSave)
{
    // Prepeare to save this record?
    CheckBox thisSave = (CheckBox)myItem.FindControl("chkSave");
    thisSave.Checked = toSave;
    // Establish the row's position in the table
    Label sNo = (Label)myItem.FindControl("SNo");
    int rowNum = Convert.ToInt32(sNo.Text) - 1;
    CheckBox rowSave = (CheckBox)grid.Items[rowNum].FindControl("chkSave");

    // Update background color on the row to remove/add highlight 
    if (rowSave.Checked == true)
        grid.Items[rowNum].BackColor = System.Drawing.Color.GreenYellow;
    else
    {
        Color bgBlue = Color.FromArgb(212, 231, 247);
        grid.Items[rowNum].BackColor = bgBlue;
        // some code here to refresh data from table?
    }
}
fearoffours