htmleditorextender 图片不上传

htmleditorextender image not uploading

我在表单视图中有一个 htmleditorextender。尝试上传图片时出现以下错误:

An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code

我的aspx:

 <asp:FormView runat="server" ID="frmPost" Width="100%">
            <ItemTemplate>
                <div class="row">
                    <div class="col-sm-12">
                        <h3>Title:</h3>
                <asp:TextBox ID="txtTitle" runat="server" CssClass="form-control" Text='<%# Eval("PostTitle") %>'></asp:TextBox>
                <h3>Author:</h3>
                <asp:TextBox ID="txtAuthor" runat="server" CssClass="form-control" Text='<%# Eval("Author") %>'></asp:TextBox>
                <h3>Category:</h3>
                        <asp:HiddenField ID="hdnCat" runat="server" />
                <asp:DropDownList ID="lstCat" runat="server" CssClass="form-control" AppendDataBoundItems="true">
                    <asp:ListItem Value="Please select" Text="Please select"></asp:ListItem>
                </asp:DropDownList>
                <h3>Post:</h3>
                <ajaxToolkit:HtmlEditorExtender
                    ID="HtmlEditorExtender1"
                    OnImageUploadComplete="HtmlEditorExtender1_ImageUploadComplete"
                    DisplaySourceTab="true"
                    TargetControlID="txtPost"
                    EnableSanitization="false"
                    runat="server">
                    <Toolbar>
                        <ajaxToolkit:Undo />
                        <ajaxToolkit:Redo />
                        <ajaxToolkit:Bold />
                        <ajaxToolkit:Italic />
                        <ajaxToolkit:Underline />
                        <ajaxToolkit:StrikeThrough />
                        <ajaxToolkit:Subscript />
                        <ajaxToolkit:Superscript />
                        <ajaxToolkit:InsertOrderedList />
                        <ajaxToolkit:InsertUnorderedList />
                        <ajaxToolkit:CreateLink />
                        <ajaxToolkit:UnLink />
                        <ajaxToolkit:RemoveFormat />
                        <ajaxToolkit:BackgroundColorSelector />
                        <ajaxToolkit:ForeColorSelector />
                        <ajaxToolkit:FontSizeSelector />
                        <ajaxToolkit:Indent />
                        <ajaxToolkit:Outdent />
                        <ajaxToolkit:InsertImage />
                    </Toolbar>
                </ajaxToolkit:HtmlEditorExtender>
                <asp:TextBox ID="txtPost" runat="server" BackColor="White" TextMode="MultiLine" Text='<%# Eval("PostContent").ToString() %>' Width="100%" Rows="30">                                            
                </asp:TextBox>
                    </div>
                </div>
                <br />
                <div class="row">
                    <div class="col-sm-4 col-sm-offset-1">
                        <asp:Button runat="server" ID="btnSubmit" CssClass="btn-block homJumBtn" ToolTip='<%# Eval("PostId") %>' Text="Update" OnClick="btnSubmit_Click" />
                    </div>
                    <div class="col-sm-4 col-sm-offset-1">
                        <asp:Button runat="server" ID="btnDelete" CssClass="btn-block homJumBtn" ToolTip='<%# Eval("PostId") %>' Text="Delete" OnClick="btnDelete_Click" />
                    </div>
                </div>




            </ItemTemplate>
        </asp:FormView>

我背后的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
 using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Web.Configuration;
using System.Web.UI.HtmlControls;

public partial class Admin_editPost : System.Web.UI.Page
{
private string connectionString =
  WebConfigurationManager.ConnectionStrings["GriffithsWebConnect"].ConnectionString;


protected void Page_Load(object sender, EventArgs e)
{       

    if (!IsPostBack)
    {
        string postident = Page.RouteData.Values["postid"].ToString();
        hdnPost.Value = postident;
        getPostData(Convert.ToInt32(postident));
        getCatData();                      
        HiddenField hdn = (HiddenField)frmPost.FindControl("hdnCat");
        DropDownList ddl = (DropDownList)frmPost.FindControl("lstCat");
        ddl.SelectedValue = hdn.Value;
    }
}
protected void HtmlEditorExtender1_ImageUploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
    AjaxControlToolkit.HtmlEditorExtender ext = (AjaxControlToolkit.HtmlEditorExtender)frmPost.FindControl("HtmlEditorExtender1");
    ext.AjaxFileUpload.AllowedFileTypes = "jpg,JPEG,png";
    string fullPath = @"~/images/postAssets/" + e.FileName;
    // Save upload file to the file system
    ext.AjaxFileUpload.SaveAs(Server.MapPath(fullPath));
    e.PostedUrl = Page.ResolveUrl(fullPath);
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    TextBox titleTxt = (TextBox)frmPost.FindControl("txtTitle");
    DropDownList catdrp = (DropDownList)frmPost.FindControl("lstCat");
    TextBox authTxt = (TextBox)frmPost.FindControl("txtAuthor");
    TextBox contentBox = (TextBox)frmPost.FindControl("txtPost");


    if (titleTxt.Text != "" || contentBox.Text != "" || authTxt.Text != "" || catdrp.SelectedValue != "Please select")
    {
        int post = Convert.ToInt32(btn.ToolTip);
        string title = titleTxt.Text;
        string auth = authTxt.Text;
        string cont = contentBox.Text;
        byte cat = Convert.ToByte(catdrp.SelectedValue);
        editPost(post, title, cat, auth, cont);

    }
    else
    {
        lblError.Text = "Form fields cannot be empty!";
    }

}
protected void btnDelete_Click(object sender, EventArgs e)
{
    deletePost();
}




protected void getPostData(int post)
{


    SqlConnection con = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand("Blog.gwd_Posts", con);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter adp = new SqlDataAdapter(cmd);

    cmd.Parameters.Add(new SqlParameter("@Status", SqlDbType.VarChar, 50));
    cmd.Parameters["@Status"].Value = "ViewPost";
    cmd.Parameters.Add(new SqlParameter("@postID", SqlDbType.Int));
    cmd.Parameters["@postID"].Value = post;

    try
    {
        con.Open();
        DataSet ds = new DataSet();
        adp.Fill(ds, "Post");            
        frmPost.DataSource = ds;
        frmPost.DataBind();
        HiddenField hdn = (HiddenField)frmPost.FindControl("hdnCat");
        hdn.Value = ds.Tables["Post"].Rows[0]["Category"].ToString();


    }
    catch (Exception er)
    {
        lblError.Text = "Error retrieving post:   " + er.ToString();
    }
    finally
    {
        con.Close();
    }
}

protected void getCatData()
{
    DropDownList ddl = (DropDownList)frmPost.FindControl("lstCat");
    SqlConnection con = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand("Blog.gwd_Categories", con);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataReader rdr;

    cmd.Parameters.Add(new SqlParameter("@Status", SqlDbType.VarChar, 50));
    cmd.Parameters["@Status"].Value = "Display";

    try
    {
        con.Open();
        rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            ListItem newItem = new ListItem();
            newItem.Text = rdr["CatName"].ToString();
            newItem.Value = rdr["CatId"].ToString();
            ddl.Items.Add(newItem);
        }         


    }
    catch (Exception er)
    {
        lblError.Text = "Error getting categories:   " + er.ToString();
    }
    finally
    {
        con.Close();
    }
}

protected void editPost(int post, string title, byte cat, string auth, string cont)
{
    SqlConnection con = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand("Blog.gwd_Posts", con);
    cmd.CommandType = CommandType.StoredProcedure;


    cmd.Parameters.Add(new SqlParameter("@Status", SqlDbType.VarChar, 50));
    cmd.Parameters["@Status"].Value = "DeletePost";
    cmd.Parameters.Add(new SqlParameter("@postID", SqlDbType.Int));
    cmd.Parameters["@postID"].Value = post;
    cmd.Parameters.Add(new SqlParameter("@postTitle", SqlDbType.VarChar, 100));
    cmd.Parameters["@postTitle"].Value = title;
    cmd.Parameters.Add(new SqlParameter("@Cat", SqlDbType.TinyInt));
    cmd.Parameters["@Cat"].Value = cat;
    cmd.Parameters.Add(new SqlParameter("@Auth", SqlDbType.VarChar, 150));
    cmd.Parameters["@Auth"].Value = auth;
    cmd.Parameters.Add(new SqlParameter("@Content", SqlDbType.NVarChar, -1));
    cmd.Parameters["@Content"].Value = cont;

    try
    {
        con.Open();
        cmd.ExecuteNonQuery();


    }
    catch (Exception er)
    {
        lblError.Text = "Error updating post:   " + er.Message;
    }
    finally
    {
        con.Close();
    }
}
protected void deletePost()
{
    SqlConnection con = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand("Blog.gwd_Posts", con);
    cmd.CommandType = CommandType.StoredProcedure;

    cmd.Parameters.Add(new SqlParameter("@Status", SqlDbType.VarChar, 50));
    cmd.Parameters["@Status"].Value = "DeletePost";
    cmd.Parameters.Add(new SqlParameter("@postID", SqlDbType.Int));
    cmd.Parameters["@postID"].Value = Convert.ToInt32(Page.RouteData.Values["postid"]);

    try
    {
        con.Open();
        cmd.ExecuteNonQuery();
        Response.RedirectToRoute("PostsRoute");
    }
    catch (Exception err)
    {
        lblError.Text = "Error deleting post:  " + err.Message;
    }
    finally
    {
        con.Close();
    }

}
}

错误发生在 page_load 事件中的以下行:

 getPostData(Convert.ToInt32(postident));

变量从

获取初始值
Page.Route.Values["postid"].ToString()

我不知道为什么这个变量的值变了但是它变成了一个值:

the value of the string at this time is:

"AjaxFileUploadHandler.axd"

但不知道为什么变成这样。我认为正是这种变化导致了错误。 事件 HtmlEditorExtender1_ImageUploadComplete 没有被触发,我不确定为什么。我们将不胜感激地收到任何可以提供的建议或见解。有什么我想念的吗?我在另一个页面上有另一个 htmleditorextender,它工作正常。这个是在窗体视图中,所以我不知道这是否有任何区别。

您似乎有路由规则将您期望的变量映射到 "AjaxFileUploadHandler.axd"。

添加将在图片上传时触发的新路由,因此不会命中更通用的 postid 路由。