2014-01-17 18 views
0

或者我可能不需要,我不知道。这是我想要做的。我在一个页面上有一个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

你可以得到blogID如下

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

我会用int.TryParse避免在查询字符串

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

嘿,我很确定你是对的。我只想问现在我遇到了一个与插入数据库不同的错误。当我按下按钮时,它给我一个错误,说“多步OLE DB操作产生错误”有什么想法?另外,请看我的更新代码,我添加了一些东西。 – Joseph

+1

没关系,让它工作!你是一个救星! – Joseph

1

一个视图状态支持的属性添加到EditThisPost.aspx持有BlogID: -

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

坐落在EditThisPost.aspx的pageLoad的这种属性,然后用它UploadFile。

+0

我投你的答案,因为它在非整数值的情况下例外是我从来没有听说过的事情,并将进行调查,但其他人的答案像一个魅力和非常简单的工作! – Joseph

+0

Viewstate支持的属性是一种相当常见的技术。感谢upvote和祝你好运;) – sh1rts

相关问题