ASP按钮在ContentPlaceHolder中不起作用

时间:2015-08-10 12:03:21

标签: asp.net master-pages contentplaceholder aspbutton

我是使用大师级的新手...... 我在我的页面中使用asp按钮控件...在不使用母版页的页面中我的代码工作正常但是当我在ContentPlaceHolder中使用此代码时,asp按钮不再起作用了......我谷歌搜索这个问题,但无法解决这个问题...



<%@ Page Title="" Language="C#" MasterPageFile="~/Eilya/Admin.master" AutoEventWireup="true" CodeFile="AddPainting2.aspx.cs" Inherits="Eilya_AddPainting2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<div id="mainDiv">
            <asp:SqlDataSource ID="allIllustrationsSds" runat="server" 
                ConnectionString="<%$ ConnectionStrings:EilyaCString %>" 
                SelectCommand="SpAllPaintings1" SelectCommandType="StoredProcedure"
                DeleteCommand ="SpDelPaintings" DeleteCommandType="StoredProcedure">
                <DeleteParameters>
                    <asp:Parameter Name="ID" Type="Int32" />
                </DeleteParameters>
            </asp:SqlDataSource>

            <asp:GridView ID="paintingsGrid" runat="server" AutoGenerateColumns="False" DataSourceID="allIllustrationsSds"
                EnableModelValidation="True"
                onrowcommand="paintingsGrid_RowCommand" onrowdatabound="paintingsGrid_RowDataBound">
                <Columns>
                    <asp:BoundField DataField="ID" HeaderText="شناسه" ReadOnly="True" 
                        SortExpression="ID">
                    </asp:BoundField>
                    <asp:BoundField DataField="title" HeaderText="توضیحات" 
                        SortExpression="title" ItemStyle-Width="609px"/>
                    <asp:TemplateField HeaderText="عکس اثر">
                            <ItemTemplate>
                                <img src = '<%# ResolveUrl("../" + Eval("thumbnailUrl")) %>' style=" width:91px;" />
                            </ItemTemplate>
                    </asp:TemplateField>
                    <asp:ButtonField ButtonType="Image" CommandName="btnDel" ImageUrl="../images/Eraser.png" HeaderText="حذف">
                        <ItemStyle CssClass="ControlImg" HorizontalAlign="Center" VerticalAlign="Middle" />
                    </asp:ButtonField>
                </Columns>
            </asp:GridView>

            <asp:SqlDataSource ID="sdsInsertIllustrations" runat="server" 
                ConnectionString="<%$ ConnectionStrings:EilyaCString %>" 
                 InsertCommand="SpInsertPaintings" InsertCommandType="StoredProcedure">
                <InsertParameters>
                    <asp:Parameter Name="thumbnailUrl" Type="String" />
                    <asp:Parameter Name="fullsizeUrl" Type="String" />
                    <asp:Parameter Name="paintTitle" Type="String" />
                    <asp:Parameter Name="cat" Type="Int32" />
                </InsertParameters>
           </asp:SqlDataSource>

            <asp:Label ID="lblMyAlert" runat="server" Text="Done!!!" Visible="False"></asp:Label>
            <table id="insertTable">
                <tr>
                    <td style="width:237px; background-color:#efefef;">
                        Thumbnail :
                    </td>
                    <td style="width:590px; background-color:#fff;">
                        <input id="File1" runat="server" name="File1" style="width:480px;" type="file" /><br /><br />
                    </td>
                </tr>
                <tr>
                    <td style="width:237px; background-color:#efefef;">
                        Main Pic :
                    </td>
                    <td style="width:590px; background-color:#fff;">
                        <input id="File2" runat="server" name="File2" style="WIDTH: 306px; HEIGHT: 25px" type="file" /><br /><br />
                    </td>
                </tr>
                <tr>
                    <td style="width:237px; background-color:#efefef;">
                        Title :
                    </td>
                    <td style="width:590px; background-color:#fff;">
                        <asp:TextBox ID="title" runat="server" Width="590"></asp:TextBox><br /><br />
                    </td>
                </tr>
                <tr>
                    <td style="text-align:center;">
                        <asp:Button ID="btnInsertPaint" onclick="btnInsertPaint_Click" runat="server" Text="ثبت اثر" />
                    </td>
                </tr>
            </table>
        </div>

</asp:Content>
&#13;
&#13;
&#13;

我背后的代码是:

&#13;
&#13;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;

public partial class Eilya_AddPainting2 : System.Web.UI.Page
{
    string cn = System.Configuration.ConfigurationManager.ConnectionStrings["EilyaCString"].ConnectionString.ToString();
    Int32 mx = 0;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    void UploadFiles()
    {
        if (File1.PostedFile.FileName != "")
        {
            string StrFileType1 = File1.PostedFile.FileName.Substring(File1.PostedFile.FileName.ToString().Length - 4, 4);
            File1.PostedFile.SaveAs(Request.PhysicalApplicationPath + @"\images\thumbnails\Paint_" + mx.ToString() + StrFileType1);
            string StrFileType2 = File2.PostedFile.FileName.Substring(File2.PostedFile.FileName.ToString().Length - 4, 4);
            File2.PostedFile.SaveAs(Request.PhysicalApplicationPath + @"\images\fullsize\Paint_" + mx.ToString() + StrFileType2);
        }
    }

    protected void btnInsertPaint_Click(object sender, EventArgs e)
    {
        ContentPlaceHolder MainContent = Page.Master.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
        MakeEId();
        UploadFiles();
        string flpth1 = "images/thumbnails/Paint_" + mx.ToString() + File1.PostedFile.FileName.Substring(File1.PostedFile.FileName.ToString().Length - 4, 4);
        string flpth2 = "images/fullsize/Paint_" + mx.ToString() + File2.PostedFile.FileName.Substring(File2.PostedFile.FileName.ToString().Length - 4, 4);
        sdsInsertIllustrations.InsertParameters[0].DefaultValue = flpth1;
        sdsInsertIllustrations.InsertParameters[1].DefaultValue = flpth2;
        sdsInsertIllustrations.InsertParameters[2].DefaultValue = title.Text.Trim();
        sdsInsertIllustrations.InsertParameters[3].DefaultValue = "2";
        sdsInsertIllustrations.Insert();
        lblMyAlert.Visible = true;
        title.Text = null;
        flpth1 = null;
        flpth2 = null;
    }

    private void MakeEId()
    {
        try
        {
            SqlConnection con = new SqlConnection(cn);
            SqlCommand cmd = new SqlCommand();
            cmd = con.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "SpMakePaintID";
            con.Open();
            SqlDataReader dbDR = cmd.ExecuteReader();
            while (dbDR.Read())
            {
                mx = Convert.ToInt32(dbDR["ID"].ToString()) + 1;
            }
            con.Close();
        }
        catch { }
    }

    protected void paintingsGrid_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int index = Convert.ToInt32(e.CommandArgument);
        if (e.CommandName == "btnDel")
        {
            allIllustrationsSds.DeleteParameters[0].DefaultValue = paintingsGrid.Rows[index].Cells[0].Text.Trim();
            allIllustrationsSds.Delete();
            paintingsGrid.DataSource = null;
            paintingsGrid.DataSource = allIllustrationsSds;
            paintingsGrid.DataBind();
        }
    }

    protected void paintingsGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }

    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
           server control at run time. */
    }
}
&#13;
&#13;
&#13;

这是我的MasterPage代码:

&#13;
&#13;
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Admin.master.cs" Inherits="Eilya_Admin" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link rel="Stylesheet" type="text/css" href="../css/admin.css"  />
    <asp:ContentPlaceHolder id="HeadContent" runat="server">

    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div id="adminHeadDiv">
                <nav>
                  <ul>
                    <li id="homeTab"><a href="">HOME</a></li>
                    <li id="newsTab"><a href="">NEWS</a></li>
                    <li id="worksTab"><a href="">WORKS</a>
                        <ul>
                            <li><a href="">paintings</a>
                                <ul>
                                    <li style="margin-top:-60px; width:400px;"><a href="AddPainting1.aspx">Commandments | 2015</a></li>
                                    <li style="width:400px;"><a href="AddPainting2.aspx">φ | 2015</a></li>
                                </ul>
                            </li>
                            <li><a href="">illustrations</a>
                                <ul>
                                    <li style="margin-top:-60px; width:400px;"><a href="AddIllustration1.aspx">placebo | 2015</a></li>
                                    <li style="width:400px;"><a href="AddIllustration2.aspx">φ | 2015</a></li>
                                    <li style="width:400px;"><a href="AddIllustration3.aspx">Amour | 2015</a></li>
                                    <li style="width:400px;"><a href="AddIllustration4.aspx">Coup de grass | 2014</a></li>
                                    <li style="width:400px;"><a href="AddIllustration5.aspx">Catharsis | 2014</a></li>
                                    <li style="width:400px;"><a href="AddIllustration6.aspx">In a heaven where no crow No scarecrow either | 2013</a></li>
                                    <li style="width:400px;"><a href="AddIllustration7.aspx">Primitive Fututre | 2013</a></li>
                                    <li style="width:400px;"><a href="AddIllustration8.aspx">Untitled Series | 2012</a></li>
                                    <li style="width:400px;"><a href="AddIllustration9.aspx">Sharh-al-Taaligheh | 2011</a></li>
                                    <li style="width:400px;"><a href="AddIllustration10.aspx">And I will Acervate with Love | 2010</a></li>
                                    <li style="width:400px;"><a href="AddIllustration11.aspx">Untitled Series | 2009</a></li>
                                    <li style="width:400px;"><a href="AddIllustration18.aspx">Great men are doing these acts | 2008</a></li>
                                    <li style="width:400px;"><a href="AddIllustration12.aspx">Oh Cow! thou will become immortal | 2005-2008</a></li>
                                    <li style="width:400px;"><a href="AddIllustration19.aspx">Shat-e-Ranj | 2007</a></li>
                                    <li style="width:400px;"><a href="AddIllustration13.aspx">The moon guest’s your eyes | 2007</a></li>
                                    <li style="width:400px;"><a href="AddIllustration14.aspx">Untitled Series | 2004</a></li>
                                    <li style="width:400px;"><a href="AddIllustration15.aspx">A man with a white mustache | 2004</a></li>
                                    <li style="width:400px;"><a href="AddIllustration16.aspx">Allegro | 2000-2003</a></li>
                                    <li style="width:400px;"><a href="AddIllustration17.aspx">Untitled Series | before 2000</a></li>
                                </ul>
                            </li>
                            <li><a href="">photos</a></li>
                            <li><a href="">graphics</a></li>
                            <li><a href="">video art</a></li>
                            <li><a href="">performance art</a></li>
                            <li><a href="">installation art</a></li>
                            <li><a href="">sound art</a></li>
                            <li><a href="">etcetra</a></li>
                        </ul>
                    </li>
                    <li id="exhibitsTab"><a href="">EXHIBITS</a></li>
                    <li id="mediaTab"><a href="">MEDIA</a>
                        <ul>
                            <li><a href="">books & catalogs</a></li>
                            <li><a href="">posters</a></li>
                            <li><a href="">articles</a></li>
                            <li><a href="">tv / video</a></li>
                        </ul>
                    </li>
                    <li id="aboutTab"><a href="">ABOUT</a></li>
                  </ul>
                </nav>
            </div>
        </form>
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
        
        </asp:ContentPlaceHolder>
</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

在加载ContentPlaceHolder1之前,您会关闭标记表单。 尝试在以下位置移动标记表单

  <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
  </asp:ContentPlaceHolder>
</form>