GridView RowCommand事件和保存文本

时间:2012-06-01 18:42:52

标签: asp.net gridview postback

我有一个GridView设置:

<asp:GridView id="GridView1" Runat="server" AutoGenerateColumns="False" OnRowCommand = "GridView1_RowCommand" EnableViewState="true">
   <Columns>
       <asp:TemplateField HeaderText="Delete" ItemStyle-HorizontalAlign="Center"> 
            <ItemTemplate><asp:Button runat="server" ID="Delete" ImageUrl="~/images/Close.gif" CommandName="DeleteRow" CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"/></ItemTemplate></asp:TemplateField>

       <asp:TemplateField HeaderText="Comment" ItemStyle-Width="175px">
            <ItemTemplate><textarea class="raTextBox" id="txtItemComment" rows="4" cols="30"></textarea></ItemTemplate></asp:TemplateField> 
   </Columns>   
</asp:GridView>

代码隐藏中的RowCommand设置如下:

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
   If (e.CommandName = "DeleteRow") Then
     //do stuff here

GridView是页面加载的数据绑定,如下所示:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  If Not IsPostBack Then
     Session("CalledModule") = "RCMT0021"
  Else
      With ViewState
           _intRepor = CInt(.Item("Report"))
      End With
  End If
     DataBind()   //Gridview Load
End Sub

我的问题:

  1. 永远不会触发row命令事件。该页面刚进入Page_Load
  2. 中的ELSE(Not is Postback)
  3. 如何跟踪每行(“TEXTAREA”)的“注释”列中输入的内容,并在单击“保存更改(表单)”按钮时将数据保存到数据库?
  4. 谢谢!

    更新: Grid-View的数据绑定如下:

    Public Sub DataBind()
    Dim clsDatabase As New clsDatabase
    Dim cmd As New OleDbCommand()
    Try
     cmd.CommandText = "SELECT A, B FROM WHERE C = ? ORDER BY A"
       Dim report As New OleDbParameter("@Report", _intReportNumber)
       cmd.Parameters.Add(report)
    
       cmd.Connection = clsDatabase.Open_DB()
       Dim dReader As OleDbDataReader
       dReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    
       Dim dt As New DataTable
       dt.Columns.Add(New DataColumn("PictureURL", GetType(String)))
       dt.Columns.Add(New DataColumn("Seq", GetType(Int16)))
       dt.Columns.Add(New DataColumn("ReportNumber", GetType(String)))
    
       Do While (dReader.Read())
          Dim dr As DataRow = dt.NewRow()
    
          _strComments = dReader(0).ToString
          dr("Seq") = dReader.GetInt16(1)
          dr("PictureURL") = "GetImages.aspx?report=" + _intReportNumber.ToString + "&seq=" + dReader.GetInt16(1).ToString
          dr("ReportNumber") = _intReportNumber.ToString
          dt.Rows.Add(dr)
       Loop
    
       GridView1.DataSource = dt
       GridView1.DataBind()
    
      Catch err As Exception
     End Try
    End Sub
    

    enter image description here

    基本上,GridView有三个可见列 - 注释字段,图片字段和带有删除按钮(图像)的字段,用于从数据库中删除图片(和注释,如果有的话)。第4列是隐藏的,跟踪图像的图像ID。为简单起见,我没有包括图片和其他列。

    • 用户可以添加评论,当他点击“保存更改”按钮时,应该为图片保存相应的评论。
    • 'SELECT IMAGE'打开一个ModalDialogBox,使用户可以选择图像。关闭时,它会导致回发,并重新绑定gridview以显示用户刚刚选择的图像。因此,我需要GridView在回发时重新绑定,或者解决这个问题。
    • 删除图片按钮(在gridview中)应删除数据库中的图片和评论。

    再次感谢!

1 个答案:

答案 0 :(得分:1)

试试这个结构...

aspx页面:(注意textarearunat="server"

<body>
    <form runat="server">
    <asp:ScriptManager runat="server" />
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand"
                EnableViewState="true" OnRowDataBound="GridView1_RowDataBound">
                <Columns>
                    <asp:TemplateField HeaderText="Delete" ItemStyle-HorizontalAlign="Center">
                        <ItemTemplate>
                            <asp:Button runat="server" ID="Delete" Text="Delete" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Comment" ItemStyle-Width="175px">
                        <ItemTemplate>
                            <textarea runat="server" class="raTextBox" id="txtItemComment" rows="4" cols="30"></textarea></ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>
    </form>
</body>

aspx.cs:

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

protected void BindGrid()
{
    var items = new List<string>();
    for (int i = 0; i < 10; i++)
    {
        items.Add(i + ";comment" + i.ToString());
    }
    GridView1.DataSource = items;
    GridView1.DataBind();
}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "DeleteRow")
    {
        var idx = Convert.ToInt32(e.CommandArgument);
        var cmt = GridView1.Rows[idx].FindControl("txtItemComment") as System.Web.UI.HtmlControls.HtmlTextArea;
        cmt.Value = DateTime.Now.ToString();
    }
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var item = e.Row.DataItem.ToString().Split(";".ToCharArray());

        var del = e.Row.FindControl("Delete") as Button;
        del.CommandName = "DeleteRow";
        del.CommandArgument = item[0];

        var cmt = e.Row.FindControl("txtItemComment") as System.Web.UI.HtmlControls.HtmlTextArea;
        cmt.Value = item[1];
    }
}

then clicked on 3 buttons