【问题标题】:How do I access a control from a different page within my site?如何从我的站点中的不同页面访问控件?
【发布时间】:2014-01-17 01:10:44
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    你可以得到如下的 blogID

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

    我会使用int.TryParse 来避免查询字符串中的非整数值出现异常

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

    【讨论】:

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

    向 EditThisPost.aspx 添加一个 Viewstate 支持的属性来保存 BlogID:-

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

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

    【讨论】:

    • 我投票赞成你的答案,因为这是我从未听说过并且会调查的事情,但另一个人的回答就像一个魅力而且非常简单!
    • Viewstate 支持的属性是一种相当常见的技术。感谢您的投票和祝您好运;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-09
    • 1970-01-01
    • 2015-09-15
    相关资源
    最近更新 更多