Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 为SqlDataSource动态设置参数字段_Asp.net - Fatal编程技术网

Asp.net 为SqlDataSource动态设置参数字段

Asp.net 为SqlDataSource动态设置参数字段,asp.net,Asp.net,我需要动态填写“SelectParameters”,但我在Google上找到的只是使用固定参数 <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="XX" OnSelecting="SqlDataSource2_Selecting" SelectCommand=""> <Sel

我需要动态填写“SelectParameters”,但我在Google上找到的只是使用固定参数

<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="XX"
                            OnSelecting="SqlDataSource2_Selecting" SelectCommand="">
                            <SelectParameters>
                            </SelectParameters>
                        </asp:SqlDataSource>

您可以在代码隐藏中设置如下变量:

SqlDataSource1.SelectParameters.Add("parameterName", "parameterValue");
protected void SqlDataSource1_Init(object sender, EventArgs e)
{
     SqlDataSource1.ConnectionString = <whatever code you want here>;
     SqlDataSource1.ProviderName = <whatever code you want here>;
}

但是请注意,您需要在添加参数之前清除这些参数。否则,您将得到重复的参数

您可以在代码隐藏中设置如下变量:

SqlDataSource1.SelectParameters.Add("parameterName", "parameterValue");
protected void SqlDataSource1_Init(object sender, EventArgs e)
{
     SqlDataSource1.ConnectionString = <whatever code you want here>;
     SqlDataSource1.ProviderName = <whatever code you want here>;
}

但是请注意,您需要在添加参数之前清除这些参数。否则,您将得到重复的参数

如果在初始化控件时设置连接字符串没有问题,可以在控件的init事件上这样做:

SqlDataSource1.SelectParameters.Add("parameterName", "parameterValue");
protected void SqlDataSource1_Init(object sender, EventArgs e)
{
     SqlDataSource1.ConnectionString = <whatever code you want here>;
     SqlDataSource1.ProviderName = <whatever code you want here>;
}
protectedvoid SqlDataSource1_Init(对象发送方,事件参数e)
{
SqlDataSource1.ConnectionString=;
SqlDataSource1.ProviderName=;
}

这仍然是动态的,因为您在运行时而不是设计时确定连接属性。这有助于我避免求助于web.xml转换,并根据指定QA/PROD环境的环境变量在QA和PROD连接字符串之间切换。

如果在初始化控件时设置连接字符串没有问题,可以在控件的init事件上这样做:

SqlDataSource1.SelectParameters.Add("parameterName", "parameterValue");
protected void SqlDataSource1_Init(object sender, EventArgs e)
{
     SqlDataSource1.ConnectionString = <whatever code you want here>;
     SqlDataSource1.ProviderName = <whatever code you want here>;
}
protectedvoid SqlDataSource1_Init(对象发送方,事件参数e)
{
SqlDataSource1.ConnectionString=;
SqlDataSource1.ProviderName=;
}

这仍然是动态的,因为您在运行时而不是设计时确定连接属性。这有助于我避免求助于web.xml转换,并根据指定QA/PROD环境的环境变量在QA和PROD连接字符串之间切换。

是否要设置动态参数的值或变量和值?@Arion仅在参数字段中,我知道如何设置值,但在参数不存在时失败(我刚刚更新了我的问题)。您想设置动态参数的值还是变量和值?@a仅在参数字段中,我知道如何设置值,但在参数不存在时失败(我刚更新了我的问题)