In order to meet the requirements, the grid should have AutoGenerateColumns = true
with no use of TemplateField
. Only manipulation of the .cs
file is allowed. Key requirements include:
- The grid should be autogenerated
- Sorting and paging features are permitted
It seems that Asp.Net fills the header row deep inside, as during events like PreRender
or RowDataBound
, the header row remains empty. Renaming it may work, but then it renders as plain text. Attempting to hardcode the postback URL leads to an exception being thrown in the following code block:
private void FillHeaders()
{
const string linkText = @"<a href=""javascript:__doPostBack('ctl00$PlaceHolderMain$AuditGridView','Sort${0}')"">{1}</a>";
bool a = true;
if (a)
for (int i = 0; i < LanitAudit.Properties.Length; i++)
{
AuditGridView.HeaderRow.Cells[i].Text = string.Format(linkText, LanitAudit.Properties[i].Key, LanitAudit.Properties[i].Value);
}
}
This results in the exception message:
Invalid postback or callback argument. Event validation is enabled either through configuration settings or by adding <%@ Page EnableEventValidation="true" %> to the page. To ensure security, this feature checks that postback or callback arguments originate from the server control that initially rendered them. If the data is valid and expected, you can use ClientScriptManager.RegisterForEventValidation to register the postback or callback data for validation.
Client-side JavaScript usage is not an option at this point.
Thank you for the responses, however, my question may be unclear. While I can replace header text, after doing so I encounter issues with sorting the gridview (as per the second requirement). The screenshot below illustrates the problem - clicking on the new header text does not trigger any action as intended. Unfortunately, attempting to manually trigger __doPostBack leads to the error mentioned earlier.