如题,我在GridView中的模板列中定义了一个TextBox(GridView有多列数据),然后在GridView控件外面定义一个Button, 我要实现的功能是,我在GirdView模板列中的TextBox中更改了数据,然后点击Button后,得到刚才GridView模板列中更改数据的那一列的主键ID值,代码如下,谢谢高手的指点!               代码:
                  <asp:GridView ID="gvwOrder" runat="server" 
                  AutoGenerateColumns="False" DataKeyNames="ProductID"
                  OnRowDeleting="gvwOrder_RowDeleting" Width="100%" 
                  OnRowCommand="gvwOrder_RowCommand">
                  <Columns>
                          <asp:BoundField DataField="ProductName" HeaderText="Name" ReadOnly="True" />
                         <asp:BoundField DataField="UnitPrice" HeaderText="Price"  ReadOnly="True"/>
                     <asp:TemplateField HeaderText="Qty">
                         <ItemTemplate>
                            <asp:TextBox ID="editQuantity"  Width="24px" MaxLength="3" runat="server" Text='<%# Bind("Quantity") %>' ></asp:TextBox>
                         </ItemTemplate>
                     </asp:TemplateField>
                         <asp:BoundField DataField="SalesAmount" HeaderText="Amt" ReadOnly="True" />
                        <asp:CommandField ShowDeleteButton="True" />
                 </Columns>
             </asp:GridView>
其中,<asp:TextBox ID="editQuantity"是我在上面更改数据,<asp:Button ID="btnUpdateQuantity" runat="server" Text="Update Qty" OnClick="btnUpdateQuantity_Click" />按钮<asp:Button ID="btnUpdateQuantity" 是我点击后,要在点击事件方法中找到GridView模板列中更改了TextBox内容所对应的主键ID值。希望高手能赐教!!!   
     

解决方案 »

  1.   

    没有好的办法,比较容易实现的是每行用一个隐藏的Label里也存放 Quantity的值,然后在按钮的点击时间中遍历GridView的每一行,判断TextBox的值和标签的值是否一样,如果不同,再进行更新
      

  2.   

    <asp:GridView ID="gvwOrder" runat="server" 
    AutoGenerateColumns="False" DataKeyNames="ProductID" 
    OnRowDeleting="gvwOrder_RowDeleting" Width="100%" 
    OnRowCommand="gvwOrder_RowCommand"> 
        <Columns> 
            <asp:BoundField DataField="ProductName" HeaderText="Name" ReadOnly="True" /> 
            <asp:BoundField DataField="UnitPrice" HeaderText="Price"  ReadOnly="True"/> 
            <asp:TemplateField HeaderText="Qty"> 
                <ItemTemplate> 
                    <asp:Label ID="lblQuantity" runat="server" Text=' <%# Eval("Quantity") %>' Visible="false"></asp:Label>
                    <asp:TextBox ID="editQuantity"  Width="24px" MaxLength="3" runat="server" Text=' <%# Eval("Quantity") %>' > </asp:TextBox> 
                    
                </ItemTemplate> 
            </asp:TemplateField> 
            <asp:BoundField DataField="SalesAmount" HeaderText="Amt" ReadOnly="True" /> 
            <asp:CommandField ShowDeleteButton="True" /> 
        </Columns> 
    </asp:GridView> 
    <asp:Button ID="btnUpdateQuantity" runat="server" Text="Update Qty" OnClick="btnUpdateQuantity_Click" /> protected void btnUpdateQuantity_Click(object sender, EventArgs e)
    {
        Label lblQuantity = null;
        TextBox editQuantity = null;    foreach (GridViewRow row in gvwOrder.Rows)
        {
            lblQuantity = row.FindControl("lblQuantity") as Label;
            editQuantity = row.FindControl("editQuantity") as TextBox;
            if (string.Compare(lblQuantity.Text, editQuantity.Text, true) != 0)
            {
                int productID = Convert.ToInt32(gvwOrder.DataKeys[row.RowIndex].Value);
                //做你的更新操作
            }
        }
    }
      

  3.   

    点击GridView更改数据时,将该行的id保存到ViewState["myid"](或保存到Label,这个Label的Visibled是false)
    点击页面的Button时,取ViewState["myid"]的值(取Label的值)
      

  4.   

    声明为<asp:TextBox ID="editQuantity"  Width="24px" MaxLength="3" runat="server" Text=' <%# Bind("Quantity") %>' 
        DataID='<%# Eval("primaryKeyField") %>' OnTextChanged="editQuantity_TextChanged"> </asp:TextBox> 
    之后,在事件处理程序editQuantity_TextChanged中,可以这样得到绑定的数据:protected void editQuantity_TextChanged(object sender, EventArgs e)
    {
        TextBox txt=sender as TextBox;
        string DataID=txt.Attributes["DataID"];
        //将txt.Text的值写入DataID所标记的对象持久化机制中
    }如果另外一个模板列中有其它控件照样可以这样取得同一行中其它控件的值:protected void editQuantity_TextChanged(object sender, EventArgs e)
    {
        TextBox txt=sender as TextBox;
        string DataID=txt.Attributes["DataID"];
        TextBox otherTextBox=txt.FindControl("OtherTextBox1") as TextBox;
        Label otherLabel=txt.FindControl("OtherLabel1") as Label;
        Label otherDropDownList=txt.FindControl("OtherDropDownList1") as DropDownList;
        //将txt.Text、otherTextBox.Text、otherLabel.Text、otherDropDownList.SelectedValue 的值写入DataID所标记的对象持久化机制中
    }
      

  5.   

    另外你要注意,你的设计中,点击按钮时不一定只发现一个TextBox的TextChanged,如果用户在同一列的多个行的多个TextBox上都修改了,当点击按钮时,每一个TextBox的TextChanged都会依次被触发。而你的描述中看起来只是考虑“一个”TextBox被修改,这是不准确的。
      

  6.   

    我在类似帖子中的回复,可以参见:《GridView里模板列的CheckBox问题