在ASP.Net中编辑单个GridView单元 [英] Edit Individual GridView Cells in ASP.Net

查看:72
本文介绍了在ASP.Net中编辑单个GridView单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello,

I am attempting to determine which grid cell (i.e. row and column) is being edited but having some difficulty. The grid cells are comprised of textbox control in a template field. I'd tried following the code used in the Code Project article "Edit Individual GridView Cells in ASP.Net". Here's a sample of the code:


HTML:
<pre lang="HTML">
<asp:GridView ID="grdBusinessCodes" runat="server" style="z-index:101; font-family:'Arial'; font-size:9pt;"

CssClass="GridAppearanceStyle" HeaderStyle-CssClass="GridHeaderStyle" AutoGenerateColumns="False" RowStyle-CssClass="GridItemStyle">

    <RowStyle CssClass="GridItemStyle" />
    <Columns>                
        <asp:TemplateField HeaderText="Column0" Visible="False">
            <ItemTemplate>
                 <asp:TextBox ID="TextBox0" runat="server" Text='<%# Bind("Column0") %>' style="font-family:'Arial'; font-size:9pt;" 

                    TextMode="MultiLine" Enabled="True" ReadOnly="False" AutoPostBack="true" OnTextChanged="TextBox0_TextChanged"></asp:TextBox>                                 
            </ItemTemplate>
        </asp:TemplateField>
        
        <asp:TemplateField HeaderText="Column1" Visible="False">
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Column1", "{0:c}") %>' 

                    style="font-family:'Arial'; font-size:9pt;" TextMode="MultiLine" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:CommandField HeaderText="row_selector" ShowDeleteButton="False" 

            ShowEditButton="True" ShowInsertButton="False" ShowSelectButton="True" ButtonType="Link" Visible="False" />
    </Columns>
    <HeaderStyle CssClass="GridHeaderStyle" />

</asp:GridView>





背后代码:





Behind-code:

Private Sub grdBusinessCodes_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdBusinessCodes.RowCommand

        m_RowIndex = Integer.Parse(e.CommandArgument.ToString())
        miColumnClicked = Integer.Parse(Request.Form("__EVENTARGUMENT").ToString())        

End Sub



Private Sub grdBusinessCodes_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdBusinessCodes.RowDataBound
        Dim itemType As DataControlRowType = e.Row.RowType
        Dim button As LinkButton
        Dim strPostBack As String
        Dim sJavaScriptArg As String


        If e.Row.RowType = DataControlRowType.DataRow Then
            'get the link button control in the first cell
            button = CType(e.Row.Cells(e.Row.Cells.Count - 1).Controls(0), LinkButton)    'The linkbutton is in the last cell **jf 8/1/14                                    
            strPostBack = Me.Page.ClientScript.GetPostBackClientHyperlink(button, "")

            For i = 0 To e.Row.Cells.Count - 1
                'add the column index as the event argument parameter
                sJavaScriptArg = strPostBack.Insert(strPostBack.Length - 2, i.ToString)
                

                If e.Row.Cells(i).HasControls And e.Row.Cells(i).Controls.Count > 1 Then 'TypeOf e.Row.Cells(i).Controls(1) Is TextBox Then
                    If i <> (e.Row.Cells.Count - 1) Then
                        CType(e.Row.Cells(i).Controls(1), TextBox).Attributes("onclick") = sJavaScriptArg
                        CType(e.Row.Cells(i).Controls(1), TextBox).Attributes.Add("onclick", "this.style.backgroundColor='Highlight';")
                        CType(e.Row.Cells(i).Controls(1), TextBox).Attributes.Add("onblur", "this.style.backgroundColor='White';")
                    End If
		End If
	    Next
	End If

End Sub



单元格可以单独编辑,但无法确定单元格所属的行和列。



感谢任何帮助。


The cells can be edited individually but unable determine which row and column the cell belongs to.

Any help is appreciated.

推荐答案

我更正了设置单元格的点击事件和样式RowDataBound事件似乎可以解决问题:



I corrected the setting the click event and style for the cells in the RowDataBound event and that seemed to do the trick:

sJavaScriptArg = strPostBack.Insert(strPostBack.Length - 2, i.ToString)
e.Row.Cells(i).Attributes("onclick") = sJavaScriptArg
' Add a cursor style to the cells
e.Row.Cells(i).Attributes("style") += "cursor:pointer;cursor:hand;"


这篇关于在ASP.Net中编辑单个GridView单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆