Click here to Skip to main content
15,886,100 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following codes that I tried to change it to grid view which is giving me 11 errors, all of them stating "name doesn't exist in current context." I am not much familiar with ASP.NET. Can anyone look at my codes and let me know where else do I need to edit or what did I not do right? I looked at my articles and blogs online but nothing is helping me right now.

Original code:

ASP.NET
<asp:TableRow ID="trDeviceDownData" runat="server" Visible="false">
                    <asp:TableCell Width="15%">
                        <asp:Label runat="server" ID="lblDeviceDown" Text="Device Down"></asp:Label>
                    </asp:TableCell>

                    <asp:TableCell Width="57%">
                        <asp:LinkButton ID="lnkDName" runat="server" OnClick="lnkDName_Click"></asp:LinkButton>
                        <asp:Label runat="server" ID="lblDtag"></asp:Label>
                    </asp:TableCell>

                    <asp:TableCell Width="7%">
                        <asp:Label runat="server" ID="lblDonval"></asp:Label>
                    </asp:TableCell>
                    <asp:TableCell Width="7%">
                        <asp:Label runat="server" ID="lblDoffval"></asp:Label>
                    </asp:TableCell>


                    <asp:TableCell Width="7%">
                        <asp:Button ID="btnModify" runat="server" Text="Modify" CssClass="button1" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    </asp:TableCell>
                    <asp:TableCell Width="7%">

                        <asp:Button ID="btnClear" runat="server" Text="Clear" OnClick="btnClear_Click" CssClass="button1" />

                    </asp:TableCell> 
       </asp:TableRow>


And this is what I did:

ASP.NET
<asp:TableRow ID="trDeviceDownData" runat="server" Visible="false">
                    <asp:TableCell Width="100%" ColumnSpan="6">

                        <asp:GridView ID="grdDeviceDown" runat="server" Width="100%" CellPadding="0" CellSpacing="1"
                             AutoGenerateColumns="false"
                             OnRowDataBound="grdDeviceDown_RowDataBound"
                             OnRowDeleting="grdDeviceDown_RowDeleting" 
                             OnRowCommand="grdDeviceDown_RowCommand" GridLines="None">
                     <Columns>
                          <asp:TemplateField ItemStyle-Width="15%">
                                    <ItemTemplate>
                        <asp:Label runat="server" ID="lblDeviceDown" Text="Device Down"></asp:Label>
                    </ItemTemplate>
                   </asp:TemplateField>

                    <asp:TemplateField ItemStyle-Width="57%">
                                    <ItemTemplate>
                        <asp:LinkButton ID="lnkDName" runat="server" OnClick="lnkDName_Click"
                       CommandName="Modify" 
                       CommandArgument='<%# Eval("reci_seq") +"," +Eval("pims_Status") %>'>
                                             <%# Eval("shortname") %> 
                          </asp:LinkButton>
                                        <i>(<%# Eval("pims_tag_id") %>)</i>
                                        </ItemTemplate>

                                </asp:TemplateField>

                         <asp:TemplateField ItemStyle-Width="7%">
                             <ItemTemplate>
                                 <asp:Label runat="server" ID="lblDtag"></asp:Label>
                             </ItemTemplate>
                        </asp:TemplateField>

                    <asp:TemplateField ItemStyle-Width="7%">
                                    <ItemTemplate>
                        <asp:Label runat="server" ID="lblDonval"></asp:Label>
                   </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField ItemStyle-Width="7%">
                                    <ItemTemplate>
                        <asp:Label runat="server" ID="lblDoffval"></asp:Label>
                      </ItemTemplate>
                                </asp:TemplateField>


                    <asp:TemplateField ItemStyle-Width="7%">
                                  <ItemTemplate>
                        <asp:Button ID="btnModify" runat="server" Text="Modify" CssClass="button1" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    </ItemTemplate>
                                </asp:TemplateField>

                   <asp:TemplateField ItemStyle-Width="7%">
                                    <ItemTemplate>

                        <asp:Button ID="btnClear" runat="server" Text="Clear" OnClick="btnClear_Click" CssClass="button1" />
                             </ItemTemplate>
                                </asp:TemplateField>
                         <asp:BoundField DataField ="EVNT_SEQ" />
                         <asp:BoundField DataField="RECI_SEQ" />
                  </Columns>

                         </asp:GridView>
                     </asp:TableCell>                 
                </asp:TableRow>


In my aspx.cs here is where I am getting errors: "lnkDName", "lblDtag","lblDoffval", "lblDonval", "btnModify" doesn't exit in current context.

C#
if (pName == "" && pTag == "")
                    {
                        trDeviceDownSwitchbutton.Visible = false;
                        //trDeviceDownData.Visible = true;
                    }
                    else
                    {
                        trDeviceDownData.Visible = true;
                        lnkDName.Visible = true;
                        lnkDName.Text = pName;
                        lblDtag.Text = "<i>(" + pTag + ")</i>";
                        lblDonval.Text = onVal;
                        lblDoffval.Text = offVal;
                    }
                        btnModify.Attributes.Add("onclick", "javascript: return popswitch('" + eventSeq + "')");


And here:
C#
protected void btnClear_Click(object sender, EventArgs e)
     {
       WEIS.EmissionUnitSetup objEmissionSetup = new WEIS.EmissionUnitSetup();
       try
       {
           DialogResult result = MessageBox.Show(Resources.Resource.ProcessSetup_ClearBtnMsg, "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MessageBoxOptions.DefaultDesktopOnly);
           if (result == DialogResult.Yes)
           {
               flag.Value = "pdwndel";
               DeleteEventCtrl();
               DeleteEvent();
               DeleteRI();
               trDeviceDownData.Visible = false;
               trDeviceDownSwitchbutton.Visible = true;

               lblDtag.Text = "";
               lblDonval.Text = "";
               lblDoffval.Text = "";
               lnkDName.Text = "";
               lnkDName.Visible = false;
                flag.Value = "";
            }
         }


What I have tried:

I tried to restart my visual studio and even my computer. Looked for namespace if missing. Tried to refactor. Deleted all the changes and restarted from the beginning.
Posted
Updated 1-Apr-16 10:38am
Comments
Karthik_Mahalingam 1-Apr-16 13:22pm    
means those controls doesn't exist in the .aspx file.
try to add them once again with the same name.

The controls inside your TemplateField do not create fields in the code-behind, because there will be multiple copies of those controls - one for each row in the data source that you bind your GridView to. You'll need to handle the RowDataBound event, and use FindControl to find the instance of the controls within that specific row.

Also, since you're using ASP.NET, MessageBox.Show will not work. It might appear to work when you debug your code in Visual Studio, but once you deploy to a real server, it will fail. If you're lucky, you'll get an exception telling you the current process is not interactive. Otherwise, the message will pop up on the server, where nobody will ever see it, and your code will hang waiting for someone to log in to the server and press the button.

If you want the user to click "OK" before continuing, you'll need to use some javascript code:
ASPX
<asp:Button ID="btnClear" runat="server" Text="Clear" CssClass="button1" 
    OnClientClick="if(!confirm('Are you sure?')){return false;}"
    OnClick="btnClear_Click"
/>

C#
protected void btnClear_Click(object sender, EventArgs e)
{
    WEIS.EmissionUnitSetup objEmissionSetup = new WEIS.EmissionUnitSetup();
    try
    {
        // DialogResult result = MessageBox.Show(Resources.Resource.ProcessSetup_ClearBtnMsg, "", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MessageBoxOptions.DefaultDesktopOnly);
        // if (result == DialogResult.Yes)
        // {
            flag.Value = "pdwndel";
            DeleteEventCtrl();
            DeleteEvent();
            DeleteRI();
            trDeviceDownData.Visible = false;
            trDeviceDownSwitchbutton.Visible = true;
 
            lblDtag.Text = "";
            lblDonval.Text = "";
            lblDoffval.Text = "";
            lnkDName.Text = "";
            lnkDName.Visible = false;
            flag.Value = "";
        // }
    }
    ...
}
 
Share this answer
 
v2
@Richard- thank you. As you mentioned I handled the RowDataBound. All the errors disappeared but 1 new error popped up. "A control with ID 'lnkDName' could not be found for the trigger in UpdatePanel 'UpdatePanel1'. I did include lnkDName that trigger in UpdatePanel. Isn't how this is done or I am off track? Can you take a peak again and suggest me when you have time please?

ASP.NET
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
*******************
**********************
<triggers>
     <asp:postbacktrigger controlid="btnDeviceDownSwitch" xmlns:asp="#unknown" />
     <asp:postbacktrigger controlid="lnkDName" xmlns:asp="#unknown" />
</triggers>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900