如何获取触发 onSelecting 事件的数据源的 datasourceID?

发布于 2024-10-21 05:34:41 字数 1225 浏览 5 评论 0原文

我在 asp.net 页面上有几个不同的 sqldatasources,它们都触发相同的 onSelecting 事件处理程序。这样做的原因是因为事件处理程序中的相同代码可以应用于所有数据源(本质上是在查询中动态生成过滤器)。但是,现在我有另一个数据源,它仍然可以使用大部分代码,但需要稍微不同地处理。如果我可以引用触发事件的数据源的 ID(我已经尝试过),我可以很容易地做到这一点,但这似乎不可能。这是我最初尝试做的:

protected void sdsTable_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
    SqlDataSource sds = sender as SqlDataSource;
    string dsID = sds == null ? "" : sds.ID;
    if (dsID == "aDataSourceID")
    {
        //do this
    }
    else
    {
        //do that
    }

    //more code
 }

这不起作用,因为发件人的类型为 SqlDataSourceView 并尝试将发件人转换为 SqlDataSource 返回 null。所以,我将其更改为SqlDataSourceView:

protected void sdsTable_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
    SqlDataSourceView sds = sender as SqlDataSourceView;
    string dsID = sds == null ? "" : sds.Name; //Tried Name property because ID isn't available
    if (dsID == "aDataSourceID")
    {
        //do this
    }
    else
    {
        //do that
    }

    //more code
 }

但这仍然不起作用。 SqlDataSourceView 似乎没有可用的属性来提供当前触发事件的数据源的 datasourceID。有一个 SqlDataSourceView.Name 属性,但这是不同的。有谁知道在处理该事件时是否可以获得触发 Selecting 事件的数据源的 ID?如果是这样,您能提供一个示例来说明如何执行此操作吗?

谢谢!

I have a couple different sqldatasources on an asp.net page that all fire the same onSelecting Event Handler. The reason for this is because the same code in the event handler can be applied to all datasources (which essentially generates filters in the queries dynamically). However, now I have another datasource that can still use most of the code, but needs to be handled slightly differenty. I could do this very easily if I could reference the datasource's ID that is firing the event (which I've tried), but this doesn't seem to be possible. Here is what I initially attempted to do:

protected void sdsTable_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
    SqlDataSource sds = sender as SqlDataSource;
    string dsID = sds == null ? "" : sds.ID;
    if (dsID == "aDataSourceID")
    {
        //do this
    }
    else
    {
        //do that
    }

    //more code
 }

This didn't work because sender is of type SqlDataSourceView and trying to cast sender as SqlDataSource returns null. So, I changed it to SqlDataSourceView:

protected void sdsTable_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
    SqlDataSourceView sds = sender as SqlDataSourceView;
    string dsID = sds == null ? "" : sds.Name; //Tried Name property because ID isn't available
    if (dsID == "aDataSourceID")
    {
        //do this
    }
    else
    {
        //do that
    }

    //more code
 }

But this still doesn't work. SqlDataSourceView doesn't seem to have a property available that gives the datasourceID of the datasource that is currently firing the event. There is a SqlDataSourceView.Name property, but this is something different. Does anyone know if it is possible to get the ID of the DataSource firing the Selecting event when that event is being handled? If so, can you provide an example on how to do this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

誰ツ都不明白 2024-10-28 05:34:41

我还没有尝试过这个,但 SqlDataSource 有 GetViewNames() 方法,也许您可​​以获取 DataSource 的 ViewName,并检查哪一个具有当前的 SqlDataSourceView.Name

I'm have not tried this but SqlDataSource has GetViewNames() method and maybe you can get the ViewNames of your DataSources and check to see which one has the current SqlDataSourceView.Name.

悲念泪 2024-10-28 05:34:41

这有点hackish,但它确实有效。为每个 SqlDataSource 提供一个未使用的 SelectParameter,其名称相同,但默认值不同。例如,一个人可能有:

<asp:Parameter DefaultValue="Value1" Name="sdsName" Type="String" />

而另一个人可能有:

<asp:Parameter DefaultValue="Value2" Name="sdsName" Type="String" />

然后在选择事件时,您可以执行如下操作:

switch (e.Command.Parameters["@sdsName"].Value.ToString())
    {
        case "Value1":
            //Do something!
            break;
        case "Value2":
            //Do something!
            break;
    }

希望有人有更好的解决方案。

This is a bit hackish, but it works. Give each of the SqlDataSources a otherwise unused SelectParameter with the same name, but different default values. For example one might have:

<asp:Parameter DefaultValue="Value1" Name="sdsName" Type="String" />

And another might have:

<asp:Parameter DefaultValue="Value2" Name="sdsName" Type="String" />

Then on the selecting event you can do something like this:

switch (e.Command.Parameters["@sdsName"].Value.ToString())
    {
        case "Value1":
            //Do something!
            break;
        case "Value2":
            //Do something!
            break;
    }

Here's hoping someone has a better solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文