更新gridview中的行时出现问题

时间:2013-12-16 03:45:18

标签: c# asp.net gridview

我有gridview,用户可以在gridview中编辑和更新数据但我的问题是当我在Action字段中放入一些文本时,没有更新任何内容。我试图运行调试模式,所以我可以看到发生了什么,但我没有看到我在文本框中输入的值,它在我的变量中显示为空白。这是我的aspx代码:

 <asp:GridView ID="GridView1" runat="server"  Width = "855px" AutoGenerateColumns = "False" Font-Names = "Arial" 
        OnPageIndexChanging = "OnPaging" onrowediting="EditGridView1"
        onrowupdating="UpdateGridView1"  onrowcancelingedit="CancelEdit" CellPadding="3" 
                BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" >
       <Columns>
        <asp:TemplateField ItemStyle-Width = "30px"  HeaderText = "ID">
            <ItemTemplate>
                <asp:Label ID="lblQST_SK" runat="server" Text='<%# Eval("QUEST_SK")%>'></asp:Label>
            </ItemTemplate> 

            <ItemStyle Width="10px" />
        </asp:TemplateField> 

        <asp:TemplateField ItemStyle-Width = "150px"  HeaderText = "Action">
            <ItemTemplate>
                <asp:Label ID="lblAction" runat="server" Text='<%# Eval("ACTION")%>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtAction" runat="server"  Text='<%# Eval("ACTION")%>' TextMode="MultiLine" Height="80px"></asp:TextBox>
            </EditItemTemplate>  
            <ItemStyle Width="150px" />
        </asp:TemplateField>        
       </Columns> 
           <FooterStyle BackColor="White" ForeColor="#000066" />
           <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" 
               HorizontalAlign="Left" />
           <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
           <RowStyle ForeColor="#000066" />         
    </asp:GridView> 

这是我的代码背后:

protected void UpdateGridView1(object sender, GridViewUpdateEventArgs e)
    {
        string QUEST_SK = ((Label)GridView1.Rows[e.RowIndex].FindControl("lblQST_SK")).Text;
        string ACTION = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtAction")).Text;
        SqlConnection con = new SqlConnection(strConnString);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "update mytable set ACTION=@ACTION" +
         "where QUEST_SK=@QUEST_SK;" +
         "SELECT QUEST_SK, ACTION FROM mytable";

        cmd.Parameters.Add("@QUEST_SK", SqlDbType.VarChar).Value = QUEST_SK;
        cmd.Parameters.Add("@ACTION", SqlDbType.VarChar).Value = ACTION;
        GridView1.EditIndex = -1;
        GridView1.DataSource = GetData(cmd);
        GridView1.DataBind();
    }

1 个答案:

答案 0 :(得分:0)

尝试在页面加载事件中编写以下函数:

Page_Load()
{

if(IsPostBack)
{
    return`;
}

}