How should I pass a variable to a "controlid" on a ControlParameter.
-
I need to pass a variable to the controlid of a ControlParameter so i can tell a Gridview which row the dropdownlist im editing belongs to. What would be the best moethod for setting the control id? Thanks.
-
I need to pass a variable to the controlid of a ControlParameter so i can tell a Gridview which row the dropdownlist im editing belongs to. What would be the best moethod for setting the control id? Thanks.
Basically, the
ControlParameter
does not support theDataBinding
event so you cannot use a data binding expression to set theControlID
property of the parameter, and your choice is to do this in code. Also, you need to be aware that theControlID
property needs to be updated before it is used to populate the value, if it happens after then you'll get an error as the specified control is not found. Normally, the ControlParameter uses its ControlID property to evaluate the parameter value in the overridableEvaluate
method which occurs in theLoadComplete
event of the Page instance. So you can put your code to update the ControlID in the events (of the data source control or the Page instance) that happens before the Page_LoadComplete and of cource after the data source control is built. For example, you can use theLoad
event of the data source control:protected void SqlDataSource1_Load(object sender, EventArgs e)
{
//Assuming the ControlPatameter is the first one in the UpdateParameter collection.
ControlParameter para = SqlDataSource1.UpdateParameters[0] as ControlParameter;
para.ControlID = "GridView1$ctl0" + (GridView1.EditIndex + 2) + "$DropDownList1";
}where the
EditIndex
property of the GridView control will give you the index of the row being edited, this is a zero-based value, however the index used in the UniqueID of the dropdownlist is a one-based value and it is counted from the header row. Therefore, you have to add2
to the EditIndex to get the correct number used in the UniqueID.