LinkButton is not initializing the control inside grid view for second time

Hzyyqmrdgomhz

New Member
I had a \[code\]gridview\[/code\] in which there is a \[code\]TemplateField\[/code\] associated with a \[code\]LinkButton\[/code\] in \[code\]ItemTemplate\[/code\] with \[code\]CommandName\[/code\] as "Edit". When ever a user clicks on the \[code\]LinkButton\[/code\] it opens a ModalPOp Up to edit the current row records of the grid.For the first time it is working fine and as the modalPopUp is closed and user again clicks on the same LinkButton, it is not working a getting \[code\]object reference not set to an instance of an object\[/code\] exception. After debugging i found that for the second time their is a Label \[code\]lblUserID\[/code\] in the current selected row is not initializing. Please suggest me that how i fix this issue as it is very urgent.Please find the Code below:-\[code\]<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="http://stackoverflow.com/questions/14577433/0" /> <asp:ListItem Text="Disable" Value="http://stackoverflow.com/questions/14577433/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="http://stackoverflow.com/questions/14577433/1" /> <asp:ListItem Text="TL/TC" Value="http://stackoverflow.com/questions/14577433/2" /> <asp:ListItem Text="AM" Value="http://stackoverflow.com/questions/14577433/3" /> <asp:ListItem Text="OM" Value="http://stackoverflow.com/questions/14577433/4" /> <asp:ListItem Text="Sr OM" Value="http://stackoverflow.com/questions/14577433/5" /> <asp:ListItem Text="Admin" Value="http://stackoverflow.com/questions/14577433/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>\[/code\]C# Code :-\[code\]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; }}\[/code\]
 
Back
Top