在gridview中绑定时更新列值 [英] Update column value while binding in gridview

查看:76
本文介绍了在gridview中绑定时更新列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据列为Status的列值更新列Action的值。如果Status为True,则Action中的值必须更新为Deactivate。运行时,操作字段不会更新。我认为代码隐藏有误。



这是代码隐藏:

I'd like to update the values of column Action based on the value of the column Status which is Boolean. If Status is True then value in Action must update to Deactivate. When I run, the Action field doesn't updates. I think there is an error in codebehind.

Here is the codebehind:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string value = e.Row.Cells[4].Text;

        TextBox TextBox2 = (TextBox)e.Row.FindControl("TextBox2");
        if (value == "True")
        {
            TextBox2.Text = "Take";
        }
        else if (value == "False")
        {
            TextBox2.Text = "Available";
        }
    }

}





这是ASP代码:





Here is the ASP code:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"       AutoGenerateColumns="False" DataSourceID="SqlDataSource1" 

AllowSorting="True" 

onselectedindexchanged="GridView1_SelectedIndexChanged">       
<columns>
    <asp:BoundField DataField="ShopNumber" HeaderText="ShopNumber"     ItemStyle-Width="80" SortExpression="ShopNumber" >


<asp:BoundField DataField="ShopName" HeaderText="ShopName" ItemStyle-Width="80" SortExpression="ShopName" >


<asp:BoundField DataField="Address" HeaderText="Address" ItemStyle-Width="80"SortExpression="Address" >


<asp:BoundField DataField="Website" HeaderText="Website" ItemStyle-Width="80" SortExpression="Website" >


<asp:TemplateField HeaderText="Status" SortExpression="Status">
<itemtemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Status") %>'>            
</itemtemplate>
<edititemtemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Status") %>'>   
</edititemtemplate>
<itemstyle width="80px" />


<asp:TemplateField HeaderText="Action" SortExpression="Action">
<itemtemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Action") %>'>
</itemtemplate>
<edititemtemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Action") %>'> 
</edititemtemplate>
<edititemtemplate>
<asp:TextBox ID="TextBox1" runat="server">
</edititemtemplate>

</columns>

推荐答案

TextBox2 状态 TextBox1 行动。你这样做错了。



这应该是......

TextBox2 is Status and TextBox1 is Action. You are doing it wrong.

It should be something like...
TextBox txtAction = (TextBox)e.Row.FindControl("TextBox1");
TextBox txtStatus = (TextBox)e.Row.FindControl("TextBox2");

bool status;
            
if (Boolean.TryParse(txtStatus.Text, out status))
{
    txtAction.Text = "Take";
}
else
{
    txtAction.Text = "Available";
}



注意:请将您的控件ID更改为有意义的名称,而不是 TextBox1 TextBox2


Note: Please change your control IDs to meaningful names, not TextBox1 and TextBox2.


这篇关于在gridview中绑定时更新列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆