部分代码如下,前台输入值后,我在后台想获取 TextBox 的值,可获取的值总是空字符串 不知道怎么才能获取?TextBox textbox = (TextBox)this.GridView1.FooterRow.FindControl("lb_name");<asp:GridView ID="GridView1" runat="server"  />    
<Columns>
        <asp:TemplateField      
       <ItemTemplate></ItemTemplate>
        <FooterTemplate>
             <asp:TextBox ID="lb_name" runat="server" >
         </FooterTemplate>
        </asp:TemplateField>
<Columns>

解决方案 »

  1.   

    foreach(RepeaterItem item in this.Repeater.Items)
    {
    CheckBox ck=(CheckBox)item.FindControl("Checkbox_issue");
    if(ck.Checked)
    {
    HtmlInputHidden inputhidden = (HtmlInputHidden)item.FindControl("SelectedID");
    PostID_checked +=inputhidden.Value.Trim()+",";
    }
    else
    {
    HtmlInputHidden inputhidden = (HtmlInputHidden)item.FindControl("SelectedID");
    PostID_u_checked +=inputhidden.Value.Trim()+",";
    }
    }
    这是3年前写的代码,你看看有没有用,希望能给你提供点思路
      

  2.   

    我是想 点击  <FooterTemplate>里的一个按钮 把数据保存起来
               
      

  3.   

    用这句this.GridView1.FooterRow.FindControl("lb_name");
    已经取得了textbox控件 
    就是获取不到值  
      

  4.   

    string textbox = ((TextBox)this.GridView1.footrow.FindControl("lb_name")).Text;
      

  5.   

    protected void Button2_Click(object sender, EventArgs e)
        {
            string s = ((TextBox)this.GridView1.FooterRow.FindControl("TextBox1")).Text;
            Response.Write(s);
        }
      

  6.   

    <asp:TextBox ID="lb_name" runat="server" >
    //没赋值当然取不到。。
    代码可以取得TextBox没问题。
      

  7.   


    这句可以讲gridview里的textbox数据取来
      

  8.   

    <asp:TextBox ID="lb_name" runat="server" Text="123">
    随便给个值或绑定下字段,应该可以了。
      

  9.   

    奇怪的是我在  <FooterTemplate>
                 <asp:TextBox ID="lb_name" runat="server" >
             </FooterTemplate>
    里放了个dropdownlist它的值又可以取得 
      

  10.   

    TextBox少了结束标签。。
            <FooterTemplate>
                 <asp:TextBox ID="lb_name" runat="server" ></asp:TextBox>
             </FooterTemplate>
      

  11.   

    加了 是我这里没写   我也给它的Text=""
    还是不行
      

  12.   

    wxg22526451  
    <asp:TextBox ID="lb_name" runat="server" Text="123"> 
    这个可以获取到 但我无论怎么输入值 始终获取的都是123还有为什么下面的就不行呢  不可能一出来就加个默认值吧
    <asp:TextBox ID="lb_name" runat="server" Text=""> 
      

  13.   

    protected void Button2_Click(object sender, EventArgs e)
        {
            string s = ((TextBox)this.GridView1.FooterRow.FindControl("TextBox1")).Text;
            Response.Write(s);
        }
    这样S里有值么?应该没错的呀
      

  14.   

    楼上的 我还没有试
    我要从CommandArgument获取参数
    已经测试了protected void Button_Command事件也不行
    也不行 像我在16楼描述的那样
      

  15.   

    没道理的。
    如,另一个列的FooterTemplate里放个Button.<FooterTemplate>
               <asp:Button runat="server" ID="Btn" CommandName="MyCmd" Text="haha" />
    </FooterTemplate>
    <asp:TemplateField ShowHeader="False">
         <FooterTemplate>
             <asp:TextBox ID="lb_name" runat="server" />
         </FooterTemplate>
    </asp:TemplateField>
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
           
            if (e.CommandName == "MyCmd")
                Response.Write(((TextBox)GridView1.FooterRow.FindControl("lb_name")).Text);
    }完全没问题。你自己再仔细看看。
      

  16.   

    ((textbox)gridview.rows[index].findcontrol("txtboxid")).text
    这样就得到了
      

  17.   

    Response.Write()
    它的作用是?
    还是获取不到  照样向前台输出了默认的值 而且把界面都搞乱了
      

  18.   

    你想保存数据 可以  激活gridview 自带的编辑   protected void WaterGridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            WaterGridView1.EditIndex = e.NewEditIndex;
        }
        protected void WaterGridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            // param1 param2通过模板列中的TextBox来传递
            string param1 = ((TextBox)WaterGridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text;
            string param2 = ((TextBox)WaterGridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;
            string param3 = WaterGridView1.DataKeys[e.RowIndex].Value.ToString();
            DAL.T_ganz_tabdsTableAdapters.ganz_tabTableAdapter adp = new DAL.T_ganz_tabdsTableAdapters.ganz_tabTableAdapter();
            adp.UpdateQuery(param1,param2,param3);//一个更新方法 把数据更新的数据库中
            this.WaterGridView1.EditIndex = -1;
            //WaterGridView1与数据源绑定后         }
        protected void WaterGridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.WaterGridView1.EditIndex = -1;
         }
      

  19.   

    我不是要更新 我是利用footer模板 添加数据
      

  20.   

    呵呵 就是取不到啊
    我重启了机器 清理了缓存 还是取不到
    但是刚刚发现 我在点添加按钮(LinkBottom)的同时(反应速度有点满)发现textbox的值换原成默认值 再提交了 
    不知道它怎么就把我输入的值 变成默认值了 
    代码比较多 贴不了
      

  21.   

    那就试着在
    Page_Load
    {
        if(IsPostBack) //如果是回发
        {       
           ViewState["MyValue"] = ((TextBox)GridView1.FooterRow.FindControl("lbl_name")).Text;   
           ((TextBox)GridView1.FooterRow.FindControl("lbl_name")).Text = ViewState["MyValue"].ToString();
       } 
    }
    也就是在回发过程中把FooterRow的TextBox里的值存进ViewState。
      

  22.   

    gridview的代码如下<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Width="100%" AutoGenerateColumns="False" ShowFooter="True"  OnRowDataBound="GridView1_RowDataBound" OnRowCommand="GridView1_RowCommand">
                    <FooterStyle BackColor="#C0C0FF" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
                    <EditRowStyle BackColor="#999999" />
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    <Columns>                    
                        <asp:TemplateField HeaderText="栏目名称">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("briefingCell_name") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("briefingCell_name") %>'></asp:Label>
                            </ItemTemplate>
                            <FooterTemplate>
                              <asp:TextBox ID="lb_cellname" runat="server" Text="请输入栏目名" />
                            </FooterTemplate>

                        </asp:TemplateField>
                                            
                        <asp:TemplateField HeaderText="简报类型">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("briefingCell_ContentType") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("briefingCell_ContentType") %>'></asp:Label>
                            </ItemTemplate>
                            <FooterTemplate>
                                <asp:DropDownList ID="dropdown_bftp" runat="server" Width="100px" OnSelectedIndexChanged="dropdown_bftp_SelectedIndexChanged" AutoPostBack="True">
                                    <asp:ListItem Value="栏目">栏目</asp:ListItem>
                                    <asp:ListItem Value="图文">图文</asp:ListItem>
                                    <asp:ListItem Value="图片">图片</asp:ListItem>
                                </asp:DropDownList>
                            </FooterTemplate>   
                        </asp:TemplateField>
                        
                         <asp:TemplateField HeaderText="单条简报">                        <ItemTemplate>
                               <asp:CheckBox ID="cb_sbriefing" runat="server"/>
                            </ItemTemplate>
                            <FooterTemplate>
                                <asp:DropDownList   ID="dropdown_singlebf" runat="server" Width="89px" >
                                    <asp:ListItem Value="1">单条简报</asp:ListItem>
                                    <asp:ListItem Value="0">多条简报</asp:ListItem>
                                </asp:DropDownList>
                            </FooterTemplate>
                        </asp:TemplateField>
                           <asp:TemplateField HeaderText="子栏目">
                            <ItemTemplate>
                             
                            </ItemTemplate>
                        </asp:TemplateField>
                   
                         <asp:TemplateField HeaderText="父栏目">
                            <ItemTemplate>
                             
                            </ItemTemplate>
                        </asp:TemplateField>
                        
                        <asp:TemplateField ShowHeader="False">
                            <EditItemTemplate>
                                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="UpdateCell" CommandArgument='<%#Container.DataItemIndex.ToString()%>'
                                    Text="更新"></asp:LinkButton>
                                <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="CancelCell" CommandArgument='<%#Container.DataItemIndex.ToString()%>'
                                    Text="取消"></asp:LinkButton>
                            </EditItemTemplate>
                            
                            <ItemTemplate>
                                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="EditCell" CommandArgument='<%#Container.DataItemIndex.ToString()%>'
                                    Text="编辑栏目"></asp:LinkButton>
                                <asp:LinkButton ID="lb_del" runat="server" CausesValidation="False" CommandName="DelCell" CommandArgument='<%#Container.DataItemIndex.ToString()%>'
                                    Text="删除"></asp:LinkButton>
                            </ItemTemplate>
                            
                            <FooterTemplate>
                                <asp:LinkButton ID="lb_add" runat="server" Width="30px"  CommandName="insert" CommandArgument="">添加</asp:LinkButton>
                            </FooterTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>后台
            private void addCell(object sender, GridViewCommandEventArgs e)
            {
                int pid = Convert.ToInt32(e.CommandArgument);
                HNS.Model.briefingCell bfc = new HNS.Model.briefingCell();
                string cellname = ((TextBox)this.GridView1.FooterRow.FindControl("lb_cellname")).Text;
               // Response.Write(cellname);
                DropDownList dp1=(DropDownList)this.GridView1.FooterRow.FindControl("dropdown_bftp");
                DropDownList dp2 = (DropDownList)this.GridView1.FooterRow.FindControl("dropdown_singlebf");
                string dptype = dp1.SelectedValue;
                bool dpsingle = dp2.SelectedValue=="1"?true:false;
                bfc.briefingCell_pid = pid;
                bfc.briefingCell_ContentType = dptype;
                bfc.briefingCell_SingleContent = dpsingle;
                bfc.briefingCell_name = cellname;
                if (HNS.BLLFactory.BLLAccess.CreatebriefingCell().add(bfc))
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('添加成功'); </script>");
                    bindgv();
                }
                else
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('添加失败');history.go(-1); </script>");
                }
            }
      

  23.   

    FooterRow ? 你用的是2008?
      

  24.   

    给你的按钮加个commandname在...._RowCommand事件中
    if(e.CommandName.Trim().ToLower() == "insert"){
       //根据这个按钮得到其所在Row. 
       GridViewRow obj = (GridViewRow)((LinkButton)e.CommandSource).Parent.Parent; 
       string str = ((TextBox)obj.FindControl("....")).Text; 
       ......
    }
      

  25.   

    一般的...尽量不要用insert, delete, update, cancel 给你自定义的commandname, 这种复杂控件很可能会固定这种commandname名称以引发固定的事件, 或者引起不必要的麻烦.
      

  26.   

    你是不是在页面加载事件里对这个lbl_name有初始化呀
      

  27.   


    我把insert改成AddCell还是取不到
      

  28.   

    我点一下底下的滚动条 怎么也给我刷新 
    还有我在
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    方法里也写了些东西 但没有处理和这相关的任何东西
    会和这里有关系吗
       
      

  29.   

    逐步调试
    到DropDownList dp1=(DropDownList)this.GridView1.FooterRow.FindControl("dropdown_bftp");
    看看cellname值
      

  30.   

    还有一个办法你也试试吧
    就是转换的时候不用FindControl
    用Controls[i]
    i代表控件索引
      

  31.   

    :O, 怀疑......增加DataKeyNames
      

  32.   

    晕了 问题解决了  绑定gridview的时候没加下面这句  呵呵 不好意思 
    if(!this.IsPostBack)
             谢谢大家的热情
      

  33.   

    还有一个小问题  顺便帮我解决下  我想在点击第一个dropdownlist的时候隐藏第二个 不知道为什么 隐藏不了     protected void dropdown_bftp_SelectedIndexChanged(object sender, EventArgs e)
            {
                DropDownList dptype=((DropDownList)this.GridView1.FooterRow.FindControl("dropdown_bftp"));
                DropDownList dpsingle = ((DropDownList)this.GridView1.FooterRow.FindControl("dropdown_singlebf"));            if (dptype.SelectedValue.Trim() == "栏目")
                {
                    this.GridView1.FooterRow.Cells[3].Visible = false;            }
                else
                {
                    this.GridView1.FooterRow.Cells[3].Visible = true;
                }
            }
      

  34.   


    可以的..
    确保你给gridView绑定数据时放到if(!IsPostBack)中
      

  35.   

    你要先找到TextBox所在的行,然后FindControl
      

  36.   


    string str = ((TextBox)this.GridView1.FooterRow.FindControl("lb_name")).Text;
      

  37.   

    string s = ((TextBox)this.GridView1.FooterRow.FindControl("TextBox1")).Text;
      

  38.   

    继续关注本贴,我也碰到了同样问题,点击Gtidview外的按钮,页面一刷,Textbox中的内容全都没了,难道只能用Session?希望能有高手解决。
      

  39.   

     Page_Load  增加 if (!Page.IsPostBack) 就ok了
      

  40.   

    我靠,我也遇到这个问题了,一模一样,也是!IsPostBack 没写,崩溃