0

I have an aspx-Page with an int in Codebehind:

private const int id = 11;

And in Markup I have

<asp:SqlDataSource ID="SqlDataSource" runat="server" 
 SelectCommand="SELECT Name FROM [StatusOption] WHERE StatusId = 11">

I would like to reference the id from codebehind in the markup, so that I only have to change it in codebehind when necessary. Is it possible and how.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
AGuyCalledGerald
  • 7,882
  • 17
  • 73
  • 120

1 Answers1

2

You could just set the desired value in the SqlDataSource.Selecting event.

I added a new select parameter to your data source - statusId and set a value to it in the SqlDataSource_Selecting event handler:

<asp:SqlDataSource ID="SqlDataSource" runat="server" 
    SelectCommand="SELECT Name FROM [StatusOption] WHERE StatusId = @statusId"
    OnSelecting="SqlDataSource_Selecting">
    <SelectParameters>
        <asp:Parameter Name="statusId" Type="Int32" />
    </SelectParameters>

and then in code-behind:

protected void SqlDataSource_Selecting(
    object sender, 
    SqlDataSourceSelectingEventArgs e)
{
    e.Command.Parameters["@statusId"].Value = id;
}
Oleks
  • 31,955
  • 11
  • 77
  • 132