Sorry, I couldn't get to this earlier... Had an extremely hectic
week !
I must say that your version of "simplified" is actually far from
simple in my opinion. In order to create a working test page, I had to
remove a lot of stuff (including the custom user control) and had to
create a table structure purely based on deduction. Nevertheless,
after a few hours spent on this, I have a lot of points to elucidate
upon. Note that I have modified the SELECT statements and variable
names as per my convenience.
1. Let's start with your UserControl -
_App_Controls_ctlUserNameSelect. I have no idea how it functions or
what properties it exposes but I think the more intuitive way of
handling it would be to use a bindable property of the control instead
of applying checks in the code-behind. I don't really see why you need
to handle Load and Init events of individual controls at all. You also
don't seem to require the hidden field (hfUserID) as an intermediary
to the Username control. For details, read on...
2. Regarding the DropDownList, you can simply bind it as follows:
ASPX:
----
<asp:DropDownList ID="ddlRequestType" runat="server"
DataSourceID="dsRequestType" DataTextField="ReqType"
DataValueField="ReqTypeID" SelectedValue='<%# Bind("ReqType") %>'
AppendDataBoundItems="true">
<asp:ListItem Text="Select" Value="0" />
</asp:DropDownList>
<asp:SqlDataSource ID="dsRequestType" runat="server"
ConnectionString="<%$ConnectionStrings:MyConn %>"
SelectCommand="SELECT ReqTypeID, ReqType FROM RequestTypes WHERE
ReqCategory = @ReqCategory" OnSelecting="dsRequestType_Selecting">
<SelectParameters>
<asp:Parameter Name="ReqCategory" Type="String"
DefaultValue="Public" />
</SelectParameters>
</asp:SqlDataSource>
Code-behind:
-----------
protected void dsRequestType_Selecting(object sender,
SqlDataSourceSelectingEventArgs e)
{
string privateOrPublic = GetPrivateOrPublic();
DbCommand cmd = e.Command;
if ((cmd.Parameters.Count > 0) && (cmd.Parameters.Contains
("@ReqCategory")))
{
cmd.Parameters["@ReqCategory"].Value = privateOrPublic;
}
}
private string GetPrivateOrPublic()
{
// Return value as a combination of the logic within the
"ctlUserName_Refresh" method and the "hfUserID_Init" method.
// Neither of those methods should be required if this is done
right.
}
Note that:
a) I have preferred to handle the SqlDataSource Selecting event and
set the parameter value there instead of retrieving and storing values
in other events such as the Init or Refresh.
b) The GetPrivateOrPublic() method would retrieve the value from the
ServerVariables and AppSettings and store it in a string variable.
This would then be compared with the value in the ctlUserNameSelect
control and "Private"/"Public" would be returned accordingly.
c) I have dispensed with the ddlRequestType_DataBound event and
instead chose to add the Default item "Select" at design time itself.
Setting the "AppendDataBoundItems" property allows me to do this.
d) I bind the "SelectedValue" property of the DDL to my datasource,
which makes sure that the selected value will automatically be set
when selecting data and retrieved when inserting data.
Using similar logic, I do not require any ControlParameters in the
InsertParameters section of the "dsEntry" SqlDatasource. I can set any
custom parameters in the dsEntry_Inserting event. All other parameter
values should directly result from the property Binding that occurs
during design mode. This pattern precludes the need for having Hidden
fields as an intermediary.
Therefore, to answer the basic question raised by you, I do think that
not a single hidden field is required in the scenario presented by
you.
HTH,
--
Cerebrus.