簡體   English   中英

如何從站點內的其他頁面訪問控件?

[英]How do I access a control from a different page within my site?

或者也許我不需要,我不知道。 這就是我想要做的。 我在一個頁面上有一個gridview,當我單擊一個項目時,它獲取ID並將其鏈接到另一頁,該頁面顯示了與該項目一起在gridview中的所有信息。 在第二頁上,我希望能夠將帶有一些文本的照片插入數據庫,但是我要確保插入的照片帶有正確的blogID(在第一頁上單擊)。 到目前為止,這是我的代碼:

EditBlogPosts.aspx

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
    AutoGenerateEditButton="True" 
    DataSourceID="AccessDataSource1"
    AutoGenerateColumns="False" DataKeyNames="ID"
    AlternatingRowStyle-BackColor="Gray"  
    AlternatingRowStyle-CssClass="editGridFormat" RowStyle-CssClass="editGridFormat"        
    RowStyle-VerticalAlign="Top" 
    onselectedindexchanged="GridView1_SelectedIndexChanged">

后面的代碼:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow row = GridView1.SelectedRow;
    Response.Redirect("~/EditThisPost.aspx?ID=" + row.Cells[2].Text);
}

EditThisPost.aspx

<asp:FormView ID="Formview1" runat="server" DefaultMode="Insert" DataSourceID="AccessDataSource1" >
    <InsertItemTemplate>
        <br />
        <asp:Label ID="TextforPhotoLabel" runat="server" Text="Put your text to go with your photo here:" /><br />
        <asp:TextBox ID="PhotoText" runat="server" Rows="10" Columns="100" /><br /><br />
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Label ID="UploadStatusLabel" runat="server" Text="Status: " /><br />
        <asp:Button ID="UploadButton" runat="server" OnClick="UploadFile" Text="Insert Item" /><br />
    </InsertItemTemplate>
</asp:FormView>

后面的代碼(特別注意我聲明為int blogID的行):

protected void UploadFile(object sender, EventArgs e)
{
    TextBox txtPhotoText = (TextBox)Formview1.FindControl("PhotoText");
    FileUpload FileUpload1 = (FileUpload)Formview1.FindControl("FileUpload1");
    Label UploadStatusLabel = (Label)Formview1.FindControl("UploadStatusLabel");
    if (FileUpload1.HasFile)
    {
        try
        {
            if (FileUpload1.PostedFile.ContentType == "image/jpeg")
            {
                if (FileUpload1.PostedFile.ContentLength < 10240000)
                {
                    string filename = Path.GetFileName(FileUpload1.FileName);
                    FileUpload1.SaveAs(Server.MapPath("~/photos/PeoplePhotos/") + filename);
                    UploadStatusLabel.Text = "Upload status: Complete!";
                    string constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\TravelJoansDB.mdb;";
                    string cmdstr = "INSERT INTO BlogEntryItems (BlogID, Picture, PicText1) VALUES (?,?,?)";
                    int blogID = int.Parse(Request.QueryString["ID"]);

                    OleDbConnection con = new OleDbConnection(constr);
                    OleDbCommand com = new OleDbCommand(cmdstr, con);

                    con.Open();
                    com.Parameters.AddWithValue("@BlogID", blogID);
                    com.Parameters.AddWithValue("@Picture", filename);
                    com.Parameters.AddWithValue("@PicText1", txtPhotoText);
                    com.ExecuteNonQuery();
                    con.Close();
                }
                else
                    UploadStatusLabel.Text = "Upload status: The file has to be less than 10 MB!";
            }
            else
                UploadStatusLabel.Text = "Upload status: Only JPEG files are accepted!";
        }
        catch (Exception ex)
        {
            UploadStatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
        }
    }
}

我非常感謝您的幫助。 我以為我可以某種方式獲取傳遞的ID,以使“〜\\ EditThisPost.aspx?ID =“成為有效的鏈接。 但是,如果有更好的方法來做,或者甚至我所想的方法都不存在,那么我該如何完成我所需要的?

你可以得到如下的blogID

int blogID  = int.Parse(Request.QueryString["ID"]);

我將使用int.TryParse來避免查詢字符串中非整數值的情況下的異常

int blogID;
int.TryParse(Request.Querystring["ID"], out blogID); 

將一個Viewstate支持的屬性添加到EditThisPost.aspx以保存BlogID:-

http://www.codingwith.net/2008/01/viewstate-backed-properties-part-one.html

在EditThisPost.aspx的PageLoad中設置此屬性,然后在UploadFile中使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM