使用ADO.Net&更新Gridview ImageField存储过程

时间:2015-10-16 14:02:43

标签: c# asp.net gridview ado.net

我有一个gridview,使用ado.net和存储过程填充数据库中的数据。

ASPX代码:

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="MovieComingSoonID"
            OnRowDataBound="OnRowDataBound" OnRowEditing="OnRowEditing" OnRowCancelingEdit="OnRowCancelingEdit"
            OnRowUpdating="OnRowUpdating" OnRowDeleting="OnRowDeleting" EmptyDataText="No records have been added!" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
            <Columns>
                <asp:TemplateField HeaderText="Title" ItemStyle-Width="150">
                    <ItemTemplate>
                        <asp:Label ID="lblTitle" runat="server" Text='<%# Eval("Title") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>

                        <asp:TextBox ID="txtTitle" runat="server" Text='<%# Eval("Title") %>'></asp:TextBox>

                    </EditItemTemplate>

                    <ItemStyle Width="150px"></ItemStyle>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Short Description" ItemStyle-Width="150">
                    <ItemTemplate>
                    <asp:Label ID="lblShort" runat="server" Text='<%# Eval("ShortDescription") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtShort" runat="server" Text='<%# Eval("ShortDescription") %>'></asp:TextBox>
                </EditItemTemplate>

                <ItemStyle Width="150px"></ItemStyle>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Long Description" ItemStyle-Width="150">
                <ItemTemplate>
                    <asp:Label ID="lblLong" runat="server" Text='<%# Eval("LongDescription") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtLong" runat="server" Text='<%# Eval("LongDescription") %>'></asp:TextBox>
                </EditItemTemplate>

                <ItemStyle Width="150px"></ItemStyle>
            </asp:TemplateField>

            <asp:ImageField DataImageUrlField="ImageUrl" ControlStyle-Height="100" HeaderText="Image URL" ItemStyle-Width="150">
                <itemtemplate>
        <asp:Label ID="lblImage" runat="server" Text='<%# Eval("ImageUrl") %>'></asp:Label>
    </itemtemplate>
                <edititemtemplate>
        <asp:TextBox ID="txtImage" runat="server" Text='<%# Eval("ImageUrl") %>'></asp:TextBox>
    </edititemtemplate>

                <ItemStyle Width="150px"></ItemStyle>
            </asp:ImageField>

            <asp:CommandField ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true" ItemStyle-Width="150">
                <ItemStyle Width="150px"></ItemStyle>
            </asp:CommandField>
        </Columns>
    </asp:GridView>

可以使用以下代码更新gridview中的记录:

protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = GridView1.Rows[e.RowIndex];
            int MovieComingSoonID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
            string Title = (row.FindControl("txtTitle") as TextBox).Text;
            string shortDescription = (row.FindControl("txtShort") as TextBox).Text;
            string longDescription = (row.FindControl("txtLong") as TextBox).Text;
            string imageUrl = (row.FindControl("txtImage") as TextBox).Text;
            string constr = System.Configuration.ConfigurationManager
            .ConnectionStrings["conString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand("MoviesComingSoon_CRUD"))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Action", "UPDATE");
                    cmd.Parameters.AddWithValue("@MovieComingSoonID", MovieComingSoonID);
                    cmd.Parameters.AddWithValue("@Title", Title);
                    cmd.Parameters.AddWithValue("@ShortDescription", shortDescription);
                    cmd.Parameters.AddWithValue("@LongDescription", longDescription);
                    cmd.Parameters.AddWithValue("@ImageUrl", imageUrl);
                    cmd.Connection = con;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
            GridView1.EditIndex = -1;
            this.BindGrid();
        }

我得到的问题是gridview中的最后一列,它是一个imageField,而不是其他三个是TemplateFields。运行更新功能时,我收到错误

  

类型'System.Web.UI.WebControls.ImageField'没有公开   名为'itemtemplate'的属性。

我有点理解为什么我遇到错误却无法找到解决方法。我如何能够以与其他三列相同的方式捕获图像URL并使用它更新数据库?

帮助非常感谢大家!谢谢!

0 个答案:

没有答案