最近做项目的时候遇到的问题,使用gridView进行更新的时候,获取不到textBox里面的值,如果使用Eval绑定数据,则获得的就是原来绑定的值,并不是用户在更新界面下重新输入的值。在使用其他数据显示控件的时候也是遇到相同的问题!
Code
<asp:GridView ID="GridView1" runat="server" CellPadding="4" DataKeyNames="id"
ForeColor="#333333" GridLines="None" Width="797px" AutoGenerateColumns="False"
onrowdatabound="GridView1_RowDataBound"
onselectedindexchanged="GridView1_SelectedIndexChanged"
style="text-align: left" Height="194px"
onrowcommand="GridView1_RowCommand" onrowediting="GridView1_RowEditing"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowupdated="GridView1_RowUpdated" onrowupdating="GridView1_RowUpdating">
<RowStyle BackColor="#E3EAEB" />
<Columns>
<asp:TemplateField HeaderText="任务名称">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Eval("taskName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("id", "taskDetails.aspx?id={0}") %>'
Text='<%# Eval("taskName") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="创建时间">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("ct") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("ct") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="状态">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("taskStatus") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("taskStatus") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="操作">
<EditItemTemplate>
<asp:LinkButton ID="LinkBtnUpdate" runat="server" CommandName="Update">更新</asp:LinkButton>
|<asp:LinkButton ID="LinkBtnCancle" runat="server" CommandName="Cancle" Text="取消"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButtonEdit" runat="server" CommandName="Edit"
>编辑任务</asp:LinkButton>
|
<asp:LinkButton ID="LinkBChangeStatus" runat="server" CommandName="changeStatus">改变状态</asp:LinkButton>
|
<asp:LinkButton ID="LinkButtonDelete" runat="server" CommandName="Delete" >删除</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
这个是更新事件的代码
Code
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//获取TextBox的值,即用户输入的值
string newName = (GridView1.Rows[e.RowIndex].FindControl("TextBox3") as TextBox).Text;
//获取id
int taskID=Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
//进行更新
dataAccess da = new dataAccess();
if (da.EditTask(newName, taskID))
{
//提示更新成功
}
else
{
//提示出错,更新失败
}
}