2009-11-05 50 views
0

下面是我希望控件工作的方式。点击gridview中的一行编辑。在其中一列中有一个文本框,我希望它是空白的,即使该列的数据库中有数据,但是当我单击更新时,我想要用户输入的任何新文本来更新该列的数据库。这将是单向数据绑定,但反过来呢?ASP .Net Gridview仅在点击更新时绑定到数据

回答

0

下面是我如何在生成的select,update和delete方法中使用sql数据源。

首先,你需要做的是要与一个项目模板,并编辑项目模板编辑这样的一个模板场任意列:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
     DataKeyNames="ID" DataSourceID="SqlDataSource1" 
     onrowupdating="GridView1_RowUpdating"> 
    <Columns> 
    <asp:TemplateField HeaderText="City" SortExpression="City"> 
     <ItemTemplate> 
      <asp:Label runat="server" Text='<%# Eval("City") %>'></asp:Label> 
     </ItemTemplate> 
     <EditItemTemplate> 
      <asp:TextBox runat="server" ID="txtCityEdit" Text=""></asp:TextBox> 
     </EditItemTemplate> 
    </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

下一页处理GridView控件的更新事件的:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
    { 
     //find the value control who's value you want 
     TextBox tb = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCityEdit"); 

     //since we are not using a bound column for city we need to explicitly insert the value 
     //into the update parameter. 
     SqlDataSource1.UpdateParameters["City"].DefaultValue = tb.Text; 
    } 

即使您使用的不是SQL数据源,也应该是基本的解决方案。

+0

这并不适合我,但绝对让我走上正轨!我最大的问题是将文本框的值从模板字段中取出,因为intellisense不会给我。 我尝试使用UpdateParameters喜欢你的例子,但得到一个空引用错误,然后我试图添加我的新集合,并没有工作。 我结束了我的新值添加到“NewValues”集合,它的工作。 – user204019

0
// Find notes textbox 
TextBox tb = (TextBox)MyActiveReferrals.Rows[e.RowIndex].FindControl("NotesTextBox"); 
e.NewValues.Add("Notes", tb.Text); 

我用了一个linq数据源。