LinkBut​​ton没有第二次初始化网格视图中的控件

时间:2013-01-29 07:06:36

标签: asp.net c#-4.0 asp.net-ajax

我有一个gridview,其中TemplateFieldLinkButton中的ItemTemplate相关联,CommandName为“编辑”。当用户点击LinkButton时,它会打开一个ModalPOp Up来编辑网格的当前行记录。这是第一次正常工作并且当modalPopUp关闭并且用户再次点击相同的LinkBut​​ton时,它不起作用object reference not set to an instance of an object例外。调试后我发现第二次它们是当前所选行中的标签lblUserID未初始化。请建议我如何解决这个问题,因为它非常紧急。

请找到以下代码: -

<cc1:GridView ID="gvUser" runat="server" AutoGenerateColumns="False" AllowSorting="True"
        CssClass="grid" OnDataBound="gvUser_DataBound" DataSourceID="odsUserDetails"
        AllowPaging="True" ShowFooter="false" OnRowCancelingEdit="gvUser_RowCancelingEdit"
        OnRowCommand="gvUser_RowCommand" OnRowDeleting="gvUser_RowDeleting" 
        OnSelectedIndexChanged="gvUser_SelectedIndexChanged"
        OnRowDataBound="gvUser_RowDataBound" Height="385px" Width="900px" EnableViewState="false">
    <AlternatingRowStyle CssClass="altrowstyle" />
    <HeaderStyle CssClass="headerstyle" />
    <RowStyle CssClass="rowstyle" Wrap="false" />
    <EmptyDataRowStyle BackColor="#edf5ff" Height="300px" VerticalAlign="Middle" HorizontalAlign="Center" />                                            
    <Columns>
        <asp:TemplateField HeaderText="UserID" SortExpression="userID" HeaderStyle-Font-Bold="true"
                HeaderStyle-Font-Names="Calibre" HeaderStyle-ForeColor="White" Visible="false">
            <ItemTemplate>
                <asp:Label ID="lblUserID" runat="server" Text='<%#Eval("userID") %>' CssClass="GridContent" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Label ID="lblEditUserID" runat="server" Text='<%#Eval("userID") %>' Style="width: 50px;"
                    CssClass="GridContent" />
            </EditItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="E Code" SortExpression="userecode" HeaderStyle-Font-Bold="true"
                HeaderStyle-Font-Names="Calibre" HeaderStyle-ForeColor="White" ItemStyle-Width="100px">
            <ItemTemplate>
                <asp:Label ID="lblUserECode" runat="server" Text='<%#Eval("UserECode") %>' CssClass="GridContent" />
            </ItemTemplate>                                                    
            <FooterTemplate>
                <asp:TextBox ID="txtAddECode" runat="server" Style="width: 50px;" CssClass="GridContent" />
                <asp:RegularExpressionValidator ID="REVAddECode" runat="server" ControlToValidate="txtAddECode"
                    ErrorMessage="*" Style="color: Red;" ValidationExpression="^\d+$" ToolTip="E Code must be numeric value only.">
                </asp:RegularExpressionValidator>
                <asp:RequiredFieldValidator ID="RFVAddECode" runat="server" ErrorMessage="*" ControlToValidate="txtAddECode"
                    Style="color: Red;" ToolTip="E CodeCan't be left empty.">
                </asp:RequiredFieldValidator>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="User Name" SortExpression="username" HeaderStyle-Font-Bold="true"
                HeaderStyle-Font-Names="Calibre" HeaderStyle-ForeColor="White" ItemStyle-Width="150px">
            <ItemTemplate>
                <asp:Label ID="lblUserName" runat="server" Text='<%#Eval("UserName") %>' CssClass="GridContent" />
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtAddUserName" runat="server" Style="width: 150px;" CssClass="GridContent" />
                <asp:RequiredFieldValidator ID="RFVAddUserName" runat="server" ErrorMessage="*" ControlToValidate="txtAddUserName"
                    Style="color: Red;" ToolTip="User Name can't be left empty.">
                </asp:RequiredFieldValidator>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Enable/Disable" SortExpression="IsFlagEnabled" HeaderStyle-Font-Bold="true"
                HeaderStyle-Font-Names="Calibre" HeaderStyle-ForeColor="White">
            <ItemTemplate>
                <asp:Label ID="lblIsFlagEnabled" runat="server" Text='<%# (bool)Eval("IsFlagenabled") == true ? "Disable" : "Enable" %>'
                    CssClass="GridContent" />
            </ItemTemplate>
            <FooterTemplate>
                <asp:DropDownList ID="ddlAddIsFlagEnabled" runat="server" AutoPostBack="true" Style="width: auto;"
                        CssClass="GridContent">
                    <asp:ListItem Text="Enable" Value="0" />
                    <asp:ListItem Text="Disable" Value="1" />
                </asp:DropDownList>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Email Address" SortExpression="EmailAddress" HeaderStyle-Font-Bold="true"
                HeaderStyle-Font-Names="Calibre" HeaderStyle-ForeColor="White" Visible="false">
            <ItemTemplate>
                <asp:Label ID="lblEmailAddress" runat="server" Text='<%#Eval("EmailAddress") %>'
                    CssClass="GridContent" />
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtAddEmailAddress" runat="server" Style="width: 150px;" CssClass="GridContent" />
                <asp:RegularExpressionValidator ID="REVAddEmailAddress" runat="server" ControlToValidate="txtAddEmailAddress"
                    ErrorMessage="*" Style="color: Red;" ValidationExpression="^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$"
                    ToolTip="Not a valid Email Address">
                </asp:RegularExpressionValidator>
                <asp:RequiredFieldValidator ID="RFVAddEmailAddress" runat="server" ErrorMessage="*"
                    ControlToValidate="txtAddEmailAddress" Style="color: Red;" ToolTip="Email Can't be left empty.">
                </asp:RequiredFieldValidator>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Designation" SortExpression="Designation" HeaderStyle-Font-Bold="true"
                HeaderStyle-Font-Names="Calibre" HeaderStyle-ForeColor="White" ItemStyle-Width="100px">
            <ItemTemplate>
                <asp:Label ID="lblDesignation" runat="server" Text='<%#Eval("Designation") %>' CssClass="GridContent" />
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtAddDesignation" runat="server" Style="width: 150px;" CssClass="GridContent" />
                <asp:RequiredFieldValidator ID="RFVAddDesignation" runat="server" ErrorMessage="*"
                    ControlToValidate="txtAddDesignation" Style="color: Red;" ToolTip="Designation Can't be left empty.">
                </asp:RequiredFieldValidator>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Role ID" SortExpression="RoleID" HeaderStyle-Font-Bold="true"
                HeaderStyle-Font-Names="Calibre" HeaderStyle-ForeColor="White">
            <ItemTemplate>
                <asp:Label ID="lblRoleID" runat="server" Text='<%#Eval("RoleID") %>' CssClass="GridContent" />
            </ItemTemplate>
            <FooterTemplate>
                <asp:DropDownList ID="ddlAddRole" runat="server" AutoPostBack="true" Style="width: auto;"
                        CssClass="GridContent">
                    <asp:ListItem Text="Standard" Value="1" />
                    <asp:ListItem Text="TL/TC" Value="2" />
                    <asp:ListItem Text="AM" Value="3" />
                    <asp:ListItem Text="OM" Value="4" />
                    <asp:ListItem Text="Sr OM" Value="5" />
                    <asp:ListItem Text="Admin" Value="6" />
                </asp:DropDownList>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Shift Start Time" SortExpression="ShiftStartTime"
                HeaderStyle-Font-Bold="true" HeaderStyle-Font-Names="Calibre" HeaderStyle-ForeColor="White"
                Visible="true">
            <ItemTemplate>
                <asp:Label ID="lblShiftStartTime" runat="server" Text='<%#Eval("ShiftStartTime") %>'
                    CssClass="GridContent" />
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtAddShiftStartTime" runat="server" Text='<%#Eval("ShiftStartTime") %>'
                    Style="width: 65px;" CssClass="GridContent" />
                <asp:RegularExpressionValidator ID="RegularExpValidatorddlAddShiftStartTime" runat="server"
                    ControlToValidate="txtAddShiftStartTime" ValidationExpression="^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$"
                    ErrorMessage="*" Font-Bold="true" ForeColor="Red" ToolTip="Must be in HH:MM" />
                <asp:RequiredFieldValidator ID="RFVAddShiftStartEdit" runat="server" ControlToValidate="txtAddShiftStartTime"
                    ErrorMessage="*" Font-Bold="true" ForeColor="Red" ToolTip="Shift Start Time can't be left empty.">
                </asp:RequiredFieldValidator>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Shift End Time" SortExpression="ShiftEndTime" HeaderStyle-Font-Bold="true"
                HeaderStyle-Font-Names="Calibre" HeaderStyle-ForeColor="White" Visible="true">
            <ItemTemplate>
                <asp:Label ID="lblShiftEndTime" runat="server" Text='<%#Eval("ShiftEndTime") %>'
                    CssClass="GridContent" />
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtAddShiftEndTime" runat="server" Text='<%#Eval("ShiftEndTime") %>'
                    Style="width: 65px;" CssClass="GridContent" />
                <asp:RegularExpressionValidator ID="RegularExpValidatorddlAddShiftEndTime" runat="server"
                    ControlToValidate="txtAddShiftEndTime" ValidationExpression="^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$"
                    ErrorMessage="*" Font-Bold="true" ForeColor="Red" ToolTip="Must be in HH:MM" />
                <asp:RequiredFieldValidator ID="RFVAddShiftEndTimeEdit" runat="server" ControlToValidate="txtAddShiftEndTime"
                    ErrorMessage="*" Font-Bold="true" ForeColor="Red" ToolTip="Shift End Time can't be left empty.">
                </asp:RequiredFieldValidator>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Saturday Shift Start Time" SortExpression="WeekendShiftStartTime"
                HeaderStyle-Font-Bold="true" HeaderStyle-Font-Names="Calibre" HeaderStyle-ForeColor="White"
                Visible="true">
            <ItemTemplate>
                <asp:Label ID="lblWeekendShiftStartTime" runat="server" Text='<%#Eval("WeekendShiftStartTime") %>'
                    CssClass="GridContent" />
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtAddWeekendShiftStartTime" runat="server" Text='<%#Eval("WeekendShiftStartTime") %>'
                    Style="width: 65px;" CssClass="GridContent" />
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Saturday Shift End Time" SortExpression="weekendshiftendtime"
                HeaderStyle-Font-Bold="true" HeaderStyle-Font-Names="Calibre" HeaderStyle-ForeColor="White"
                Visible="true">
            <ItemTemplate>
                <asp:Label ID="lblWeekendShiftEndTime" runat="server" Text='<%#Eval("weekendshiftendtime") %>'
                    CssClass="GridContent" />
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtAddWeekendShiftEndTime" runat="server" Text='<%#Eval("weekendshiftendtime") %>'
                    Style="width: 65px;" CssClass="GridContent" />
                <asp:RegularExpressionValidator ID="RegularExpValidatorAddddlWeekendShiftEndTime"
                    runat="server" ControlToValidate="txtAddWeekendShiftEndTime" ValidationExpression="^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$"
                    ErrorMessage="*" Font-Bold="true" ForeColor="Red" ToolTip="Must be in HH:MM" />
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Immediate Supervisor" SortExpression="BossName" HeaderStyle-Font-Bold="true"
                HeaderStyle-Font-Names="Calibre" HeaderStyle-ForeColor="White" ControlStyle-Width="120px">
            <ItemTemplate>
                <asp:Label ID="lblSupervisorName" runat="server" CssClass="GridContent" Text='<%#Eval("BossName") %>' />
            </ItemTemplate>
            <FooterTemplate>
                <asp:DropDownList ID="ddlAddImmediateSupervisor" runat="server" AutoPostBack="true"
                    Style="width: auto;" CssClass="GridContent" DataSourceID="" DataTextField="UserName"
                    DataValueField="UserECode" AppendDataBoundItems="True">
                </asp:DropDownList>
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Select" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Names="Calibre"
                HeaderStyle-ForeColor="White">
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
                    Text="Edit" ForeColor="White" OnClick='LinkButton1_Click'>
                </asp:LinkButton>
            </ItemTemplate>
            <FooterTemplate>
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
    <PagerTemplate>
        <table width="100%">
            <tr>
                <td style="text-align: left">
                    Page Size:
                    <asp:DropDownList ID="ddPageSize" runat="server" EnableViewState="true" OnSelectedIndexChanged="ddPageSize_SelectedIndexChanged"
                            AutoPostBack="true" Style="width: 50px;">
                        <asp:ListItem Text="10"></asp:ListItem>
                        <asp:ListItem Text="25"></asp:ListItem>
                        <asp:ListItem Text="50"></asp:ListItem>
                        <asp:ListItem Text="75"></asp:ListItem>
                        <asp:ListItem Text="100"></asp:ListItem>
                    </asp:DropDownList>
                </td>
                <td style="text-align: right">
                    <asp:Label ID="lblPageCount" runat="server"></asp:Label>
                </td>
            </tr>
        </table>
    </PagerTemplate>
</cc1:GridView>

C#代码: -

protected void gvUser_RowCommand(object sender, GridViewCommandEventArgs e)
{
    try
    {
        if (e.CommandName.Equals("Edit"))
        {
            Control ctrl = e.CommandSource as Control;
            if (ctrl != null)
            {
                GridViewRow _currenrtrow = ctrl.Parent.NamingContainer as GridViewRow;
                Label lblUserID = (Label)_currenrtrow.FindControl("lblUserID");
                string userID = lblUserID.Text.ToString();
                ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "OpenModalDialog", "<script type='text/javascript'>window.showModalDialog('Userinfo.aspx?code=" + userID.ToString() + "', null, 'unadorned:yes ;resizable:0 ;dialogWidth:800px ;dialogHeight:550px ;status:no ;scroll:no ;status=no;');</script>", false);

            }
        }
        if (e.CommandName.Equals("Select"))
        {

        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

1 个答案:

答案 0 :(得分:0)

protected void gvUser_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName.Equals("Edit"))
            {
                Control ctrl = e.CommandSource as Control;
                string userId;
                if (ctrl != null)
                {
                    GridViewRow _currenrtrow = ctrl.Parent.NamingContainer as GridViewRow;
                    Label lblUserID = (Label)_currenrtrow.FindControl("lblUserID");
                    if (lblUserID == null)
                    {
                        userId = StaticGlobalValue.RequestedUser.ToString();
                    }
                    else
                    {
                        userId = lblUserID.Text.ToString();
                        StaticGlobalValue.RequestedUser = userId;
                    }

                    ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "OpenModalDialog", "<script type='text/javascript'>window.showModalDialog('Userinfo.aspx?code=" + userId.ToString() + "', null, 'unadorned:yes ;resizable:0 ;dialogWidth:800px ;dialogHeight:550px ;status:no ;scroll:no ;status=no;');</script>", false);
                    gvUser.DataBind();
                }
            }

            if (e.CommandName.Equals("Select"))
            {

            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }

创建了一个静态实体RequestedUser,它解决了我的问题。