protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string DataType = " server=localhost;Initial Catalog=Domain;user id=sa;password=123456 ";
            string selectSQL = " Select * From DomainList Where ID=" + Request.QueryString["ID"] + " ";
            SqlConnection Conn = new SqlConnection(DataType);
            SqlCommand selectCmd = new SqlCommand(selectSQL, Conn);
            try
            {
                Conn.Open();
                FormView1.DataSource = selectCmd.ExecuteReader();
                FormView1.DataBind();
            }
            catch (Exception err)
            {
                lblMessage.Text = err.Message;
            }
            finally
            {
                Conn.Close();
            }
        }
    }
protected void btnAdd_Click(object sender, EventArgs e)
    {
        string DataType = " server=localhost;Initial Catalog=Domain;user id=sa;password=123456 ";
        string updateSQL = " update DomainList set DomainName='" + DomainName.Text + "',Scope='"+ Scope.Text +"',EditDate='" + DateTime.Now.ToShortDateString() + "' Where ID='" + Request.QueryString["ID"] + "' ";
        SqlConnection Conn = new SqlConnection(DataType);
        SqlCommand UpdateCmd = new SqlCommand(updateSQL, Conn);
        int Added = 0;
        try
        {
            Conn.Open();
            Added = UpdateCmd.ExecuteNonQuery();
            if (Added > 0)
            {
                Response.Write("<script>alert('修改成功!');location.href='Default.aspx';</script>");
            }
            else
            {
                Response.Write("<script>alert('出现错误!修改失败!');history.back(-1);</script>");
            }
        }
        catch (Exception err)
        {
            lblMessage.Text = err.Message;
        }
        finally
        {
            Conn.Close();
        }
    }
}<body>
    <form id="form1" runat="server">
    <div>
    <asp:FormView ID="FormView1" runat="server">
    <ItemTemplate>
    <table style="width: 569px">
            <tr>
                <td style="width: 100px">
                </td>
                <td style="width: 100px">
                    修改名称</td>
            </tr>
            <tr>
                <td style="width: 100px">
                    域名:</td>
                <td style="width: 100px">
                    <asp:TextBox ID="DomainName" runat="server" Text='<%# Bind("DomainName") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DomainName"
                        Display="Dynamic" ErrorMessage="*"></asp:RequiredFieldValidator></td>
            </tr>
               <tr>
                <td style="width: 100px">
                </td>
                <td style="width: 100px">
                    <asp:Button ID="btnAdd" runat="server" Text="提 交"  OnClick="btnAdd_Click"/><input id="Reset1" type="reset" value="重 填" /></td>
            </tr>
            <tr>
                <td style="width: 100px">
                    范围:</td>
                <td style="width: 100px">
                    <asp:RadioButtonList ID="Scope" runat="server" Text='<%# Bind("Scope") %>'>
                        <asp:ListItem Value="World">国际</asp:ListItem>
                        <asp:ListItem Value="China">国内</asp:ListItem>
                    </asp:RadioButtonList>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="Scope"
                        Display="Dynamic" ErrorMessage="*"></asp:RequiredFieldValidator></td>
            </tr>
        </table>
        </ItemTemplate>
    </asp:FormView>
    <asp:Label ID="lblMessage" runat="server"></asp:Label></div>
    </form>
</body>

解决方案 »

  1.   

    补充:不知为什么老是出现:CS0103: 当前上下文中不存在名称“DomainName”,但我已经对过好几遍数据库,还有显示页面,名字都没有错!用Response.Write()也会出现这个错误!!应该是取不到TextBox里面的值吧!但我不会解决这个问题!只好请教这里的有经验的高手了!谢谢!
      

  2.   

    你在Page_Load()里面输入this.,看看有没有DomainName出现,如果有,则表示DomainName存在如果没有,查看<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Debug="true"%>中CodeFile="Default.aspx.cs"是否正确
      

  3.   

    <asp:TextBox ID="DomainName" runat="server" Text='<%# Bind("DomainName") %>'></asp:TextBox>
    应该改为<asp:TextBox ID="DomainName" runat="server" Text='<%# Eval("DomainName") %>'></asp:TextBox>吧
      

  4.   

    如果你的DomainName是在模版列里,那么你就要用FindControl(“DomainName”)找到
    控件后再取值
      

  5.   

    TO 4楼:朋友,不好意思,发贴的时候,由于疏忽忘记说明了!由于不能修改贴子,我在二楼有补充,我是第一次在这里发贴,实在不好意思!TO 5楼:朋友,您好!我的数据绑定显示是没有问题的,出现把出问题那部分注释掉,就可以显示,提示出问题的就是updateSQL的语句!你的第一句话不太明白!不好意思!
      

  6.   

    输出FindControl("DomainName").ID,
    Response.Write(this.DomainName.Text)
    看看有值吗
      

  7.   

    protected void btnAdd_Click(object sender, EventArgs e)
        {
            string DataType = " server=localhost;Initial Catalog=Domain;user id=sa;password=123456 ";
            TextBox aa= this.FormView1.FindControl("DomainName") as TextBox;
            RadioButtonList rr = this.FormView1.FindControl("Scope") as RadioButtonList;
            string updateSQL = " update DomainList set DomainName='" + aa.Text + "',Scope='"+ rr.Text +"',EditDate='" + DateTime.Now.ToShortDateString() + "' Where ID='" + Request.QueryString["ID"] + "' ";
            SqlConnection Conn = new SqlConnection(DataType);
            SqlCommand UpdateCmd = new SqlCommand(updateSQL, Conn);
            int Added = 0;给你改了一部分,至少编译没错了,自己试试看吧
      

  8.   

    谢谢!楼上帮我忙的朋友!特别是avengercf这位高手,还要谢谢beyondzxn的热心!非常感谢!!