<asp:Content ID="ctnMain" ContentPlaceHolderID="cphMain" runat="server"> 
    <asp:GridView ID="grvCart" runat="server" AutoGenerateColumns="False" HorizontalAlign="Center" 
        Width="400px" ShowFooter="True" OnDataBound="grvCart_DataBound" OnRowDataBound="grvCart_RowDataBound"> 
        <Columns> 
            <asp:TemplateField HeaderText="產品"> 
                <ItemTemplate> 
                    <table border="0" cellpadding="0" cellspacing="0" width="100%"> 
                        <tr valign="top"> 
                            <td style="width: 80px;"> 
                                <asp:Image ImageUrl=' <%# Eval("product_image") %>' ID="imgProduct" runat="server" /> </td> 
                            <td rowspan="2"> 
                                <asp:Literal Text=' <%# Eval("product_name") %>' ID="lblProductName" runat="server"> </asp:Literal> 
                            </td> 
                        </tr> 
                        <tr valign="top"> 
                            <td> 
                                <asp:Literal ID="lblProductCode" runat="server"> </asp:Literal> </td> 
                        </tr> 
                    </table> 
                </ItemTemplate> 
            </asp:TemplateField> 
            <asp:TemplateField HeaderText="數量"> 
                <ItemStyle HorizontalAlign="Center" Width="60px" /> 
                <ItemTemplate> 
                    <asp:TextBox Text=' <%# Eval("product_quantity") %>' ID="txtQuantity" runat="server" MaxLength="5" Width="30px"> </asp:TextBox> 
                </ItemTemplate> 
            </asp:TemplateField> 
            <asp:TemplateField HeaderText="單價"> 
                <ItemStyle HorizontalAlign="Right" Width="80px" /> 
                <ItemTemplate> 
                    <asp:Literal Text=' <%# Eval("product_unit_price") %>' ID="lblUnitPrice" runat="server"> </asp:Literal> 
                </ItemTemplate> 
                <FooterTemplate> 
                    總金額︰ 
                </FooterTemplate> 
                <FooterStyle Font-Bold="True" HorizontalAlign="Right" /> 
            </asp:TemplateField> 
            <asp:TemplateField HeaderText="小計"> 
                <ItemStyle HorizontalAlign="Right" Width="80px" /> 
                <ItemTemplate> 
                    <asp:Literal ID="lblSubtotal" runat="server"> </asp:Literal> 
                </ItemTemplate> 
                <FooterTemplate> 
                    <asp:Literal ID="lblAmountTotal" runat="server"> </asp:Literal> 
                </FooterTemplate> 
            </asp:TemplateField> 
            <asp:TemplateField HeaderText="刪除"> 
                <ItemStyle HorizontalAlign="Center" Width="30px" /> 
                <ItemTemplate> 
                    <asp:CheckBox ID="chkDelete" runat="server" /> 
                </ItemTemplate> 
            </asp:TemplateField> 
        </Columns> 
        <EmptyDataTemplate> 
            購物籃內並沒有產品。 
        </EmptyDataTemplate> 
        <RowStyle VerticalAlign="Top" /> 
    </asp:GridView> 
    <div style="text-align: right;"> 
        <asp:Button ID="cmdRefresh" runat="server" CausesValidation="False" Text="更新購物籃" OnClick="cmdRefresh_Click" /> 
        <asp:Button ID="cmdNext" runat="server" CausesValidation="False" Text="下一步" OnClick="cmdNext_Click" /> </div> 
</asp:Content> 
前台代码!(  button  不是在gridview 里面。)gridview 包含三个控件:button , checkbox , textbox . 
我想在button_click()事件里获取 textbox  和 checkbox 控件并修改它们的值。 protected void cmdRefresh_Click(object sender, EventArgs e)  
        { 
            DataTable DT = (DataTable)Session["shopping_cart"]; 
            TextBox T = new TextBox(); 
            CheckBox C = new CheckBox(); 
            foreach (DataRow DW in DT.Rows) 
            { 
                foreach (GridViewRow row in grvCart.Rows) 
                {                     if (row.RowType == DataControlRowType.DataRow) 
                    {    
                        // 一下三行代码都有问题:(获取不到控件)
                      //T = this.grvCart.Rows[i].FindControl("lblSubtotal") as TextBox; 
                        T = (TextBox)row.FindControl("lblSubtotal"); 
                        if (T.Text != null) 
                        {

解决方案 »

  1.   

    是啊,没成功。
    依然没找到textbox ,checkbox 子控件。
      

  2.   


    protected void cmdRefresh_Click(object sender, EventArgs e) 
            {
                DataTable DT = (DataTable)Session["shopping_cart"];
                TextBox T ;
                CheckBox C ;
                GridViewRow GRV = (sender as Button).NamingContainer as GridViewRow;
                if (GRV != null)
                {
                    T = (TextBox)GRV.FindControl("lblSubtotal");
                    if (T != null)
                    {
                        foreach (DataRow DW in DT.Rows)
                        {
                            if ( GRV.RowType == DataControlRowType.DataRow)
                            {
                                if (FormatFunc.StrIsInt(T.Text))
                                {
                                    DW["product_quantity"] = Convert.ToInt32(T.Text);
                                }红色地方判断为false ,直接跳出if()判断。
      

  3.   

     foreach (GridViewRow row in this.gvview.Rows)
                    {
                       T = (((TextBox)row.Cells[?].Controls[1]).Text);
                    }
      

  4.   

    真无语了,我最后的回复你没看啊……protected void cmdRefresh_Click(object sender, EventArgs e)  

        foreach(GridViewRow gvr in grvCart.Rows)
        {
            if(gvr.RowType == DataControlRowType.DataRow)
            {
                TextBox T = gvr.FindControl("txtQuantity") as TextBox;
                if(T != null)
                {
                     //to do...
                }
            }
        }
    }
      

  5.   


    真无语了,我最后的回复你没看啊……
    C# codeprotectedvoid cmdRefresh_Click(object sender, EventArgs e)  
    {foreach(GridViewRow gvrin grvCart.Rows)
        {if(gvr.RowType== DataControlRowType.DataRow)
            {
                TextBox T= gvr.FindControl("txtQuantity")as TextBox;if(T!=null)
                {//to do...            }
            }
        }
    }
    [/Quote]oj 兄弟,这个我也试过了。 T为null。
      

  6.   

    easy,try debug:
    example:
    (TextBox)(gv.Rows[int.Parse(e.CommandArgument.ToString())].Cells[2].FindControl("txbTest"));
      

  7.   


     T 依然为 null ! oj 兄。
      

  8.   

    protected void cmdRefresh_Click(object sender, EventArgs e)
            {
                DataTable DT = (DataTable)Session["shopping_cart"];
                CheckBox C ;            foreach (GridViewRow GRV in grvCart.Rows)
                {
                    if (GRV.RowType == DataControlRowType.DataRow)
                    {
                        TextBox T = GRV.FindControl("lblSubtotal") as TextBox;
                        if (T != null)
                        {
                            foreach (DataRow DW in DT.Rows)
                            {                            if (FormatFunc.StrIsInt(T.Text))
                                {
                                    DW["product_quantity"] = Convert.ToInt32(T.Text);
                                }
                                C = (CheckBox)GRV.FindControl("checkboxid");
                                if (C.Checked)
                                {
                                    Response.Write("aaa");
                                }现在的代码....
      

  9.   

    兄弟,lblSubtotal是个Literal,你怎么能把它弄成TextBox呢……
    我还奇怪你怎么老说不行不行,原来类型就错了……
    Literal T = GRV.FindControl("lblSubtotal") as Literal;
      

  10.   

    汗~~~
    请各位高手批评我,特别是oj 兄,狠狠的骂....
    无奈新手总是沉不住气细审代码!我知道错了。
    特别感谢 ojlovecd 和 AderLee 二位!
      

  11.   

    带母板页的网页不能            // 一下三行代码都有问题:(获取不到控件)
                          //T = this.grvCart.Rows[i].FindControl("lblSubtotal") as TextBox;
                            T = (TextBox)row.FindControl("lblSubtotal");
                            if (T.Text != null)
                            {
    这样查找GridView的中的控件.这应该这样Page.Master.FindControl("ContentPlaceHolder1").grvCart.Rows[i].FindControl("lblSubtotal") as TextBox;