Asp.net用户控件擅长更新 [英] Asp.net usercontrol excel like updating

查看:88
本文介绍了Asp.net用户控件擅长更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,程序员,我需要在这里解决一些问题.
我正在创建一个像更新这样的Excel,其中,如果用户单击特定的列/行,它将更改为文本框,而当他单击另一个时,它将返回标签,但它已被更新.顺便说一句,我使用的usercontrol将基于gridview的行进行双向绑定
我的代码如下:
客户端

Hello programmers, got a few twitching i need to resolve here.
I am creating an excel like updating wherein if the user click on a specific column/row it will change to textbox and when he click it another it will return to label but it is already been updated. By the way I am using usercontrol that will bi bind based on the row of a gridview
My codes are as follows:
clientside

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="CourseUserControl.ascx.vb"

    Inherits="CourseUserControl" %>
        <asp:GridView ID="grdCourse" runat="server" Width="800px" HorizontalAlign="Left"

            AutoGenerateColumns="False" align="center" DataKeyNames="TrainingPlanID" Font-Names="verdana,arial,helvetica,sans-serif"

            Font-Size="8pt" BackColor="White" GridLines="Vertical" OnRowCommand="grdCourse_RowCommand">
            <Columns>
                <asp:ButtonField Text="SingleClick" CommandName="SingleClick" Visible="False" />
                <asp:TemplateField HeaderText="TrainingPlanID" SortExpression="TrainingPlanID" HeaderStyle-HorizontalAlign="Left">
                    <ItemTemplate>
                        <asp:Label ID="lblTrainingPlanID" runat="server" Text='<%# Eval("TrainingPlanID")%>'

                            Width="30px" Height="10px"> </asp:Label>                            
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="CourseName" SortExpression="CourseName" HeaderStyle-HorizontalAlign="Left">
                    <ItemTemplate>
                        <asp:Label ID="lblCoursename" runat="server" Text='<%# Bind("CourseName")%>' Width="300px"

                            Height="10px"> </asp:Label>
                        <asp:TextBox ID="txtCourseName" runat="server" Text='<%# Bind("CourseName") %>' Font-Size="XX-Small"

                            Font-Names="Verdana" Width="300px" Visible="false"></asp:TextBox>                            
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Course Code" HeaderStyle-HorizontalAlign="Left">
                    <ItemTemplate>
                        <asp:Label ID="lblCourseCode" runat="server" Text='<%# Eval("CourseCode")%>' Height="10px"> </asp:Label>
                        <asp:TextBox ID="txtCourseCode" runat="server" Text='<%# Eval("CourseCode") %>' Font-Size="XX-Small"

                            Font-Names="Verdana" Width="300px" Visible="false"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Duration" HeaderStyle-HorizontalAlign="Left">
                    <ItemTemplate>
                        <asp:Label ID="lblDuration" runat="server" Text='<%# Eval("Duration")%>' Height="10px"> </asp:Label>
                        <asp:TextBox ID="txtDuration" runat="server" Text='<%# Eval("Duration")%>' Font-Size="XX-Small"

                            Font-Names="Verdana" Visible="false"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="EditDetail" HeaderStyle-HorizontalAlign="Left">
                    <ItemTemplate>
                        <asp:ImageButton ID="EditDetail" Height="15px" ImageUrl="Images/edit2.png" runat="server"

                            CommandName="cmdlink" CommandArgument='<%# Eval("TrainingPlanID")%>' PostBackUrl='<%#"TrainingPlanDetails.aspx?TPID=" & Eval("TrainingPlanID")%>' />
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Delete" HeaderStyle-HorizontalAlign="Left">
                    <ItemTemplate>
                        <asp:ImageButton ID="ImageDeleteCourse" Height="15px" ImageUrl="Images/Delete.png"

                            runat="server" CommandName="cmdDelete" CommandArgument='<%# Eval("TrainingPlanID")%>'

                            OnClick="ImageDeleteCourse_Click" OnClientClick="return confirm('Are you sure to delete this Course?')" /><!-- onclick="ImageDeleteCourse_Click" -->
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" />
                </asp:TemplateField>
                <asp:CommandField ShowDeleteButton="True" />
                <asp:TemplateField HeaderText="DisciplineID" HeaderStyle-HorizontalAlign="Left">
                    <ItemTemplate>
                        <asp:Label ID="DisciplineCourseID" runat="server" Text='<%# Eval("DisciplineID")%>'

                            Height="10px"> </asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="EditCourse" HeaderStyle-HorizontalAlign="Left">
                    <ItemTemplate>
                        <asp:ImageButton ID="ImageEdit" Height="15px" ImageUrl="Images/edit.png" runat="server"

                            CommandName="cmdlink" CommandArgument='<%# Eval("TrainingPlanID")%>' />
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Center" />
                </asp:TemplateField>
                <asp:CommandField ShowEditButton="true" />
            </Columns>
            <RowStyle BackColor="White" ForeColor="#333333" />
            <FooterStyle BackColor="White" ForeColor="#333333" />
            <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
        </asp:GridView>


现在是我的usercontrol gridview上的客户端,这是我的代码


this is the client side now on my usercontrol gridview updating here is my code

Protected Sub grdCourse_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles grdCourse.RowUpdating
        Dim _gridView As GridView = DirectCast(sender, GridView)
        Dim key As String = ""
        Dim value As String = ""
        ' The keys for the NewValues collection
        Dim _columnKeys As String() = New String() {"CourseName", "CourseCode", "Duration"}

        If e.RowIndex > -1 Then

            'Course Name column = 2
            '_gridView.Rows(e.RowIndex).Cells(2)
            'Course Code column = 3
            ' Loop though the columns
            For i As Integer = _firstEditCellIndex To 4
                ' Get the controls in the cell
                Dim _displayControl As Control = _gridView.Rows(e.RowIndex).Cells(i).Controls(1)
                Dim _editControl As Control = _gridView.Rows(e.RowIndex).Cells(i).Controls(3)
                'Object
                ' Get the column key
                key = _columnKeys(i - _firstEditCellIndex)

                ' If a cell in edit mode get the value of the edit control
                If _editControl.Visible Then
                    If TypeOf _editControl Is TextBox Then
                        value = DirectCast(_editControl, TextBox).Text
                        'error since jan12 2012(the value cannot get DATA<value null>)-------------------------------------------------------
                        'try to debug
                        End If
                    ' Add the key/value pair to the NewValues collection
                    e.NewValues.Add(key, value)
                Else
                    ' else get the value of the display control
                    value = DirectCast(_displayControl, Label).Text.ToString()
                    ' Add the key/value pair to the NewValues collection
                    e.NewValues.Add(key, value)
                End If
            Next
            ' Clear the selected index to prevent 
            ' another update on the next postback
            _gridView.SelectedIndex = -1
        End If
    End Sub


as you see that is my error the value doesn''t give what i typed in. It is null

any ideas?

更新:
我的错. My goal si this;
Switching from label to textbox
when I clicked on label it will change to textbox with the text of the label on the textbox
now when I change the content of the textbox that is the problem cause even if I have a text on my textbox


as you see that is my error the value doesn''t give what i typed in. It is null

any ideas?

Update:
My bad. My goal si this;
Switching from label to textbox
when I clicked on label it will change to textbox with the text of the label on the textbox
now when I change the content of the textbox that is the problem cause even if I have a text on my textbox

If TypeOf _editControl Is TextBox Then<br />
                        value = DirectCast(_editControl, TextBox).Text


the value is Null or "" which mean i cant get the text
By the way here is my code on rowcommand and rowdatabound so that you could understand what I am doing


the value is Null or "" which mean i cant get the text
By the way here is my code on rowcommand and rowdatabound so that you could understand what I am doing

Protected Sub grdCourse_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdCourse.RowCommand
        Dim _gridView As GridView = DirectCast(sender, GridView)
        Select Case e.CommandName
            Case ("SingleClick")
                ' Get the row index
                Dim _rowIndex As Integer = Integer.Parse(e.CommandArgument.ToString())
                ' Parse the event argument (added in RowDataBound) to get the selected column index
                Dim _columnIndex As Integer = Integer.Parse(Request.Form("__EVENTARGUMENT"))
                ' Set the Gridview selected index
                _gridView.SelectedIndex = _rowIndex

                ' Bind the Gridview
                CourseBindGrid()

                ' Get the display control for the selected cell and make it invisible
                Dim _displayControl As Control = _gridView.Rows(_rowIndex).Cells(_columnIndex).Controls(1)
                _displayControl.Visible = False
                ' Get the edit control for the selected cell and make it visible
                Dim _editControl As Control = _gridView.Rows(_rowIndex).Cells(_columnIndex).Controls(3)
                _editControl.Visible = True
                ' Clear the attributes from the selected cell to remove the click event
                _gridView.Rows(_rowIndex).Cells(_columnIndex).Attributes.Clear()

                ' Set focus on the selected edit control
                ScriptManager.RegisterStartupScript(Me, [GetType](), "SetFocus", "document.getElementById('" + _editControl.ClientID + "').focus();", True)
                ' If the edit control is a dropdownlist set the
                ' SelectedValue to the value of the display control
                'If TypeOf _editControl Is DropDownList AndAlso TypeOf _displayControl Is Label Then
                '    DirectCast(_editControl, DropDownList).SelectedValue = DirectCast(_displayControl, Label).Text
                'End If
                ' If the edit control is a textbox then select the text
                If TypeOf _editControl Is TextBox Then
                    DirectCast(_editControl, TextBox).Attributes.Add("onfocus", "this.select()")
                End If

                Exit Select
        End Select


    End Sub





Protected Sub grdCourse_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdCourse.RowDataBound

        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim disID As Label = DirectCast(e.Row.FindControl("DisciplineCourseID"), Label)
            Dim myImage As ImageButton = DirectCast(e.Row.FindControl("ImageDeleteCourse"), ImageButton)
            If disID.Text = "" Then
                myImage.Visible = False
            Else
                myImage.Visible = True
                ' Get the LinkButton control in the first cell
                Dim _singleClickButton As LinkButton = DirectCast(e.Row.Cells(0).Controls(0), LinkButton)
                ' Get the javascript which is assigned to this LinkButton
                Dim _jsSingle As String = Page.ClientScript.GetPostBackClientHyperlink(_singleClickButton, "")

                ' If the page contains validator controls then call
                ' Page_ClientValidate before allowing a cell to be edited
                If Page.Validators.Count > 0 Then
                    _jsSingle = _jsSingle.Insert(11, "if(Page_ClientValidate())")
                End If

                ' Add events to each editable cell
                For columnIndex As Integer = _firstEditCellIndex To 5 - 1
                    ' Add the column index as the event argument parameter
                    Dim js As String = _jsSingle.Insert(_jsSingle.Length - 2, columnIndex.ToString())
                    ' Add this javascript to the onclick Attribute of the cell
                    e.Row.Cells(columnIndex).Attributes("onclick") = js
                    ' Add a cursor style to the cells
                    e.Row.Cells(columnIndex).Attributes("style") += "cursor:pointer;cursor:hand;"
                Next
            End If
        End If
    End Sub

推荐答案

You can''t compare a desktop application to a web application. Unless you use a 3rd party control you will not get Excel-like functionality from the built-in controls. You can, however, use the controls as they were designed. Put the row you want to edit into edit mode, then update the values when finised updating.

There are many examples available on DataGridView editing.
You can''t compare a desktop application to a web application. Unless you use a 3rd party control you will not get Excel-like functionality from the built-in controls. You can, however, use the controls as they were designed. Put the row you want to edit into edit mode, then update the values when finised updating.

There are many examples available on DataGridView editing.


这篇关于Asp.net用户控件擅长更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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