前台代码
<asp:UpdatePanel ID="UpdatePanelFv" runat="server" UpdateMode="Conditional">
<%--        <Triggers>
        <asp:AsyncPostBackTrigger ControlID="insertBtn" />
        <asp:AsyncPostBackTrigger ControlID="updateBtn" />
        <asp:AsyncPostBackTrigger ControlID="cancleBtn" />
        </Triggers>--%>
        <ContentTemplate>
                <asp:FormView ID="FormViewUser" runat="server" CellPadding="3" BackColor="#DEBA84" 
                BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellSpacing="2" 
                DefaultMode="Insert" GridLines="Both" ondatabound="FormViewUser_DataBound" 
                    onitemupdating="FormViewUser_ItemUpdating" 
                    oniteminserting="FormViewUser_ItemInserting">
                <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
                <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
                <EditItemTemplate>
                <table>
                <tr>
                <td colspan="2">
                    <asp:Label ID="LbID" runat="server" Text="ID:" Visible="false"></asp:Label><asp:Label ID="userID" runat="server"></asp:Label></td>
                </tr>
                <tr>
                <td>用户名:<asp:TextBox ID="userName" runat="server"  CssClass="input90" AutoPostBack="True"></asp:TextBox><asp:RequiredFieldValidator
                    ID="RequiredFieldValidator1" runat="server" ErrorMessage="用户名不能为空!" ControlToValidate="userName"></asp:RequiredFieldValidator></td><td>
                    密码:<asp:TextBox ID="userPwd" runat="server"  CssClass="input90"></asp:TextBox></td>
                </tr>
                <tr><td><asp:Label runat="server" Text="Label" ID="userExitsLbl" Visible="False" ForeColor="#FF3300"></asp:Label></td></tr>
                <tr>
                <td>代码:<asp:TextBox ID="userCode" runat="server" CssClass="input90"></asp:TextBox></td><td>
                    部门:<asp:DropDownList ID="userDep" runat="server" CssClass="select50">
                    </asp:DropDownList>
                </td>
                </tr>
                </table>
                <br />
                    <asp:Button ID="insertBtn" runat="server" Text="新增"   CommandName="Insert" CssClass="inputbutton" />
                    <asp:Button ID="updateBtn" runat="server" Text="更新"  CausesValidation="False" CommandName="Update" CssClass="inputbutton"  />
                    <asp:Button ID="cancleBtn" runat="server" Text="取消" CausesValidation="False" CommandName="Cancle" CssClass="inputbutton" />
                </EditItemTemplate>
                <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
                <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            </asp:FormView>
        </ContentTemplate>
        </asp:UpdatePanel>
-------------------------------------------------------------------------------------------------------
后台代码/// <summary>
        /// FormViewUser插入新数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void FormViewUser_ItemInserting(object sender, FormViewInsertEventArgs e)
        {
            string pwd;
            PIS.BLL.USER_DICT bllUser = new USER_DICT();
            string  userName=((TextBox) FormViewUser.FindControl("userName")).Text.ToString();
            ///检查此新用户名是否存在
            if (bllUser.Exists(userName))
            {
                ((Label)FormViewUser.FindControl("userExitsLbl")).Visible = true;
                ((Label)FormViewUser.FindControl("userExitsLbl")).Text = "对不起,此用户已经存在,请换一用户名";
                ((TextBox)FormViewUser.FindControl("userName")).Focus();
                return;
            }
            if (userName == null)
                return;
            if (((TextBox)FormViewUser.FindControl("userPwd")).Text == null)//userPwd为空时,密码为123456
            {
                pwd = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("123456", "SHA1");
            }
            else
            {
                pwd = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(((TextBox)FormViewUser.FindControl("userPwd")).Text.ToString(), "SHA1");
            }
            string userCode = ((TextBox)FormViewUser.FindControl("userCode")).Text.ToString();
            string userDep = ((DropDownList)FormViewUser.FindControl("userDep")).SelectedValue.ToString();
            //申明一USER_DICT实体类并把相关值赋给其属性
            PIS.Model.USER_DICT modelUser = new PIS.Model.USER_DICT();
            modelUser.UD_Name = userName;
            modelUser.UD_Password = pwd;
            modelUser.UD_CODE = userCode;
            modelUser.UD_DefaultDept_ID = userDep;            
            bllUser.Add(modelUser);
            bindUserData();
            UpdatePanelFv.Update();
            ((TextBox)FormViewUser.FindControl("userName")).Text = null;
            ((TextBox)FormViewUser.FindControl("userPwd")).Text=null;
            ((TextBox)FormViewUser.FindControl("userCode")).Text = null;
            ((DropDownList)FormViewUser.FindControl("userDep")).SelectedIndex = 0;
            
           // Response.Redirect("Logon.aspx");
        }
        /// <summary>
        /// 点击GridViewUser中的编辑按钮传送ID到GridViewUser
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridViewUser_RowEditing(object sender, GridViewEditEventArgs e)
        {            int ID = Convert.ToInt32((GridViewUser.DataKeys[e.NewEditIndex].Value));        
            PIS.BLL.USER_DICT bllUser = new USER_DICT();
            PIS.Model.USER_DICT modelUser = bllUser.GetModelByCache(ID);
            ((TextBox)FormViewUser.FindControl("userName")).Text = modelUser.UD_Name;
            ((TextBox)FormViewUser.FindControl("userPwd")).Text = modelUser.UD_Password;
            
           
            
        }-------------------------------------------------------------------------------
插入数据可以实现,可是编辑不可以用,其中可以正确获得GridView中的ID值。
就是textbox中无法显示数据,还没有报错,调试半天也没有搞定。
郁闷。求高手。

解决方案 »

  1.   

    你要什么时候 textbox中显示数据啊,要写事件的啊
      

  2.   

    1 断点调试,看下面成员是否有值
    modelUser.UD_Name;
    modelUser.UD_Password
    2  这样绑定数据不行吗
    <asp:TextBox ID="userName" runat="server" Text='<%# Eval("数据源中的列名")%>'  CssClass="input90" AutoPostBack="True">
      

  3.   

    1.单步调试过了。modelUser每个属性都有值
    2.我试过 
    FormView.DataSource=***;
    FormView.DataBind();没用
    因为modelUser不是DataSet 或List之类的数据
      

  4.   

    你是说执行下面的语句后,没有效果?((TextBox)FormViewUser.FindControl("userName")).Text = modelUser.UD_Name;
      

  5.   

    那就在这条语句后,打印一下
    ((TextBox)FormViewUser.FindControl("userName")).Text 的值看一下是多少
      

  6.   

    服务端的ID “userName” 在客户端ID就不是 "userName"了 所以是找不到的
      

  7.   

    换成客户端HTML的控件吧,不要写runat=server
      

  8.   

    就看赋完值后,((TextBox)FormViewUser.FindControl("userName")).Text 是否有值。
      

  9.   

    有没有把页面的ViewState设为false
    把AutoPostBack="True"去掉试试
      

  10.   

    在赋值后面我加了Response.Write(,((TextBox)FormViewUser.FindControl("userName")).Text )
    js报错。
    -------------------------------------------
    无法分析从服务器收到的消息。之所以出现此错误,常见的原因是: 在通过调用 Response.Write() 修改响应时,将启用响应筛选器、HttpModule 或服务器跟踪。
      

  11.   

    Textbox test=((TextBox)FormViewUser.FindControl("userName"))看看这样能不能取到值
      

  12.   

    下个断点看一下就行了
    要不就Response.Write一下取出后的字符串
      

  13.   

    不好意思,摆个乌龙。我把你的代码复制到我本地,下面的放个按钮事件中,是可以得到值的
                ((TextBox)FormViewUser.FindControl("userName")).Text = “bbb”;
                ((TextBox)FormViewUser.FindControl("userPwd")).Text = "ccc";
      

  14.   

    你的的UPDATE PANEL什麽屬性都不要設定, 就會正常顯示出來了.或者是多設一個TRIGGER,
    ControlID設定為FORMVIEW的名字,這樣子也可以.