或者我可能不需要,我不知道。這是我想要做的。我在一個頁面上有一個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 =」成爲一個有效的鏈接。但是,如果有更好的方法來做到這一點,或者我的想法甚至不存在,那我該如何實現我所需要的?
嘿,我很確定你是對的。我只想問現在我遇到了一個與插入數據庫不同的錯誤。當我按下按鈕時,它給我一個錯誤,說「多步OLE DB操作產生錯誤」有什麼想法?另外,請看我的更新代碼,我添加了一些東西。 – Joseph
沒關係,讓它工作!你是一個救星! – Joseph