GridView的RowCommand事件和保存文本(GridView RowCommand Eve

2019-09-17 16:50发布

我有一个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. 该行命令事件从来没有发射。 页面只是进入ELSE(不是为回传)在Page_Load中
  2. 我怎么可以跟踪什么都在每行(一个TEXTAREA)的“注释”一栏输入,并且将数据保存到数据库中点击保存更改(形式)按钮时?

谢谢!

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

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的。 我没有包括简单起见,图片等栏目。

  • 用户可以添加注释,当他点击了“保存更改”按钮,相应的意见应得到保存为图片。
  • “选择IMAGE”打开了ModalDialogBox其使用户能够选择该图像。 当关闭时,它会导致回传,以及重新绑定gridview的显示用户刚刚选择的图像。 因此,我需要在GridView重新绑定上回发,或者解决的办法。
  • 删除的ImageButton(在GridView控件)应删除数据库中的图像和评论。

再次感谢!

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



文章来源: GridView RowCommand Event and Saving Text