如何从我的站点内的其他页面访问控件?

时间:2014-01-17 01:10:44

标签: c# asp.net visual-studio-2010 gridview

或许我不需要,我不知道。这就是我想要做的。我在一个页面上有一个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 =”成为有效链接。但如果有更好的方法,或者我想的方式甚至不存在,那么我怎样才能完成我的需要呢?

2 个答案:

答案 0 :(得分:2)

你可以获得如下的blogID

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

如果查询字符串中存在非整数值,我会使用int.TryParse来避免异常

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

答案 1 :(得分:1)

将一个Viewstate支持的属性添加到EditThisPost.aspx以保存BlogID: -

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

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