Asp.net GridView OnRowUpdating

时间:2013-12-02 20:28:54

标签: c# asp.net gridview sql-update

我在gridview中使用OnRowUpdating。这是相当直接的,但由于某种原因,它没有更新。好像它没有找到EditTemplate控件。这与设置有点不同,因为所有控件都在一个列中(这可能导致问题?)并且它以“博客”格式设置。我没有收到任何运行时错误,所以它必须是逻辑。这是asp。

ASP

<asp:GridView ID="gvPosts" runat="server" AutoGenerateColumns="False" DataKeyNames="MessageID" GridLines="Vertical" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" Width="693px"
             OnPageIndexChanging="gvPosts_PageIndexChanging" OnRowEditing="gvPosts_RowEditing"
             OnRowCancelingEdit="gvPosts_RowCancelingEdit" OnRowUpdating="gvPosts_RowUpdating" AllowPaging="True">
            <AlternatingRowStyle BackColor="#CCCCCC" />
            <Columns>
                <asp:TemplateField HeaderText="Posts" HeaderStyle-Font-Size="Larger">
                    <EditItemTemplate>
                        <asp:Textbox ID="tbGridTitle" runat="server" Text='<%# Bind("Title") %>' Font-Bold="true" Font-Size="Large" ForeColor="#cc0000"></asp:Textbox>
                        <div style="margin-left: 20px">
                            <p>
                                <asp:Textbox ID="tbGridAuthor" runat="server" Text='<%# Bind("Author") %>' Font-Bold="true" Font-Size="Small" Width="150px"></asp:Textbox>                             
                                <asp:label ID="lblDate" runat="server" Text='<%# Bind("Date","{0:MM/dd/yyyy}") %>' Font-Bold="true" Font-Size="Small" Width="100px"></asp:label>
                                <asp:label ID="lblIPAddress" runat="server" Text='<%# Bind("IPAddress") %>' Font-Bold="true" Font-Size="Small" Width="100px" Visible="false"></asp:label>
                                <%--<asp:Label ID="lblMID" runat="server" Text='<%# "ImageHandler.ashx?mid="+ Eval("MessageID") %>'></asp:Label>--%>
                            </p>
                        </div>
                        <div style="margin-left: 40px">
                            <asp:TextBox ID="tbGridMessage" runat="server" Text='<%# Bind("Message") %>' TextMode="MultiLine" Width="450px"></asp:TextBox>
                            <div>
                                <asp:Image ID="postImage" runat="server" ImageUrl='<%# "ImageHandler.ashx?mid="+ Eval("MessageID") %>' Width="400px" AlternateText="No Image" ImageAlign="Middle" />
                            </div>
                        </div>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Text='<%# Bind("Title") %>' Font-Bold="true" Font-Size="Large" ForeColor="#cc0000"></asp:Label>
                        <div style="margin-left: 20px">
                            <p>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("Author") %>' Font-Bold="true" Font-Size="Small" Width="150px"></asp:Label>                             
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Date","{0:MM/dd/yyyy}") %>' Font-Bold="true" Font-Size="Small" Width="100px"></asp:Label>
                                <asp:Label ID="lblIPAddress" runat="server" Text='<%# Bind("IPAddress") %>' Font-Bold="true" Font-Size="Small" Width="100px" Visible="false"></asp:Label>
                                <%--<asp:Label ID="lblMID" runat="server" Text='<%# "ImageHandler.ashx?mid="+ Eval("MessageID") %>'></asp:Label>--%>
                            </p>
                        </div>
                        <div style="margin-left: 40px">
                            <asp:Label ID="Label3" runat="server" Text='<%# Bind("Message") %>'></asp:Label>
                            <div>
                                <asp:Image ID="postImage" runat="server" ImageUrl='<%# "ImageHandler.ashx?mid="+ Eval("MessageID") %>' Width="400px" AlternateText="No Image" ImageAlign="Middle" Visible="false" />
                            </div>
                        </div>
                    </ItemTemplate>
                    <HeaderStyle Font-Size="Large" />
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton runat="server" Text="View Comments" OnClick="lbViewComments_Click"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="MessageID" SortExpression="MessageID" Visible="true">
                    <ItemTemplate>
                        <asp:Label ID="lblMessageID" runat="server" Text='<%# Bind("MessageID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField ShowHeader="False">
                    <EditItemTemplate>
                        <asp:LinkButton ID="lbUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
                        &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField ShowHeader="False" Visible="False">
                    <ItemTemplate>
                        <asp:LinkButton ID="lbDelete" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" OnClick="lbDelete_Click"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

这是RowUpdating背后的代码..

C#

    protected void gvPosts_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow gvr = (GridViewRow)gvPosts.Rows[e.RowIndex];

        int MessageID = Convert.ToInt32(gvPosts.DataKeys[e.RowIndex].Value.ToString());
        TextBox Title = (TextBox)gvr.FindControl("tbGridTitle");
        TextBox Message = (TextBox)gvr.FindControl("tbGridMessage");
        TextBox Author = (TextBox)gvr.FindControl("tbGridAuthor");

        try
        {
            conn.Open();
            SqlCommand cmdUpdate = new SqlCommand("UPDATE BlogMessages SET Title = @Title, Message = @Message, Author = @Author WHERE MessageID = @MessageID", conn);
            cmdUpdate.Parameters.AddWithValue("@MessageID", MessageID);
            cmdUpdate.Parameters.AddWithValue("@Title", Title.Text);
            cmdUpdate.Parameters.AddWithValue("@Message", Message.Text);
            cmdUpdate.Parameters.AddWithValue("@Author", Author.Text);
            cmdUpdate.ExecuteNonQuery();
            conn.Close();

            gvPosts.EditIndex = -1;

            DataBound();
            GridControls();
        }
        catch (Exception ex)
        {
            lblMessage.Text = "Couldn't update! " + ex.ToString();
        }
    }

其他所有内容,例如RowEditingRowEditCanceling,但出于某种原因,我无法更新。我觉得一些新鲜的眼睛可能能够捕捉到我所缺少的东西。

我也尝试过这个来寻找控制但没有运气。

TextBox Title = (TextBox)gvPosts.Rows[e.RowIndex].FindControl("tbGridTitle");

我尝试了很多东西,但我不记得了。提前谢谢!

2 个答案:

答案 0 :(得分:2)

您的事件触发的顺序可能是问题。请确保您具有与下面相同的设置并根据您的需要进行修改。我尝试使用您的标记并且它可以正常工作。 我创建了一个Post类

 public class Post 
{
    public String Author { get; set; }
    public String Title { get; set; }
    public DateTime Date { get; set; }
    public String IPAddress { get; set; }
    public String Message {get;set;}
    public String MessageID { get; set; }

}

然后使用此代码进行测试

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData();
        }
    }

    public void BindData()
    {
        ArrayList myList = new ArrayList();

        myList.Add(new Post() { Author = "Abide", Title = "The Post", Date = DateTime.Now, IPAddress = "192.168.0.9", Message = "It works", MessageID = "5" });

        gvPosts.DataSource = myList;
        gvPosts.DataBind();
    }
    protected void gvPosts_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow gvr = (GridViewRow)gvPosts.Rows[e.RowIndex];

        int MessageID = Convert.ToInt32(gvPosts.DataKeys[e.RowIndex].Value.ToString());
        TextBox Title = (TextBox)gvr.FindControl("tbGridTitle");
        TextBox Message = (TextBox)gvr.FindControl("tbGridMessage");
        TextBox Author = (TextBox)gvr.FindControl("tbGridAuthor");

     // do your stuff here

    }

    protected void gvPosts_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvPosts.EditIndex = e.NewEditIndex;
        BindData();

    }

    protected void gvPosts_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }

    protected void gvPosts_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvPosts.EditIndex = -1;
        BindData();

    }


}

答案 1 :(得分:0)

原来我忽略了我的Page_Load。我有..

if(Page.IsPostBack)
{
    //code here
}

而不是..

if(!Page.IsPostBack)
{
    //code here
}
新的一天,新鲜的眼睛终于抓住了我的错误。希望这能节省一些时间。