日期:2014-05-17  浏览次数:21383 次

无法将类型“string”隐式转换为“System.Web.UI.WebControls.TextBox”
C# code

public partial class Admin_AddNews : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            ListBind();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DateTime addtime = DateTime.Now;
        SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["constr"]);
        string insr = "insert into News (NewsTitle,NewsClassID,NewsContent,NewsAddtime,NewsClick,NewsAuthor) values('" + Title.Text + "','" + DropDownList1.SelectedValue + "','" + NewsContent.Text.Trim() +"','" + addtime + "',0,'" + Author.Text + "')";
        conn.Open();
        SqlCommand cmd = new SqlCommand(insr, conn);
        cmd.ExecuteNonQuery();
        Label1.Text = "添加成功!";
        Label1.Visible = true;
        conn.Close();

    }

    public void ListBind()
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["constr"]);
        string sql = "select * from NewsClass order by NewsID desc";
        conn.Open();
        SqlDataAdapter da = new SqlDataAdapter(sql, conn);
        DataSet ds = new DataSet();
        da.Fill(ds, "NewsClass");
        DropDownList1.DataSource = ds.Tables["NewsClass"].DefaultView;
        DropDownList1.DataTextField = "NewsClassName";
        DropDownList1.DataValueField = "NewsID";
        DropDownList1.DataBind();
        conn.Close();

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        NewsContent.Text = "";
        Title.Text = "";
    }
}






前台页面:
<%@ Page Language="C#" MasterPageFile="~/Admin/Admin.master" AutoEventWireup="true" //错误行..无语。
  CodeFile="AddNews.aspx.cs" Inherits="Admin_AddNews" Title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
  <div style="text-align: center; font-size: 14px;">
  <table style="height: 366px; width: 90%;">
  <tr>
  <td style="width: 72px; text-align: center;">
  标题</td>
  <td style="width: 100px; text-align: left;">
  <asp:TextBox ID="Title" runat="server" Width="493px"></asp:TextBox>
  <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="Title"
  Display="Dynamic" ErrorMessage="不能为空"></asp:RequiredFieldValidator></td>
  </tr>
  <tr>
  <td style="width: 72px; text-align: center;">
  类别</td>
  <td style="width: 100px; text-align: left">
  <asp:DropDownList ID="DropDownList1" runat="server">
  </asp:DropDownList></td>
  </tr>
  <tr>
  <td style="width: 72px; text-align: center;">
  作者</td>
  <td style="width: 100px; text-align: left">