1

我有一个 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. 行命令事件永远不会被触发。该页面刚刚进入 Page_Load 中的 ELSE(不是回发)
  2. 如何跟踪在每一行的“评论”列(TEXTAREA)中输入的所有内容,并在单击“保存更改”(表单)按钮时将数据保存到数据库中?

谢谢!

更新:网格视图的数据绑定如下:

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

在此处输入图像描述

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

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

再次感谢!

4

1 回答 1

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];
    }
}

然后点击 3 个按钮

于 2012-06-02T04:52:59.043 回答