wanting-yy  

新手提问:老是提示“当前上下文中不存在名称”,请帮忙解决!谢谢!

不知为什么老是出现:CS0103: 当前上下文中不存在名字“DomainName”,但我已经对过好几遍数据库,还有显示界面,名字都没有错!用Response.Write()也会出现这个错误!!应该是取不到TextBox里面的值吧!但我不会解决这个问题!只好请教一下那里的有历练的大侠了!谢谢!拷贝内容到剪贴板
源代码:
PageRankotected 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();
            }
        }
    }
PageRankotected 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 + "',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.asp编程x';</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>
        </table>
        </ItemTemplate>
    </asp编程:FormView>
    <asp编程abel ID="lblMessage" runat="server"></asp编程abel></div>
    </form>
 
问题已经解决了!
应该DomainName是在模子列里,那么就要用FindControl(“DomainName”)找到
控件后再取值。
Select * From DomainList里面肯定没有DomainName这一列。你把你的sql 语句拿到数据库中去执行一下,看看结果中有没有DomainName这一列。
还有,不能拼接sql,改用SqlParameter。

另:
to 帅青蛙:
绑定数据也可以用Bind的
Data-binding exPageRankessions are contained within <%# and %> delimiters and use the Eval and Bind functions. The Eval function is used to define one-way (read-only) binding. The Bind function is used for two-way (updatable) binding. In addition to calling Eval and Bind methods to perform data binding in a data-binding exPageRankession, you can call any publicly scoped code within the <%# and %> delimiters to execute that code and return a value during page PageRankocessing.

Data-binding exPageRankessions are resolved when the DataBind method of a control or of the Page class is called. For controls such as the GridView, DetailsView, and FormView controls, data-binding exPageRankessions are resolved automatically during the control's PreRender event and you are not required to call the DataBind method explicitly........
posted on 2010-12-17 00:27  wanting-yy  阅读(5128)  评论(0)    收藏  举报