I am checking some condition in the GridView_RowCommand event. Based on the Condition I want to set SkinID or remove SkinID for the TextBox.
This is my Code:
if (dt.Rows[0]["D1Variable"].ToString() == "0")
{
txtMRVD1.Text = dt.Rows[0]["D1"].ToString();
txtMRVD1.ReadOnly = true;
txtMRVD1.SkinID = "txtreadonly";
}
else
{
txtMRVD1.Text = "";
txtMRVD1.ReadOnly = false;
}
But it throws the following error.
"The 'SkinId' property can only be set in or before the Page_PreInit event for static controls. For dynamic controls, set the property before adding it to the Controls collection."
How to solve this problem?
I would say, based on the error message, that you simply need to move that code to the
Page_PreInitevent.However, as MSDN says: “If the request is a postback, the values of the controls have not yet been restored from view state. If you set a control property at this stage, its value might be overwritten in the next event.”
You may need to get more creative in how you approach this – perhaps storing the information used to determine the skin id in Session. I suggest getting familiar, if you’re not already, with the ASP.NET Page Life Cycle.
Here’s an example that might work for you, or at least get you pointed in the right direction:
You can then do the rest of your logic in the GridView RowCommand event.