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:
<asp:Button ID="btnClear" runat="server" Text="Clear" CssClass="button1"
OnClientClick="if(!confirm('Are you sure?')){return false;}"
OnClick="btnClear_Click"
/>
protected void btnClear_Click(object sender, EventArgs e)
{
WEIS.EmissionUnitSetup objEmissionSetup = new WEIS.EmissionUnitSetup();
try
{
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 = "";
}
...
}