如何从该行上的文本框访问 DataGridRow?

发布于 2024-07-19 02:22:59 字数 385 浏览 4 评论 0原文

在 DataGrid 中,当文本框中的文本更改时,我想将该行中另一个字段的值添加到数组中。

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();
}

但是 myItem.DataItem 计算结果为 null。 我期望它评估为 DataRowView?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暮倦 2024-07-26 02:22:59

如果执行以下操作,您可以触发 TextChanged 事件:

<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>

您会注意到我设置了以下属性:
自动回传=“真”
我还手动将 OnTextChanged="txtBox_TextChanged" 添加到文本框。

在我后面的代码中,我有:

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

事件触发的唯一方法是当您在键入后失去对文本框的焦点时。

需要考虑的要点:
这将导致回发,因此 Ajax 可能是保持良好用户体验的好方法。
您需要确保将 DataBind() 包装在 if (!IsPostBack) 中

希望这有帮助!

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!

千纸鹤 2024-07-26 02:22:59

实际上,我通过向表中添加一个自动编号列,并使用它的值来确定该行在表中的位置,然后使用它的值来影响数据网格中的相应行来解决这个问题。
我现在只是更改行的颜色,而不是将该行中的值添加到数组中,如原始问题中所述。

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?
    }
}

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?
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文